> I'm having a problem working with 2 linked lists in > problem 3. I create 2 objects of the class "DLList," > call them list1 and list2, and, from looking at the > debugger, no problem with that. The class DLList > includes a "print()" function that prints the list. > If, after creating list1 and list2 I use this > function, however, the 2 lists are printed out > identical. It's like the program can't distinguish > between the 2 lists after they are created, even > though the memory address is different... does anybody > have any idea why this could be happening? I'm really > quite cluseless... :( > > Thank you, enjoy the beautiful weekend! > > --Alessandro > > PS: Here's the code I'm looking at: > > int [] array ={1,3,3,4}; > list1=new DLList (array); > > int [] array2 = {3,4,5,6}; > list2= new DLList (array2); > > list1.print(); > list2.print(); > OUTPUT: > {3,4,5,6} > {3,4,5,6}
My guess is that your DLList has this kind of form: public class DLList { private int[] my_array; public DLList(int[] array) { my_array = array; } // other code ... } The difficulty here is to understand clearly what "my_array = array;" means. If my guess is right, then what you are doing is letting my_array point to array (so my_array just becomes another name for the original array). What you should do here is allocate memory for my_array and then make a real copy of array into my_array (copying all the elements). Hans