user7124275
user7124275

Reputation: 41

Printing rows and columns in a 2D Array?

I'm having issues printing my 2d array... My ultimate goal is to have an output that looks like this:

0   0   0   0   0   
0   0   0   0   0   
0   0   0   0   0   
0   0   0   0   0   

19  36  45  44  47  
64  48  47  74  99  
55  7   48  12  21  
78  19  95  61  11

The first set of numbers prints out fine but I keep getting an error message after trying to print the second set along with it. What should I change in my code?

/*
 * To change this license header, choose License Headers in Project
 Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
 package array2d;

/**
*
 * @author stevebrowning
 */
public class Array2d 
{

public static void main(String[] args)
{
         int [][] array2d = new int [4][5];

         array2d [0][0] = 0;
         array2d [0][1] = 0;
         array2d [0][2] = 0;
         array2d [0][3] = 0;
         array2d [0][4] = 0;

         array2d [1][0] = 0;
         array2d [1][1] = 0;
         array2d [1][2] = 0;
         array2d [1][3] = 0;
         array2d [1][4] = 0;

         array2d [2][0] = 0;
         array2d [2][1] = 0;
         array2d [2][2] = 0;
         array2d [2][3] = 0;
         array2d [2][4] = 0;

         array2d [3][0] = 0;
         array2d [3][1] = 0;
         array2d [3][2] = 0;
         array2d [3][3] = 0;
         array2d [3][4] = 0;

         int rows = 4;
         int columns = 5;
         int m, n;

         for ( m = 0; m < rows; m++) {
             for ( n = 0; n < columns; n++) {
                 System.out.print( array2d[m][n] + " ");
             }
             System.out.println( "")
             }
         }
          int [][] array2d = new int [][] {{19, 36, 45, 44, 47},
                                           {64, 48, 47, 74, 99},
                                           {55, 7, 48, 12, 21},
                                           {78, 19, 95, 61, 11}};
          for ( int i = 0; i < array2d.length; i++ )
            System.out.println( array2d[i] );
             }

Upvotes: 0

Views: 213

Answers (4)

Kashyap Asrc Addepalli
Kashyap Asrc Addepalli

Reputation: 135

If you initialize any element in the array, then all the elements in the array will be initialize to zero. so no need of loops for the above code.

   int array2d[][]=new int[4][4];

            array2d[0][0]=0;

            //to display the array2d
            for(int x[]:array2d){
                for(int y:x)
                         System.out.print(y+" ");
                System.out.println();
            }

replace the above code with this.

array2d = new int [][] {{19, 36, 45, 44, 47},
                        {64, 48, 47, 74, 99},
                        {55, 7, 48, 12, 21},
                        {78, 19, 95, 61, 11}};
for(int x[]:array2d){
                    for(int y:x)
                             System.out.print(y+" ");
                    System.out.println();
                }

Upvotes: 1

GhostCat
GhostCat

Reputation: 140417

In your first example, you have two loops; one for rows, one for columns.

In your second example, there is only one loop.

Why do you expect the same things to happen when you do things differently?

You see, the thing about programming is: details matter. You don't write down some code, you think carefully and write down the code that is required.

Finally: you should not hardcode the dimensions of your arrays - in this sense your second loop is better because it uses array.length instead of a manually assigned value.

Upvotes: 3

Devendra Lattu
Devendra Lattu

Reputation: 2802

Your first block works fine which means you need to follow the same to your next block.
Replace the for loop as follows:

for (m = 0; m < array2d.length; m++) {
    for (n = 0; n < array2d[m].length; n++) {
        System.out.print( array2d[m][n] + " ");
     }
     System.out.println( "");
}

Upvotes: 0

sbowde4
sbowde4

Reputation: 777

Why aren't you printing the second one like the first one?

 for ( i = 0; i < rows; i++) {
             for ( j = 0; j < columns; j++) {
                 System.out.print( array2d[i][j] + " ");
             }
             System.out.println();//no need for quotes in a line break

             }
         }

Upvotes: 1

Related Questions