我需要遍历一个BucketMap并获取所有的键,但如果我想获取类似于bucktes[i].next.next.next.key这样的内容,该怎么做,而不是手动尝试如下所示:
public String[] getAllKeys() {
//index of string array "allkeys"
int j = 0;
String allkeys[] = new String[8];
//iterates through the bucketmap
for (int i = 0; i < buckets.length; i++) {
//checks wether bucket has a key and value
if (buckets[i] != null) {
//adds key to allkeys
allkeys[j] = buckets[i].key;
// counts up the allkeys index after adding key
j++;
//checks wether next has a key and value
if (buckets[i].next != null) {
//adds key to allkeys
allkeys[j] = buckets[i].next.key;
j++;
}
}
}
return allkeys;
}
还有,我该如何使用迭代结束后得到的版本 j
作为索引来初始化 String[] allkeys
?