2011-03-31 38 views
0

即时通讯使用JavaME,需要一些帮助。设置JavaME列表索引

我想用一个List对象,需要设置entrys的指数在此列表中..

我添加entrys与“list.addRecord(..)”功能的ATM。 伟大的作品,但我怎么说,我想设置entrys指数” ..

用 “的addRecord” 功能:

0 Entry1 
1 Entry2 
2 Entry3 
... 

我需要什么:

4 Entry1 
1 Entry2 
10 Entry3 
... 

可能吗? 谢谢。

回答

0

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

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" 
+0

完美!非常感谢! – Prexx 2011-04-02 21:59:21