Results 1 to 4 of 4
- 01-06-2012, 08:37 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 4
- Rep Power
- 0
Issues regarding object modification
To practice creating generic methods and generic data structures, i created a method to combine two lists as in the case of how the LinkedList collection functionality works. Basically to test it out, I took two lists of strings, where the first (listA) contains a set of individual words to form the sentence, "Hello World! ", then the next (listB) contains a set of words to form the sentence, "How are you? ". While I could successfully get the lists to combine into a complete two-sentence string, the first list is modified despite how i attempt to make a copy of it to prevent it's modification. I know with assigning primitive data type variables there is a right to left associativity the variable on the right does not change if the variable on the left undergoes subsequent changes in further lines of code. However, this does not seem to be the case with the listA object which is modified when my newly created newList becomes modified in subsequent lines of code. I also tried using the .clone() method on listA (for this, I made the class implement the Cloneable interface), but I got the same exact issue.
Here's the portion of the code in question:
If I printed out the three lists, this is what would show up:Java Code:public List combineList(List<T> listA, List<T> listB){ //My attempt to generate a new list to make a copy of listA so that it would not be modified, epic fail ensues. List<T> newList = new List<T>(); newList = listA; //makes the loop recognize the current node of the list to be added as the first node of the second currNode=listB.firstNode; //Adds each node of the second list one by one while(currNode!=null){ newList.insertAtBack(currNode.data); currNode = currNode.nextNode; } return newList; }
Have I been overlooking some silly basic aspect of computer science this whole time I've been programming that I never had to bother with before?list1: Hello World! How are you?
list 2: How are you?
list 3: Hello World! How are you?Last edited by bigboibrady; 01-06-2012 at 08:56 PM.
- 01-07-2012, 12:06 AM #2
Re: Issues regarding object modification
Is there a question or problem here?
Are you confusing variables that are references to objects and the objects?
- 01-07-2012, 12:22 AM #3
Member
- Join Date
- Jan 2012
- Posts
- 4
- Rep Power
- 0
Re: Issues regarding object modification
I suppose that's the mistake I'm making.
However, what I am trying to do is make a copy of the object, listA, that I can modify via the method in question. I tried implementing the Cloneable interface, and using newList = listA.clone() instead of newList = listA, but I was getting the same exact issue.
What I am trying to do is make a combined list, but retain the original separate lists in addition. The problem I am having is regarding the latter.
- 01-07-2012, 12:40 AM #4
Similar Threads
-
Game modification help
By Battelfield in forum New To JavaReplies: 2Last Post: 12-06-2011, 09:34 PM -
concurrent modification
By mharsijamel in forum New To JavaReplies: 1Last Post: 11-06-2011, 02:59 PM -
Empty Object Issues
By BeeGee in forum New To JavaReplies: 2Last Post: 07-08-2011, 08:24 PM -
Modification in XML file
By anurag_1226 in forum Java SoftwareReplies: 1Last Post: 04-10-2010, 06:53 PM -
Object creation and memory issues
By bugger in forum New To JavaReplies: 11Last Post: 11-29-2007, 12:56 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks