set JavaME List index
im new with JavaME and need some help.
I wanna use a List-object and need to set the index of the entrys in this list..
I add entrys with the "list.addRecord(..)" function atm. Works great, but how i said, i wanna set the entrys index'..
Example
with the "addReco开发者_运维技巧rd"function:
0 Entry1
1 Entry2
2 Entry3
...
what i need:
4 Entry1
1 Entry2
10 Entry3
...
Is it possible? Thanks.
There are a few ways to do it.
1. Use 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. Use two arrays (or Vectors)
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"
精彩评论