简体   繁体   中英

Java Ragged Array, trying to display multiple Arrays

I am having trouble finding out what is the issue with my code, Everything is fine till I get to the Jagged array. I want it to display the code as a jagged array but I dont know

package ajk;

public class Test2 
{

    public static void main(String[] args)
        int[][] twoD = {
            {1, 2},
            {3, 4, 5},
            {6},
            {7, 8, 9},
        };
        printArray(twoD); 
    }
    public static void printArray(int[][] arr) {
        System.out.println("[");

        int r = 4;
        int c = 3;
        int i, j;
    
        for (i=0; i < r; i++ );
            System.out.print("["); 
            for (j=0; j < c; j++ ) {
                System.out.print( arr[i][j]  + " " );
            }
        }
    }                       
}

You have some syntax errors and mistakes:

  • for (i=0; i < r; i++ ); You can not use for loop like this. You have to write your code in the braces

  • Your array's column sizes are different from each other, so you can not determine the column size with one variable. Best way is array[i].length for this situation

  • You can define i and j inside the parantez of the for loop.

If you change the code as below: it will run

public class Test2
{

    public static void main(String[] args) {

    int[][] twoD = {
            {1, 2},
            {3, 4, 5},
            {6},
            {7, 8, 9},
    };
    printArray(twoD);
}
    public static void printArray(int[][] arr) {
        System.out.println("[");

        for (int i=0; i < arr.length; i++ ) {
            System.out.print("[");
            for (int j=0; j < arr[i].length; j++ ) {
                System.out.print( arr[i][j]  + " " );
            }
        }

    }
}

You are making some mistakes in your code

1.Always put the code of for loop in curly braces like this for(){}

2.You are making a general mistake by making the array index out of bounds for that you should use array[i].length to make sure column size are not different from each other

3.your code should look like this so change and try the code below

 public class Test2    
 {

public static void main(String[] args){
int[][] twoD = {
        {1, 2},
        {3, 4, 5},
        {6},
        {7, 8, 9}
};
Test2.printArray(twoD); 
}
public static void printArray(int[][] arr) {
System.out.println("[");

int r = 4;
int c = 3;
int i, j,a=0;

for(i=0; i<r; i++ ){
System.out.print("["); 
for (j=0; j<arr[i].length; j++ ) {
    System.out.print( arr[i][j]  + " " );

}
System.out.println("]");
 }
 }
}

And

I have also added another square bracket for better understanding

It' quite simple, actually. Arrays class have some static methods to print its own elements (ints, doubles, Objects, etc.). Since, here is two dimensional array, in the loop you go through every element of the arr (which is an array of int), and call this method. Arrays.toString(someArr) represents someArr as a String.

for (int row = 0; row < arr.length; row++) {
    int[] arr1D = arr[row];
    System.out.println(Arrays.toString(arr1D));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM