Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Problem with adding to a list in a loop

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Problem with adding to a list in a loop

    Hi all,

    I wrote a code to check if an array is not in a list, then it should add it to another list. I used a linked list for this. But my problem is the program always add multiple copies of the current array and remove what's inside in the list before: My code is as follows:
    import java.util.Arrays;
    import java.util.LinkedList;
    import java.util.List;
     
    public class Trial {
     
    	/**
    	 * @param args
    	 */
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
     
    		List<int[]> G = new LinkedList<int []>(); 
    		List<int []> New = new LinkedList<int []>(); 
     
    		int [] f = new int [2];
     
    		for(int i=0; i<2; i++){
    			for(int j=0; j<2; j++){ 
    				f[0] = i;
    				f[1] = j;
    				//System.out.println("f is "+Arrays.toString(f));
    				if(!(G.contains(f))){
    					System.out.println("current f is "+Arrays.toString(f));  // I print here in       order to see what is f
    					New.add(f);
    					System.out.println("content of the list New");
    					// I print the list New to see its contents
    					for(int k=0; k<New.size(); k++){
    						System.out.println(Arrays.toString(New.get(k)));
    					}
    					System.out.println("finished printing the list New");
    				}
    			}
    		}
     
    	}
     
    }
    And this is the result I got after running:
    current f is [0, 0]
    content of the list New
    [0, 0]
    finished printing the list New
    current f is [0, 1]
    content of the list New
    [0, 1]
    [0, 1]
    finished printing the list New
    current f is [1, 0]
    content of the list New
    [1, 0]
    [1, 0]
    [1, 0]
    finished printing the list New
    current f is [1, 1]
    content of the list New
    [1, 1]
    [1, 1]
    [1, 1]
    [1, 1]
    finished printing the list New

    Please help!!!!
    Last edited by copeg; November 30th, 2010 at 10:08 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Problem with adding to a list in a loop

    When posting code, make sure you use the code tags to preserve highlighting. Otherwise nobody will want to read it.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Problem with adding to a list in a loop

    As mentioned, for future reference please flank your code with the [highlight=java][/highlight] tags. For now I have edited your post to reflect this.

    Your code changes the reference to the array, so any changes to said reference will be reflected in the references in the List. To add new values, you should create new references (eg place the int [] f = new int [2]; inside the double for loop). In general, its good practice to give variables meaningful names and don't even come close to conflicting with standard java statements (eg 'New').

Similar Threads

  1. Adding to Array from JavaSpace problem
    By rtumatt in forum Collections and Generics
    Replies: 2
    Last Post: September 15th, 2011, 07:11 AM
  2. Adding array elements inside a loop
    By Scotty in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 28th, 2010, 09:48 PM
  3. [SOLVED] Problem with a tutorial program(Adding the answer of two squared numbers together)
    By Melawe in forum What's Wrong With My Code?
    Replies: 20
    Last Post: April 7th, 2010, 09:03 AM
  4. Adding to Array from JavaSpace problem
    By rtumatt in forum Collections and Generics
    Replies: 3
    Last Post: January 5th, 2010, 07:19 PM
  5. Replies: 1
    Last Post: November 22nd, 2008, 01:32 PM