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: 2D ARRAY PROBLEM

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 2D ARRAY PROBLEM

    I have a 2D array called box.

    int[][] box = new int [2][3];

    Its has elements as follows:

    box [0][0] = 1;
    box [0][1] = 2;
    box [0][2] = 3;
    box [1][0] = 1;
    box [1][1] = 2;
    box [1][2] = 3;

    I want to end up after coding with elements in the array as follows:
    box [0][0] = 2;
    box [0][1] = 3;
    box [0][2] = 3;
    box [1][0] = 2;
    box [1][1] = 3;
    box [1][2] = 3;
    i.e All elements in each column has had its values
    replaced by values from its immediate right hand column

    I have tried :
    for (int j = 0; j < box.length - 1; j++)
      for (int i = 0; i < box.length - 1; i++)
       box[i][j] = box[i] [j + 1];
    but no joy. Any thoughts please??


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: 2D ARRAY PROBLEM

    Quote Originally Posted by av8 View Post
    but no joy.
    That provides zero information. Presumably your code didn't work. Since there is a gazillion reasons why it didn't work we have no idea what your problem is. If you provide details it will help others help you.

    Hint: a 2D array is simply a 1D array of 1D arrays.
    int[][] matrix = {{1,2,3}, {4,5,6}, {7,8,9}};
    for(int outer = 0; outer < matrix.length; outer++) {
        int[] temp = matrix[outer];
        for(int inner = 0; inner < temp.length; inner++) {
            System.out.print(temp[inner] + " ");
        }
        System.out.println();
    }
    So you can concentrate on 1 dimension instead of 2.

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2D ARRAY PROBLEM

    for (int j = 0; j < 2; j++)
    for (int i = 0; i < 1; i++)
    box[i][j] = box[i] [j + 1];

    The above code is now working correctly. Thanks for commenting Junky.

    kind regards av8

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: 2D ARRAY PROBLEM

    Do not hard code values like that. It will not work for a 2D array of any other size.

Similar Threads

  1. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  2. array problem
    By u-will-neva-no in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 2nd, 2011, 11:36 AM
  3. array problem
    By aizen92 in forum Java Theory & Questions
    Replies: 4
    Last Post: December 18th, 2010, 11:06 AM
  4. Problem with 2d array
    By Anyone in forum Collections and Generics
    Replies: 2
    Last Post: November 14th, 2009, 09:32 PM
  5. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM