-2

アイテムごとに少なくとも 2 つのデータを格納できるデータ構造が必要です。挿入された順にアイテムを保存したい。

例:

Insert (xyz, string) ... insert (789, number)
System.out.println( dataStruct );

(xyz, string)その後印刷します(789,number)

可能であれば、いくつかのサンプルコードを示してください。ありがとうございました!

4

4 に答える 4

1
public class Store {
String str1;
String str2;

public Store(String string1, String string2) {
    str1 = string1;
    str2 = string2;
}

public static void main(String[] args) {

    Store[] s = new Store[10]; // size
    for (int i = 0; i < s.length; i++) {
        s[i] = new Store("A", "B");
        System.out.println(s[i].str1);
        System.out.println(s[i].str2);

    }

}
}

これがあなたが探しているものだと思います。これはCのような構造です。これを試して。これにより、わずかな変更を加えるだけで、アイテムごとに2つ以上のデータを挿入できます。そして秩序も維持されます。

于 2013-01-29T09:43:55.770 に答える
1

ハッシュテーブルまたはリストを使用できます

例:

Hashtable<Integer,String> hTable=new Hashtable<Integer,String>();
//adding or set items in Hashtable by put method key and value pair
hTable.put(new Integer(2), "Two");
hTable.put(new Integer(1), "One");
hTable.put(new Integer(4), "Four");
hTable.put(new Integer(3), "Three");
hTable.put(new Integer(5), "Five");

いくつかのJava 構造を見てみましょう。

編集

挿入順序を維持するために、LinkedHashSetまたはLinkedHashMapを使用できます。

于 2013-01-29T09:27:05.037 に答える
1

以下のコードは、データ構造にすることができます:

 List<Item> list;

    class Item {
     public String desc;
     public Integer val;
     void insert(String desc,Integer val){
     this.desc = desc;
     this.val = val; 
     }   
     void insert(Integer val,String desc){
     insert(desc,val);
     }
    }
于 2013-01-29T09:24:55.983 に答える
0

HashMap のコンストラクターで型のパラメーターを使用することもできます。

HashMap map= new HashMap();
List<HashMap> virtualDs= new ArrayList<HashMap>();
于 2013-01-29T09:44:28.630 に答える