This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes an Object array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "an Object array" Watch "an Object array" New topic

an Object array

Castulo Ruiz

Joined: May 08, 2009
Posts: 12
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

Joined: Oct 13, 2005
Posts: 44559
You are calling makeEmpty(). Doesn't that remove everything you have just added?
Rob Spoor

Joined: Oct 27, 2005
Posts: 20132

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:

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: an Object array
It's not a secret anymore!