2011-03-31 69 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