2 个哈希表键
我有 2 条记录,即标题。
示例
记录1:我的标题
记录 2:我的另一个标题
我需要使用 Hashtable 将它们存储到 ArrayList 中。
这就是我所做的。
package com.Testing;
import java.util.Hashtable;
import java.util.ArrayList;
import java.util.Dictionary;
public class AnotherClass {
private Hashtable <String, String> items = new Hashtable <String, String>();
private ArrayList <Hashtable <String, String>> finalArray = new ArrayList <Hashtable <String, String>>();
public ArrayList <Hashtable <String, String>> returnArray() {
return finalArray;
}
public void adding() {
this.items.put("Record", "Record 1");
this.items.put("Format", "My Title");
this.finalArray.add(items);
this.items.put("Record", "Record 2");
this.items.put("Format", "My ANOTHER Title");
this.finalArray.add(items);
}
}
当我通过遍历数组列表打印项目结果时,它只显示第二条记录。
有什么建议让它显示两条记录吗?
谢谢!
i'm having 2 records that is a title.
Example
Record 1: My Title
Record 2: My Another Title
I need to store them into an ArrayList using Hashtable.
This is what i do.
package com.Testing;
import java.util.Hashtable;
import java.util.ArrayList;
import java.util.Dictionary;
public class AnotherClass {
private Hashtable <String, String> items = new Hashtable <String, String>();
private ArrayList <Hashtable <String, String>> finalArray = new ArrayList <Hashtable <String, String>>();
public ArrayList <Hashtable <String, String>> returnArray() {
return finalArray;
}
public void adding() {
this.items.put("Record", "Record 1");
this.items.put("Format", "My Title");
this.finalArray.add(items);
this.items.put("Record", "Record 2");
this.items.put("Format", "My ANOTHER Title");
this.finalArray.add(items);
}
}
When i do a print of my result of items by traversing the arraylist it only shows me the 2nd record.
any advice in getting it to show both records?
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在为两条记录插入相同的哈希表。当您插入第二条记录时,您会覆盖第一条记录的值,因此您会获得第二条记录两次。
You're inserting the same Hashtable for both records. As you're inserting the second record, you override the values for the first, so you get the second twice..
在创建第一条记录和第二条记录之间,输入:
问题是您为两条记录重复使用相同的哈希表。
另外,现在你应该使用 HashMap 而不是 Hashtable,并且应该声明具有最广泛有用类型的变量,这里意味着 List 而不是 ArrayList,以及 Map 而不是 Hashtable 或 HashMap。变量类型中不需要描述实现类;这是使用变量时不需要知道的细节,所以它只是混乱。
In between the creation of the first and second records, put:
The problem is that you're reusing the same hashtable for both records.
Also, you should use HashMap instead of Hashtable these days, and you should declare variables with the broadest useful type, which here means List rather than ArrayList, and Map rather than Hashtable or HashMap. There is no need to describe the implementation class in the variable's type; it's a detail you don't need to know when using the variable, so it's just clutter.
您将对同一个哈希表的引用两次放入
finalArray
中。当您更改哈希表时,您将看到更改会影响finalArray
的两个元素。You're putting a reference to the same hashtable into
finalArray
twice. When you change the hashtable, you will see the changes affect both elements offinalArray
.