Java 具有链表实现和存储文本文件的哈希表
我想用链表和链表实现我自己的哈希表,但是我很难弄清楚如何在main方法中使用这个实现。我需要读取一个包含数据的逗号分隔值文件,并将名称存储为键,将两个浮点数存储为值。我知道我需要使用面向对象编程,但我很难使用我的实现访问数据 这是我的密码: 公共类LinkedListHash{Java 具有链表实现和存储文本文件的哈希表,java,oop,linked-list,hashtable,Java,Oop,Linked List,Hashtable,我想用链表和链表实现我自己的哈希表,但是我很难弄清楚如何在main方法中使用这个实现。我需要读取一个包含数据的逗号分隔值文件,并将名称存储为键,将两个浮点数存储为值。我知道我需要使用面向对象编程,但我很难使用我的实现访问数据 这是我的密码: 公共类LinkedListHash{ String key; String value; LinkedListHash next; public LinkedListHash(){ } LinkedLis
String key;
String value;
LinkedListHash next;
public LinkedListHash(){
}
LinkedListHash(String key, String value){
this.key = key;
this.value = value;
this.next = null;
}
public String getValue(){
return value;
}
public void setValue(String value){
this.value = value;
}
public String getKey(){
return key;
}
public LinkedListHash getNext(){
return next;
}
public void setNext(LinkedListHash next){
this.next = next;
}
class Hashtable {
int size = 0;
LinkedListHash[] table;
Hashtable(){
table = new LinkedListHash[size];
for (int i = 0; i < size; i++){
table[i] = null;
}
}
public String get(String key){
int hash = key.hashCode();
if (table[hash] == null){
return null;
}
else {
LinkedListHash input = table[hash];
while (input != null && input.getKey() != key){
input = input.getNext();
}
if (input == null){
return null;
}
else {
return input.getValue();
}
}
}
public void put(String key, String value){
int hash = key.hashCode();
if (table[hash] == null){
table[hash] = new LinkedListHash(key, value);
}
else {
LinkedListHash input = table[hash];
while (input.getNext() != null && input.getKey() != key){
input = input.getNext();
}
if (input.getKey() == key){
input.setValue(value);
}
else {
input.setNext(new LinkedListHash(key, value));
}
}
}
}
}
public static void main(String[] args) throws FileNotFoundException{
Hashtable<String, String> tbl = new Hashtable<String, String>();
String path = args[0];
if(args.length < 1) {
System.out.println("Error, usage: java ClassName inputfile");
System.exit(1);
}
Scanner reader = new Scanner(new FileInputStream(args[0]));
while((path = reader.nextLine()) != null){
String parts[] = path.split("\t");
tbl.put(parts[0], parts[1]);
} reader.close();
} }
字符串键;
字符串值;
LinkedListHash下一步;
公共LinkedListHash(){
}
LinkedListHash(字符串键、字符串值){
this.key=key;
这个值=值;
this.next=null;
}
公共字符串getValue(){
返回值;
}
公共void设置值(字符串值){
这个值=值;
}
公共字符串getKey(){
返回键;
}
公共LinkedListHash getNext(){
下一步返回;
}
public void setNext(LinkedListHash next){
this.next=next;
}
类哈希表{
int size=0;
LinkedListHash[]表;
哈希表(){
table=新的LinkedListHash[大小];
对于(int i=0;i
任何我可以改进代码的方法都会有帮助。
请记住,我不是一个非常有经验的程序员,所以我为任何可怕的错误道歉
字符串
s不应与=
或进行比较=代码>除非您想知道它们是否是占用相同内存位置的相同字符串。请改用equals()
。String
s不应与=
或进行比较=代码>除非您想知道它们是否是占用相同内存位置的相同字符串。改用equals()
。我不知道我为什么这么做。谢谢你的评论,我不知道我为什么这么做。谢谢你的评论