0

I'm facing a problem using ArrayList. I'm using an ArrayList with this content :

String, int, HashTable, HashTable

The HashTables are formed with a String key and an int value.

At some point in my program I need to set the value of the two HashTable. If I set ArrayList(2) (so first hashtable) with a HashTable h_gain for example and then I clear h_gain, the value assigned before with the right content is set to null (cause h_gain is now empty).

a_j1.set(2, h_gain);

Hashtable<String, Integer> h_tmp2 = (Hashtable<String, Integer>) a_j1.get(2); 

// Gives the right value - like 1000 for example
System.out.println("TESSSSSSSSSSSSSST " + h_tmp2.get("En plein"));

h_gain.clear();

// Gives me null
h_tmp2 = (Hashtable<String, Integer>) a_j1.get(2);
System.out.println("TESSSSSSSSSSSSSST " + h_tmp2.get("En plein"));

I don't get why it acts like this. If I simply do it with a variable the behavior is more like expected

int val1 = 10
int val2 = val1 // val2 = 10

val1 = 0        // Well, val2 is still = 10

Any help on this?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Gilles
  • 229
  • 1
  • 3
  • 11
  • 1
    Where are `ArrayList`s involved? Primitives and object behave differently because in Java, everything is passed by value. You pass the value of a primitive. You pass the reference value of an object reference. So in the end, `h_gain` and `h_tmp2` (which are terribly named by the way, please use [camelCase](https://en.wikipedia.org/wiki/Camel_case) for variable names) reference the same `Hashtable`. – Turing85 Jan 10 '18 at 15:51
  • When using Objects you cannot just copy/clone them like primitive types as int. For example: `List a = new ArrayList; List b = a;` will only create one List and have two different vairables point at the same List. – OH GOD SPIDERS Jan 10 '18 at 15:52
  • @OHGODSPIDERS there are no pointers in Java ;) those are references. – Turing85 Jan 10 '18 at 15:58
  • I could point out that i never claimed that there are, but this is a comment not a pointer so it cannot point to anything, right? – OH GOD SPIDERS Jan 10 '18 at 16:08
  • @Turing85 Ok didn't know about passing by value or reference, I think I got it. For the naming, I'll change it ;) – Gilles Jan 10 '18 at 16:09

1 Answers1

1

I think I can answer this question for you. You might think that there is another object inside your ArrayList after the insert but that is actually not the truth.

The object h_gain is referenced in your a_j1. By clearing h_gain you also clear the object in a_j1 cause this is only a reference. When you have to clear h_gain I would recommend to clone/copy your object before inserting it into a_j1.

So try this:

a_j1.set(2, h_gain.clone());

Hashtable<String, Integer> h_tmp2 = (Hashtable<String, Integer>) a_j1.get(2); 

System.out.println("TESSSSSSSSSSSSSST " + h_tmp2.get("En plein"));

h_gain.clear();

h_tmp2 = (Hashtable<String, Integer>) a_j1.get(2);
System.out.println("TESSSSSSSSSSSSSST " + h_tmp2.get("En plein"));

The difference is, that Java do not insert a reference of the old object but the reference of the new cloned object which (indeed) is not changed anymore. And that is the reason why you now can change the old value.

HerTesla
  • 66
  • 9
  • 1
    It should be mentioned that [`h_gain.clone()` only creates a shallow copy](https://docs.oracle.com/javase/9/docs/api/java/util/Hashtable.html#clone--). This can lead to confusing behaviour if not taken into consideration. – Turing85 Jan 10 '18 at 16:02
  • @Turing85 Well in fact that solve the first part of the issue. But as you said it seems that the content is not copied. So my question is : how can I clone the structure and the content ? – Gilles Jan 10 '18 at 16:13
  • @Gilles it would be much easier if you describe the actual problem you are trying to solve instead of presenting the solution you deployed. The sounds more and more like an [XY-problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Turing85 Jan 10 '18 at 16:16
  • @Turing85 Well I was trying to explain my problem and I couldn't so I thought it might be a conception problem. I figured out how to deal with my program another way and it works now.. And my variables are all renamed ;) Thank you ! – Gilles Jan 10 '18 at 20:05