• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

an Object array

 
Castulo Ruiz
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this an array of linked list. the linked list is an ADT.
When make the array and store the linked list well it does not work. the loop stops there is nothing inside.
not sure what is going on.

 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are calling makeEmpty(). Doesn't that remove everything you have just added?
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The assignment row[i] = temp doesn't create a copy of the List object temp is referring to, it only copies the reference. Afterwards, both row[i] and temp refer to the same object. And after the loop, you have n references in the List plus the extra temp reference, all referring to one single List object. So when you clear temp, you're clearing all List objects - the only one there is.

The solution is easy - create a new copy each time:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic