Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Iteration through 2-dimensional array

array

  • Please log in to reply
5 replies to this topic

#1 Zael

Zael

    CC Newcomer

  • Just Joined
  • PipPip
  • 13 posts

Posted 19 March 2009 - 03:58 AM

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.
  • 0

#2 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 19 March 2009 - 05:19 AM

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.
  • 1

#3 Zael

Zael

    CC Newcomer

  • Just Joined
  • PipPip
  • 13 posts

Posted 19 March 2009 - 05:28 AM

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 ;)
  • 0

#4 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 19 March 2009 - 05:36 AM

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.
  • 0

#5 PinkeshSharma

PinkeshSharma

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 20 March 2013 - 01:22 AM

String[][] data = { { 'A', 'B', 'C' }, {

'D', 'E', 'F' }, {
'G', 'H', 'I' }, };

 

for(String[] stringArray : data){
    for(String string : stringArray){
      System.out.println(string);
    }
}

  • 1

#6 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 20 March 2013 - 04:52 AM

Just like you were pointed too, the solution is, in this case, two loops.

The idea is one loop per array dimension, so, three dimensional arrays will be worked with three nested loops and so on.


  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download