linked list of linked lists in java

ok, so i have a data structure that is a linked list of linked lists, both using the java collection "LinkedList"... it is supposed to fucntion as a queue of integer linked lists...

LinkedList llq=new LinkedList();

i add objects on to the queue "llq" by using the foll stmts:

LinkedList llstate = new LinkedList();

Integer xx= new Integer(x)

llstate.addLast(xx);

llq.addLast(llstate);

now, when i want to retreive the first queue of integers from the queue llq, i say:

LinkedList p = (LinkedList)llq.getFirst();

this works fine, and when llq= [ [0],[1]], i do get the desired output of p=[0] upon executing this stmt. and since i only "get" the first item of llq, and not "remove" it, i assume that it is still on the queue llq.

i go on to perform some manipulations on p, removing all its elements till p=[ ]. but now, i want it to go back to containing the first element of llq, ie [0]. so i say again:

p=(LinkedList)llq.getFirst();

however, here lies the problem: even after executing this stmt, p still is an empty list, ie [ ]. i figure there must be some referencing going on, and it may be a problem with that, that is, maybe p is a reference to the first element, and it actually changes the contents of llq when we manipulate the contents of p. but this does not seem to be true. when i output both p and q after this stmt, i get p=[ ](dunno why), and q=[[0],[1]] (as i expected)

i even tried using an entirely diff variable to retreive this [0], the first element on llq, but it simply does not seem to work.

LinkedList r=(LinkedList) llq.getFirst();

this last stmt also gives me an output r=[ ]!!! wats up wit that? seriously, i am getting outputs r(or p)= [ ] when i have JUSTchecked llq=[[0],[1]], AND i have assigned r to contain the first element of llq! i am thoroughly confused and would greatly appreciate if somebody could clear up this mystery for me!!!

Thanx!

[1988 byte] By [vpaia] at [2007-10-1 4:45:46]
# 1

> now, when i want to retreive the first queue of

> integers from the queue llq, i say:

>

> LinkedList p =

> nkedList p = (LinkedList)llq.getFirst();

>

> this works fine, and when llq= [ [0],[1]], i do get

> the desired output of p=[0] upon executing this stmt.

> and since i only "get" the first item of llq, and not

> "remove" it, i assume that it is still on the queue

> llq.

That is correct.

> i go on to perform some manipulations on p, removing

> all its elements till p=[ ]. but now, i want it to go

> back to containing the first element of llq, ie [0].

> so i say again:

> p=(LinkedList)llq.getFirst();

This statement does pretty much nothing. You created a reference to the first list in the queue, then you emptied that list, then you updated your reference to point to the same list, which is still empty.

When you call getFirst(), you get a reference to the first object in the queue - you do not get a reference to a copy of the object. You should create a of copy the list yourself if that's what you want. (I.e., replace LinkedList p = (LinkedList)llq.getFirst(); that you had earlier, with LinkedList p = new LinkedList((LinkedList)llq.getFirst());

mattbuncha at 2007-7-9 5:20:09 > top of Java-index,Administration Tools,Sun Connection...
# 2
hey thanx a bunch! i think i am finally getting it to work... i knew it wud be some sort of a referencing issue, but i hadnt figured out how... now its all clear... thanx a ton n hope i get a good grade on this when n if i am finally done! :)
vpaia at 2007-7-9 5:20:09 > top of Java-index,Administration Tools,Sun Connection...