Results 1 to 4 of 4
  1. #1
    bigboibrady is offline Member
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    Default 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:
    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;
    }
    If I printed out the three lists, this is what would show up:

    list1: Hello World! How are you?
    list 2: How are you?
    list 3: Hello World! How are you?
    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?
    Last edited by bigboibrady; 01-06-2012 at 08:56 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,570
    Rep Power
    25

    Default Re: Issues regarding object modification

    Is there a question or problem here?

    Are you confusing variables that are references to objects and the objects?

  3. #3
    bigboibrady is offline Member
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    Default Re: Issues regarding object modification

    Quote Originally Posted by Norm View Post
    Are you confusing variables that are references to objects and the objects?
    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.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,570
    Rep Power
    25

    Default Re: Issues regarding object modification

    Can you post the code that shows the problem?
    Remember that a list contains references to other objects.

Similar Threads

  1. Game modification help
    By Battelfield in forum New To Java
    Replies: 2
    Last Post: 12-06-2011, 09:34 PM
  2. concurrent modification
    By mharsijamel in forum New To Java
    Replies: 1
    Last Post: 11-06-2011, 02:59 PM
  3. Empty Object Issues
    By BeeGee in forum New To Java
    Replies: 2
    Last Post: 07-08-2011, 08:24 PM
  4. Modification in XML file
    By anurag_1226 in forum Java Software
    Replies: 1
    Last Post: 04-10-2010, 06:53 PM
  5. Object creation and memory issues
    By bugger in forum New To Java
    Replies: 11
    Last Post: 11-29-2007, 12:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •