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

Thread: Problems getting value of index in 2D array operation

  1. #1
    Junior Member Flcha's Avatar
    Join Date
    Jul 2014
    Location
    Portugal
    Posts
    10
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problems getting value of index in 2D array operation

    Before starting to tell the problem with my method, I want to greet everyone in Java community

    This time, I have a problem with an 2D array.
    Arr is of integer type with dimensions of [4][4].
    Let me display the code...
    ...
    public static void main(String args[]) {
      int[][]arr = new int[4][4];
        int size=0;
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            arr[i][j] = size * j + i;
    IO.outputln("result is: "+arr[i]+arr[j]);
        }
    }
    }

    I would like to know the value stored in arr[1][3] after executing the code above.
    I can't see the value in the console. What is missing ?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Problems getting value of index in 2D array operation

    What result are you getting? Copy and post the console from a sample run.

    You've used the 'size' variable before (I think), and you shouldn't be. That's what you're missing in this case. Post your corrected code, and then I'll show you a better way.

  3. #3
    Junior Member Flcha's Avatar
    Join Date
    Jul 2014
    Location
    Portugal
    Posts
    10
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems getting value of index in 2D array operation

    Hi Greg.
    I can't tell you the result of the operation, because the terminal window doesn't show me anything.
    About the variable size, I don't know. I just copied this small line of code from my teacher example to create a better understanding program with an output result ^^

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Problems getting value of index in 2D array operation

    That you're seeing nothing in the terminal window is the result, and it's significant.

    Trace the code - keep track of the variable values with pencil and paper if you need to - and determine what will happen at the for loops with size set to 0.

  5. #5
    Junior Member Flcha's Avatar
    Join Date
    Jul 2014
    Location
    Portugal
    Posts
    10
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problems getting value of index in 2D array operation

    I used the breakpoint and when it arrives to the step of the "For loop" it jumps to the end of the program...
    If I change size=4. the terminal window will display 16 results

    Now I can tell what is the value of arr[1][3]

    Thanks for the tip once again. the variable is the problem
    Last edited by Flcha; July 25th, 2014 at 05:56 PM.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Problems getting value of index in 2D array operation

    Glad you figured it out. As I said earlier, the code below demonstrates a more appropriate way to write for() loops to iterate arrays.
    // a method to demonstrate the declaration and initialization
    // of a 2D array with 4 rows and 4 columns. the first element of
    // each row of the array is initialized to the row number, and
    // each element in the row thereafter increases by one
    public static void arrayMethod()
    {
        // declare a 2D array with 4 rows and 4 columns
        int[][] arr = new int[4][4];
     
        // initialize the array
        // for each row, i: arr.length = number of rows
        for (int i = 0 ; i < arr.length ; i++)
        {
            // for each column, j: arr[i].length = the number of
            // elements or columns in the row i
            for (int j = 0 ; j < arr[i].length ; j++)
            {
                arr[i][j] = j + i;
            }
        }
     
        // display the completed array
        System.out.println( Arrays.deepToString( arr ) );
     
    } // end method arrayMethod()

Similar Threads

  1. How to find the array index of the lowest value in the array?
    By namenamename in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 29th, 2013, 06:57 AM
  2. help with index in array
    By lanya1 in forum Java Theory & Questions
    Replies: 5
    Last Post: July 6th, 2013, 08:58 PM
  3. [SOLVED] Merge two 1D array's into a new 1D array. Trouble appending last index
    By norske_lab in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 30th, 2012, 12:57 PM
  4. Problems with if else if statement operation
    By Farmer in forum Loops & Control Statements
    Replies: 4
    Last Post: May 29th, 2012, 06:43 AM
  5. index of array
    By nasi in forum Java Theory & Questions
    Replies: 1
    Last Post: April 12th, 2010, 02:39 AM