Results 1 to 4 of 4
Thread: LinkedList and archives
- 02-04-2011, 10:37 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 13
- Rep Power
- 0
LinkedList and archives
Hi everybody, this is my first post. Thank you very much for this forum, it can be very helpfull!
I am a very beginner of java, and while I was typing some raw and simple code, just to now how linkedlist and archives works, I came upon a very strange behaviour.
In the following I'll post my post:
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
public class Liste {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
LinkedList<Integer> list = new LinkedList<Integer>();
Stack<LinkedList<Integer>> archivioMio = new Stack<LinkedList<Integer>>();
for (int i=0; i<5; i++) {
System.out.print("digita numero: ");
list.add(new Integer(sc.nextInt()));
}
System.out.println("numero di elementi della lista: "+list.size());
System.out.println("Dettaglio degli elementi in lista:");
for (int i=0; i<list.size(); i++)
System.out.println(" "+i + ": "+list.get(i));
archivioMio.push(list);
System.out.println("list: " + list);
System.out.println("archivioMio: " + archivioMio);
list = new LinkedList<Integer>();
System.out.println("list_svuotata: " + list);
list = archivioMio.peek();
System.out.println("list_peek: " + list);
list = new LinkedList<Integer>();
System.out.println("list_svuotata. " + list);
list = archivioMio.peek();
System.out.println("list_dopo peek: " + list);
System.out.println("archivioMio: " + archivioMio );
Scanner sc2 = new Scanner(System.in);
for (int i=0; i<5; i++) {
System.out.print("digita numero: ");
list.add(new Integer(sc2.nextInt()));
}
System.out.println("list_2: " + list);
System.out.println("archivioMio: " + archivioMio );
}
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+++-+-+-
What happens is that I put some numbers from the keyboard, I put those numbers in a list, then I put the list in an archive "archivioMIo".
Then I do some stuff to learn how they works.
Then I type more numbers from keyboard. I put those numbers in the same list of before, BUT THE STRANGE THINGS IS that also the archive "archivioMio" is updated! I see it in the last row.
can someone explain me why and HOW to prevent this unwanted mechanism?
Thank you very much for you attention and for your answers!
:)
- 02-04-2011, 10:55 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Both of those variables point to the same LinkedList object. This is the way all objects work, the variable is just a reference to the actual object.
Both a and b point to the same list, so whether you do a.add() or b.add(), both variables will be updated, since they reference the same List. The same behaviour can be noticed with arrays:Java Code:List a = new LinkedList(); List b = a;
This is a nice and short compilable example of this principle. If you want to store the current version of the list without it being modified by subsequent code, use the clone() method. Here's another example:Java Code:public class RefTest { public static void main(String[] args) { int[] a = {1,2,3}; int[] b = a; b[0] = 5; System.out.println(a[0] + " " + b[0]); } }
This is how you use clone to prevent what's happening in your example.Java Code:import java.util.LinkedList; public class CloneTest { public static void main(String[] args) { LinkedList<String> a = new LinkedList<String>(); a.add("bla"); LinkedList b = a; b.add("hello"); //both lists are updated b = a.clone(); a.add("another"); //only a is updated System.out.println("List a: "); for(String s: a) System.out.print(s+" "); System.out.println("List b: "); for(String s: b) System.out.print(s+" "); } }Last edited by m00nchile; 02-04-2011 at 11:02 AM.
Ever seen a dog chase its tail? Now that's an infinite loop.
- 02-04-2011, 02:36 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 13
- Rep Power
- 0
Thank you very much for the quick answer! I 've just try your suggestion, with no success though. But it doesn't work, I have on a side a LinkedList of integers, and ont he other side an archive. I don't know if it exists a way to match the two things with clone.
Any idea? Thank you very much!
Veronica
- 02-04-2011, 03:54 PM #4
Member
- Join Date
- Feb 2011
- Posts
- 13
- Rep Power
- 0
What i've done
Ok guys, here you go with some crazy code: it does what I want but I do not know if it is conceptually correct :
Here is the goal:Java Code:public class ListaArchivio { @SuppressWarnings("unchecked") public static void main (String[] args) { /** * FIRST TIME */ Scanner sc = new Scanner(System.in); LinkedList<Integer> list = new LinkedList<Integer>(); Stack<LinkedList<Integer>> archivioMio = new Stack<LinkedList<Integer>>(); for (int i=0; i<5; i++) { System.out.print("digita numero: "); list.add(new Integer(sc.nextInt())); } LinkedList<Integer> listone= (LinkedList<Integer>) list.clone(); archivioMio.push(listone); System.out.println("list_peek: " + list); System.out.println("listone: " + listone); System.out.println("archivio: " + archivioMio); /** * SECOND TIME */ Scanner sc2 = new Scanner(System.in); for (int i=0; i<5; i++) { System.out.print("digita numero: "); list.add(new Integer(sc2.nextInt())); } System.out.println("list_2: " + list); System.out.println("archivioMio: " + archivioMio ); System.out.println("listone: " + listone); list = new LinkedList<Integer>(); listone = archivioMio.peek(); [COLOR="Red"]list = (LinkedList<Integer>) listone.clone();[/COLOR] /** * THIRD TIME */ Scanner sc3 = new Scanner(System.in); for (int i=0; i<5; i++) { System.out.print("digita numero: "); list.add(new Integer(sc3.nextInt())); } System.out.println("list_3: " + list); System.out.println("archivioMio: " + archivioMio ); System.out.println("listone: " + listone);
put the content of the LinkedList "list" in the stack "archivioMio".
The stack is needed to save from time to time the content of the list.
"List" can be updated, but the relative archive has not to be changed everytime "list" is updated.
So what I 've done with yor help is to put another list, "listone", which is a clone of list.
It works fine.
Then in the last case I want "list" to restore what archivioMio has inside.
If I simply put list = archivioMio.peek(), then when I update list will update also archivioMio.
So the solution came across the red line of the code: which means that the original list "list" is now a clone of its clone "listone".
In that way the program does what I want to see, but I don't know if it is conceptually "right".
Hope my explanation is clear enough.
Now is my question: what do you think about this code? is it conceptually right or can it be done in another more efficient way?
Thank you very much for your answers!
Similar Threads
-
LinkedList
By [RaIdEn] in forum New To JavaReplies: 7Last Post: 10-13-2009, 12:59 AM -
creating archives
By almaz__ in forum Java AppletsReplies: 2Last Post: 09-27-2009, 04:30 PM -
LinkedList help
By jigglywiggly in forum New To JavaReplies: 6Last Post: 09-19-2009, 07:24 AM -
how to use LinkedList
By fred in forum Advanced JavaReplies: 1Last Post: 07-24-2007, 01:52 AM -
Problem with LinkedList
By Eric in forum Advanced JavaReplies: 1Last Post: 07-05-2007, 06:08 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks