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 4 of 4

Thread: Adding array elements inside a loop

  1. #1
    Member Scotty's Avatar
    Join Date
    Oct 2010
    Posts
    60
    My Mood
    Scared
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Adding array elements inside a loop

    How can I stop java 'forgetting' the contents of this array when outside the for loop. I cant think of a better way to get this 2d array into a list - it outputs OK in the loop, but not outside.

    for (int i=0; i<multi.length; i++) {
    	for (int j=0; j<multi[i].length; j++) {
    	line[j] = multi[i][j];
    	System.out.print(line[j]);
    	}
    	}


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

    Default Re: Adding array elements inside a loop

    That loop works only inside for loop. You can't do it out-side of the for block.

  3. #3
    Member Scotty's Avatar
    Join Date
    Oct 2010
    Posts
    60
    My Mood
    Scared
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Adding array elements inside a loop

    I understand, that is my problem. Is there any way I can get my 2D-array into a list[]? or make the array available outside the loop?

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

    Default Re: Adding array elements inside a loop

    I'm not sure I understand the question. Both your arrays are defined outside the loops, so do you want your 2d array stored into one large 1d array? Just declare a 1d array of the size of the 2d array, and set the appropriate values in the loop:
    int[] temp = new int[multi.length * multi[0].length];
    for ( int i = 0; i < multi.length; i++ ){
        for ( int j = 0; j < multi[i].length; j++ ){
            temp[i * multi[i].length + j] = multi[i][j];//
        }
    }

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. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM
  3. Adding to Array from JavaSpace problem
    By rtumatt in forum Collections and Generics
    Replies: 3
    Last Post: January 5th, 2010, 07:19 PM
  4. Method Adding elements to an array with certain restrictions
    By Newoor in forum Collections and Generics
    Replies: 1
    Last Post: December 13th, 2009, 11:13 AM
  5. [SOLVED] Array loop problem which returns the difference between the value with fixed value
    By uplink600 in forum Loops & Control Statements
    Replies: 5
    Last Post: May 15th, 2009, 04:31 AM