0

Please advise.. In the following code(Tested. Will work fine)

public class Exp_Test { 
     public static void main (String[] args) {
        ArrayList<Hashtable> objArrlistHshTbl = new ArrayList<Hashtable>();

        Hashtable objHashTable = new Hashtable();

        objHashTable.put("Key1", "Value1");
        objHashTable.put("Key2", "Value2");

        objArrlistHshTbl.add(objHashTable);

        objHashTable.clear();

        objHashTable.put("Key3", "Value3");
        objHashTable.put("Key4", "Value4");

        objArrlistHshTbl.add(objHashTable);
        System.out.println("Hi");//put a breakpoint here to check the values
    }
}

I am expecting to see an arraylist with to Hashtables saved as array elements, first one with keys 'Key1' and 'Key2', Second one with Keys 'Key3' and 'Key4'. I get an array list with two hash tables, but values and keys inside hash table are coming as 'Key3' and 'key4' for both tables. Could someone explain. If it is a reference problem, how do I get two hash tables in to different array elements withouot creating multiple hash tables(I have to run the code inside a for loop to add multiple hash tables.Creating separate hash tables every time does not sound good)

Miquel
  • 15,405
  • 8
  • 54
  • 87
QVSJ
  • 1,165
  • 2
  • 12
  • 22
  • You decided to mark your answer correct instead of two answers posted before yours, both of which correctly pointed out the your error. That is bad manners. – Miserable Variable Jun 20 '12 at 21:56

3 Answers3

3

This line is a big nono

objHashTable.clear();

You have to understand that objHashTable in your case is pointing to an object. When you add it to an array, that array just creates a pointer to the same object. So when you call clear on any of those pointers, it clears the object. This is an essential concept in programming. You need to create a new Hashtable() and add that as the second element.

Also, Microsoft and most of the modern programming world recommends you stay away from hungarian naming convention. Check out some of the discussions on the topic: Hungarian notation in C#

Community
  • 1
  • 1
Milimetric
  • 13,411
  • 4
  • 44
  • 56
2

ArrayList stores objects by references. When you clear objHashTable, objArrlistHshTbl.get(0) is cleared.

You need to create a new HashTable for each index.

Miserable Variable
  • 28,432
  • 15
  • 72
  • 133
0

Solved

public static void main (String[] args) {
    ArrayList<Hashtable> objArrlistHshTbl = new ArrayList<Hashtable>();
    for(int i= 1; i< 3; i++){
              if(i == 1){
              Hashtable objHashTable = new Hashtable();
    objHashTable.put("Key" + i, "Value1");
    objHashTable.put("Key" + i+1, "Value2");
              objArrlistHshTbl.add(objHashTable);
              }else{
                  Hashtable objHashTable = new Hashtable();
                  objHashTable.put("Key" + i, "Value3");
        objHashTable.put("Key" + i+1, "Value4");
                  objArrlistHshTbl.add(objHashTable);
              }

    }


    }
}
QVSJ
  • 1,165
  • 2
  • 12
  • 22
  • 1
    Yes, this is correct. You do not need `objHashTable = null`; it is going out of scope in the next line. – Miserable Variable Jun 15 '12 at 15:16
  • You decided to mark your answer correct instead of two answers posted before yours, both of which pointed out the your error. That is bad manners. – Miserable Variable Jun 20 '12 at 21:55
  • I had voted your answer up, and it was really helpful of you.Please excuse my enthusiasm in having found a solution by myself. I had got the answer before the comments. :) No hard feelings.. – QVSJ Jun 27 '12 at 09:49
  • 1
    That's ok, though looking at timestamps it gives different impression. Anyway, I have a question: you are basically adding three `Hashtable` in `objArrlistHshTbl`. Each has exactlu one element (the second `objHashTable.put` overwrites the first because it is the same key). Is that an error? Also an advice: `objHashTable = null;` is not needed because it is going out of scope in the very next line. – Miserable Variable Jun 27 '12 at 16:41