当我尝试使用Display方法时,输出的只是无意义的数字,为什么?我认为输出应该是整数

  • 本文关键字:输出 整数 数字 无意义 方法 Display java
  • 更新时间 :
  • 英文 :

public class HashTable implements IHashTable{
private DataItem[] HashArray;
private int ArraySize;
private DataItem NonItem;
public HashTable(int Size)
{
this.ArraySize = Size;
this.HashArray = new DataItem[this.ArraySize];
this.NonItem = new DataItem(-1);
}
@Override
public void DisplayHashTable() {
// TODO Auto-generated method stub

System.out.println("The Table Is :n");
for(int i =0 ; i < this.ArraySize; i ++)
{
if(this.HashArray[i] != null)
System.out.print(this.HashArray[i] + "  ");
else
System.out.println("** ");
}

System.out.println(" ");

}
@Override
public int HashFunc(int Key) {
// TODO Auto-generated method stub

return Key % this.ArraySize;
}
@Override
public void insert(DataItem item) {
// TODO Auto-generated method stub
int Key = item.GetKey();
int HashVal = HashFunc(Key);
while (this.HashArray[HashVal] != null
&&this.HashArray[HashVal].GetKey() != -1)
{
++HashVal;
HashVal%=this.ArraySize;
}
this.HashArray[HashVal] = item;
// end .
}
@Override
public DataItem Delete(int Key) {
// TODO Auto-generated method stub
int HashVal = HashFunc(Key);
while(this.HashArray[HashVal] != null)
{
if(this.HashArray[HashVal].GetKey()== Key)
{
DataItem Temp = this.HashArray[HashVal];
this.HashArray[HashVal] = this.NonItem;
return Temp;
}
++HashVal;
HashVal %= this.ArraySize;
}
return null;
}
@Override
public DataItem Find(int Key) {
// TODO Auto-generated method stub
int HashVal = HashFunc(Key);
while(this.HashArray[HashVal] != null)
{
if(this.HashArray[HashVal].GetKey() == Key)
return this.HashArray[HashVal];
++HashVal;
HashVal %= this.ArraySize;
}
return null;
}

}`


导入java.util.*;公共类HashTableTest{

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int Key ;
DataItem aDataItem;
IHashTable HashTable = new HashTable(5);
Menu();
int ans =sc.nextInt();
while(ans !=5)
{

switch(ans){
case 1 :
HashTable.DisplayHashTable();
break;
case 2 :
Key = sc.nextInt();
aDataItem = new DataItem(Key);
HashTable.insert(aDataItem);
System.out.println("The Item Has Inserted");
break;
case 3 :
System.out.println("What Value You want to delete");
Key = sc.nextInt();
HashTable.Delete(Key);
break;
case 4 :
System.out.println("What Value You want to Find");
Key = sc.nextInt();
aDataItem=HashTable.Find(Key);
if(aDataItem != null)
System.out.println("Found Key");
System.out.println("Could not Found" + Key);
break;
default :
System.out.println("Wrong Number");
}
Menu();
ans =sc.nextInt();

}


}
public static void Menu()
{
System.out.println("1 - Show The Table n 2- Insert New Value n"
+"3- Delete A Value n 4- Find A Valuen 5- Exit.");
}

}

//输出

1-显示表格2-插入新值3-删除值4-查找值5-退出。1.表格为:

**********

1-显示表格2-插入新值3-删除值4-查找值5-退出。2.254项目已插入1-显示表格2-插入新值3-删除值4-查找值5-退出。2.75项目已插入1-显示表格2-插入新值3-删除值4-查找值5-退出。2.5.项目已插入1-显示表格2-插入新值3-删除值4-查找值5-退出。1.表格为:

DataItem@42d3bd8bDataItem@26ba2a48****DataItem@5f2050f6
1-显示表格2-插入新值3-删除值4-查找值5-退出。`

您的HashArrayDataItems的数组。当您调用时

System.out.print(this.HashArray[i] + "  ");

DisplayHashTable方法的for循环中,基本上是在索引i处调用DataItem并打印它。由于这是一个对象,它在内存中的地址("无意义数字"(被打印到终端。

解决这个问题的一种方法是在DataItem类中添加一个方法:

public void toString()
{
//print whatever you want to output here
}

这应该根据你想要的来工作。

最新更新