Jump to content

Iteration through 2-dimensional array

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
Zael

Zael

    Newbie

  • Members
  • PipPip
  • 13 posts
Hello pals ;)

I recently started to work with Java. As you already know, it's very similar to some other programs like C so I didn't start as a total newbie. Anyway there's always a first time to ask for help. Here is the thing:

My quest starts with creating a two-dimensional field, a filling it with some integer numbers. It should look like this:
0 0 0 0 0
0 1 2 3 4
0 2 4 6 8
0 3 6 9 12
0 4 8 12 16

int[][] matrix;

matrix = new int[5][5];

I already filled the matrix using inner and outer "for" loop. The integers are IN. There's no need for me to paste that code. My question is. How to print(onscreen) whole array using the standard 'for' iteration( format: (for(int item:array))

This is the way teacher said it has to be done. I tried and got nowhere. The construction stated above can only be used to iterate through simple, 1-dimensional array. Here's the example:
public static void main(String[] args){

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

          for (int item : numbers) {

            System.out.println("Count is: " + item);

          }

     }

What's a solution for 2 dimensional field?

p.s. I can post more code and progress if necessary. Thanks in advance :)
Regards from Croatia.

#2
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
A 2-dimensional array is really just an array of arrays.

The thing with the for each loop is it's not very easy to understand. I couldn't get it to work with the 2d array. I prefer to use an old fashioned for loop with a counter for arrays.

So you get:


for (int i=0;i<5;i++) {

     for (int x=0;x<5;x++) {

           System.out.print(array[i][x] + " ");

     } 

     System.out.println();

}


The outer loop executes once for each row in the matrix. So in your matrix there is 5 rows so it executes for 5 runs. Note that the counter starts at 0 and goes to 4 since array indices start at 0. Then the inner loop executes once for each element in the row.

So your output would be:

0 0 0 0 0
0 1 2 3 4
0 2 4 6 8
0 3 6 9 12
0 4 8 12 16


Just as you where expecting. The foreach loop is ugly. Try to avoid using it. :)

2-D Arrays In Java

Hope that helps.

#3
Zael

Zael

    Newbie

  • Members
  • PipPip
  • 13 posts
I pretty much thought and realized the same. Glad to know I was right. I'll show him tomorrow the method I already used to fill the array (and print it meanwhile as you suggested).
Thanks for a quick reply, live long ;)

#4
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
If you figure out how to print a 2d array using for each I would be grateful if you would tell me. :) I did a brief google search and didn't find much useful on the subject.