我需要在表格中找到最大搜索因子和平均搜索因子。
我有应该是什么的统计数据,我的统计数据不断超出给定的数据。
public void printStatistics() {
int numFilledLocations = 0;
for (Hash hash: hashTable) {
if (!hash.val.isEmpty()) {
numFilledLocations++;
}
}
System.out.println("Load Factor: " + ((float) numFilledLocations) / (float) tableSize);
int maxLength = 0;
int totalLengths = 0;
for (Hash hash: hashTable) {
int length = 1;
Hash nextHash = hash;
while (nextHash.next != -1) {
length++;
nextHash = hashTable[nextHash.next];
}
if (length > maxLength) {
maxLength = length;
}
totalLengths += length;
}
System.out.println("Sum of length linked list: " + totalLengths);
System.out.println("Maximum Search Factor: " + maxLength);
System.out.println("Average Search Factor: " + ((float) totalLengths / (float) tableSize));
}