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

Thread: 2d Array

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 2d Array

    42 0 33 67 145 41 23 7 8 255
    234 0 0 157 0 0 0 0 1 5
    167 0 143 0 0 0 8 1 7 255
    178 2 41 0 0 0 255 1 6 6
    189 3 1 0 0 0 0 0 5 8
    0 0 0 93 78 1 0 157 63 143
    53 0 0 0 0 2 255 4 0 165
    156 0 0 0 5 2 143 0 0 0
    69 0 0 0 74 63 35 0 0 0
    73 34 3 0 0 0 234 0 0 0


    You need to create a class, named MicroArray, which contains the following methods

    - Print out the input 2D-array as an image on the console screen.
    Replace 0 by {space}.
    Replace values between 1 and 99 by . {one dot}.
    Replace values between 100 and 199 by .. {two dots}.
    Replace values between 200 and 254 by {three dots}.
    Replace 255s by x.
    - Remember to check if the dimensions of input array are valid numbers.
    - Make use of method printf properly to print out the image nicely.

    Need help getting this started kind of confused on 2 dimensional arrays?


  2. #2
    Junior Member
    Join Date
    Oct 2009
    Posts
    26
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default Re: 2d Array

    Well, once you have a basic understanding of 2d arrays, the rest of the program is just a bunch of if/else if statements and nested for loops.

    As for a starting point, I'd recommend finding a good tutorial over 2d arrays to give you a basic understanding of them. I really can't think of any off the top of my head, but a quick Google search may bring you to several which can help.

    Basically, you just need to know that a 2d array is just a regular array with multiple rows. To create one, you just add another [] such as:

    int[][] anArray = new int[[I]number of rows here[/I]][[I]number of columns here[/I]]

    As far as putting values inside the array, there are a couple ways you can do that. You can use a nested for loop, for example if you wanted all values to be 0:

    for (int row = 0; row < anArray.length; row ++)
           for (int col = 0; col < anArray[row].length; col++)
                  anArray[row][col] = 0;

    Or if you know the exact values and where they belong, which seems like you do in this case, you can do something like this:

    int[][] anArray = {{1, 2, 3},
                              {4, 5, 6},
                              {7, 8, 9}}

    I hope this helps you get started!

Similar Threads

  1. Storing an array into an array
    By vluong in forum Collections and Generics
    Replies: 4
    Last Post: September 22nd, 2009, 02:14 PM