Well this works. I've made some changes using Eclispe. The problem may be the size of the Hashtable, which might mean the variables were not byte aligned in memory.The key value pairs would then be store in memory in a non sequiential order.
// added <String>
Vector<String> vRow2 = new Vector<String>();
Vector<String> vFruit = new Vector<String>();
Hashtable<String, Vector<String>> htItem = new Hashtable<String, Vector<String>>();
vRow2.addElement("apple");
vRow2.addElement("banana");
vFruit.addAll(vRow2); //changed to addAll()
htItem.put("0", vFruit);
//the above line of code makes everything work
//I don't understand why tho
htItem.put("1", vFruit);
htItem.put("2", vFruit);
htItem.put("3", vFruit);
htItem.put("4", vFruit);
htItem.put("5", vFruit);
htItem.put("6", vFruit); // if htItem put only up to 6 it show correct order result 6,5,4,3,2,1
htItem.put("7", vFruit); // if htItem put up to 7 it show incorrect order result 6,5,4,3,2,1,7
htItem.put("8", vFruit); // if htItem put up to 8 it show result 6,5,4,3,2,1,8,7
htItem.put("9", vFruit); // if htItem put up to 9 it show correct order 9,8,7,6,5,4,3,2,1
htItem.put("10", vFruit); // if htItem put up to 10 it show incorrect order result 9,8,6,5,4,3,2,10,1
//loop added to list each key
int i = 0;
for (i=0; i < htItem.size(); i++){
//modified to print results
String res = String.valueOf(i);
System.err.println("htItem==="+res+"\t"+htItem.get(res));
}
Output in Eclipse console:
htItem===0 [apple, banana]
htItem===1 [apple, banana]
htItem===2 [apple, banana]
htItem===3 [apple, banana]
htItem===4 [apple, banana]
htItem===5 [apple, banana]
htItem===6 [apple, banana]
htItem===7 [apple, banana]
htItem===8 [apple, banana]
htItem===9 [apple, banana]
htItem===10 [apple, banana]
Output without key "0":
htItem===0 null
htItem===1 [apple, banana]
htItem===2 [apple, banana]
htItem===3 [apple, banana]
htItem===4 [apple, banana]
htItem===5 [apple, banana]
htItem===6 [apple, banana]
htItem===7 [apple, banana]
htItem===8 [apple, banana]
htItem===9 [apple, banana]
To reverse output change loop to:
int i = htItem.size()-1;
while (i >= 1){
String res = String.valueOf(i);
System.err.println("htItem==="+res+"\t"+htItem.get(res));
i--;
}
Eclipse output:
htItem===10 [apple, banana]
htItem===9 [apple, banana]
htItem===8 [apple, banana]
htItem===7 [apple, banana]
htItem===6 [apple, banana]
htItem===5 [apple, banana]
htItem===4 [apple, banana]
htItem===3 [apple, banana]
htItem===2 [apple, banana]
htItem===1 [apple, banana]
After editing for reformatted output:
int i = htItem.size()-1;
System.err.print("htItem==={");
while (i >= 1){
String res = String.valueOf(i);
System.err.print(res+"=["+htItem.get(res)+"], ");
i--;
if (i == 1){
res = String.valueOf(i);
System.err.print(res+"=["+htItem.get(res)+"]");
System.err.println("}");
return;
}
}
Eclipse output:
htItem==={10=[[apple, banana]], 9=[[apple, banana]], 8=[[apple, banana]], 7=[[apple, banana]], 6=[[apple, banana]], 5=[[apple, banana]], 4=[[apple, banana]], 3=[[apple, banana]], 2=[[apple, banana]], 1=[[apple,banana]]}