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: 2 Dimensional Arrays: I cant manage to read/write them.

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    17
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 2 Dimensional Arrays: I cant manage to read/write them.

    Suppose I had an array like this. Its a 2 by 3 array.

    int[] [] array = new int [2] [3] // two dimensional array

    I cant manage to write on to it.

    System.out.println ("Enter first integer of first colum of array");
    array [i] [0] = Integer.parseInt (stdin.readLine ());

    And reading it

    for (int j = 0 ; j <4 ; j++)
    {
    System.out.println (array [1] + array [2]);
    }


    I may be good in math, but when it comes to writing java


  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: 2 Dimensional Arrays: I cant manage to read/write them.

    Split your problem up into smaller pieces, and concentrate on a single piece at a time. Post an SSCCE that demonstrates just that single piece, and we'll go from there.

    Recommended reading: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: 2 Dimensional Arrays: I cant manage to read/write them.

    How much do you understand about what 2 dimensional arrays are?

    You probably know what a 1 dimensional array is. If you had a 1 dimensional array of ints with size 5 and filled with increasing numbers (0-4), we would initialize it with:
    int[] array = new int[5];
    ... (assume we then add 0,1,2,3,4 respectfully) ...
    and it would look like this:
    {0,1,2,3,4}
    There is another way to initialize an array when you know all the index values. That can be done by saying:
    int[] array = new int[]{0,1,2,3,4};
    This line also creates an array that looks like this:
    {0,1,2,3,4}

    Now, a 2 dimensional array is very different than a 1 dimensional array. The easiest way of thinking about 2 dimensional arrays is to think of them as a 1 dimensional array, which holds another 1 dimensional array in each of its indexes.
    So, if we wanted to create a 2 dimensional array of ints with size 2 by 3, we can imagine it as being the same as creating a 1 dimensional array of size 2, where each index holds 1 dimensional int array of size 3. Here is an example of 3 ways to create, and fill, a 2 dimensional array with the same values. Looking at these 3 ways might help you visualize what a 2 dimensional array looks like:
    Way #1:
    int[][] array = new int[2][3];
    array[0][0] = 0;
    array[0][1] = 1;
    array[0][2] = 2;
    array[1][0] = 3;
    array[1][1] = 4;
    array[1][2] = 5;

    Way #2:
    int[][] array = new int[2][3];
    int[] subArray1 = new int[3];
    subArray1[0] = 0;
    subArray1[1] = 1;
    subArray1[2] = 2;
    array[0] = subArray1;
    int[] subArray2 = new int[3];
    subArray2[0] = 3;
    subArray2[1] = 4;
    subArray2[2] = 5;
    array[1] = subArray2;

    Way #3:
    int[][] array = new int[][]{{0,1,2},{3,4,5}};

    Now, if that all makes sense to you, then it should also be clear to you that making the call to: array[0] would return an array of ints, not an int. So in our example, it would return the array: {0,1,2}. However, the call to: array[0][0] would return just an int (0 in our case), since that statement is effectively saying: get the first element in the first array.

    Does any of that help at all?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. How to begin with multi-dimensional arrays
    By Lorelai in forum Java Theory & Questions
    Replies: 1
    Last Post: April 6th, 2012, 06:56 PM
  2. Help with 2 Dimensional Arrays
    By s1mmi in forum Object Oriented Programming
    Replies: 11
    Last Post: February 7th, 2012, 01:43 PM
  3. Two dimensional Arrays
    By masterT in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 19th, 2011, 05:37 PM
  4. Two dimensional arrays - HELP!
    By ecco in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 19th, 2010, 12:32 PM
  5. How to write 2 dimensional array of float numbers to binary file?
    By Ghuynh in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: June 17th, 2010, 04:26 PM