2 个哈希表键

发布于 2024-11-04 15:06:39 字数 1026 浏览 1 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风追烟花雨 2024-11-11 15:06:41

您正在为两条记录插入相同的哈希表。当您插入第二条记录时,您会覆盖第一条记录的值,因此您会获得第二条记录两次。

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..

爱她像谁 2024-11-11 15:06:40

在创建第一条记录和第二条记录之间,输入:

this.items = new Hashtable <String, String>();

问题是您为两条记录重复使用相同的哈希表。

另外,现在你应该使用 HashMap 而不是 Hashtable,并且应该声明具有最广泛有用类型的变量,这里意味着 List 而不是 ArrayList,以及 Map 而不是 Hashtable 或 HashMap。变量类型中不需要描述实现类;这是使用变量时不需要知道的细节,所以它只是混乱。

In between the creation of the first and second records, put:

this.items = new Hashtable <String, String>();

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.

风追烟花雨 2024-11-11 15:06:40

您将对同一个哈希表的引用两次放入 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 of finalArray.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文