【问题标题】:set JavaME List index设置 JavaME 列表索引
【发布时间】:2011-03-31 20:03:33
【问题描述】:

我是 JavaME 新手,需要一些帮助。

我想使用一个列表对象,需要设置这个列表中条目的索引..

我使用“list.addRecord(..)”函数 atm 添加条目。 效果很好,但我怎么说,我想设置条目索引'..

示例

使用“添加记录”功能:

0 Entry1
1 Entry2
2 Entry3
...

我需要什么:

4 Entry1
1 Entry2
10 Entry3
...

有可能吗? 谢谢。

【问题讨论】:

    标签: list java-me indexing


    【解决方案1】:

    有几种方法可以做到这一点。

    1。使用Hashtable

    Hashtable list=new Hashtable();
    
    list.put(new Integer(4),"Entry1");
    list.put(new Integer(1),"Entry2");
    list.put(new Integer(0),"Entry3");
    
    x=list.get(new Integer(1)); // "Entry2"
    

    2。使用两个数组(或向量)

    int[]keys=new int[ITEM_COUNT]
    String[]values=new int[ITEM_COUNT]
    
    keys[0]=4; values[0]="Entry1";
    keys[1]=1; values[1]="Entry2";
    keys[2]=0; values[2]="Entry3";
    
    int getValueByKey(int key) {
        for(int i=0;i<ITEM_COUNT;i++)
            if(keys[i]==key) return values[i];
        return -1; // No such key
    }
    x=getValueByIndex(1); // "Entry2"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-09
      • 2014-04-18
      相关资源
      最近更新 更多