我正在尝试在处理多个“动物”的哈希表中编写线性探测的解决方案,类似于下面给我的那个。
index++;
if(index == hashAnimals.length) {
index = 0;
}
if(hashAnimals[index] == null){
hashAnimals[index] = new Animal(animals.get(i));
}
但有人告诉我,这只是一种动物的解决方案,在一个位置。所以这是我目前针对多种动物的解决方案:
int index = 0;
for(int i = 0; i < hashAnimals.length) {
if(index = hashAnimals.length){
index = 0;
}
但是我在考虑找到一个自由职位的解决方案时遇到了问题。我应该使用另一个 for 循环吗?如果我只是在我的代码中复制了上面的第二个 if 语句,我相信如果索引已经被占用,我试图添加的“动物”将被跳过,并且“i”将在末尾递增到下一个动物循环。