i don't have idea how search this:
random generator = new random(); map<integer, arraylist> mapofprevop = new hashmap<>(); arraylist<integer> listprev = new arraylist<>(); listprev = mapofprevop.get(operacja); system.out.println(listprev); // show [] int rnd = generator.nextint(op_cnt) + 1; listprev.add(rnd); system.out.println(mapofprevop.get(operacja)); // show value of listprev
why second system.out print me on screen value of listprev?
it shouldn't still print [] ?
listprev = mapofprevop.get(operacja);
this line works different expect?
this suggest @ first system.out.println
invocation list empty.
if here https://docs.oracle.com/javase/7/docs/api/java/util/abstractcollection.html#tostring%28%29. can see tostring method list returns elements between square brackets. [] empty list.
at second call have added element why see it. need bare in mind in java pass objects reference meaning listprev
references same list 1 contained in map.
if want value, suggest change
listprev = mapofprevop.get(operacja);
to
listprev.addall(mapofprevop.get(operacja));
this add of elements mapofprevop.get(operacja)
listprev without subsequent operations affecting map seems want.
also, map<integer, arraylist> mapofprevop = new hashmap<>();
better use interface types in delcarations have map. consider switching arraylist
list
.
the object use self can still arraylist, this:
map mapofprevop = new hashmap<>(); list listprev = new arraylist<>();
this means if wanted change linkedlist, change in 1 place rather 3. note exception of arrays.aslist lists lists can resized.
Comments
Post a Comment