tutorguru
tutorguru

Reputation: 31

Traverse 2d array for variable size column wise

I have 2d array like this

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

I want it to be printed it like

1,4,6,1,7,2,5,4,8,3,3,9 

I understand that we have to traverse column wise, but the size of columns are not fixed. So how can I achieve the above ?

I have tried the below, but it gives IndexArrayOutOfBoundException

for(int i = 0; i < v.length; i++){
      for(int j = 0; j< v[i].length; j++) {
    
          System.out.println(v[j][i]);
      
      }
    }

How to achieve this ?

Upvotes: 1

Views: 1547

Answers (5)

user17233545
user17233545

Reputation:

Try this.

public static void main(String[] args) {
    int[][] v = {{1, 2, 3}, {4, 5}, {6, 4, 3}, {1}, {7, 8, 9}};
    boolean moreColumns = true;
    String sep = "";
    for (int c = 0, rows = v.length; moreColumns; ++c) {
        moreColumns = false;
        for (int r = 0; r < rows; ++r) {
            if (c < v[r].length) {
                System.out.print(sep + v[r][c]);
                sep = ", ";
                moreColumns = true;
            }
        }
    }
}

output:

1, 4, 6, 1, 7, 2, 5, 4, 8, 3, 3, 9

Upvotes: 0

Goet
Goet

Reputation: 57

Here's my proposal based on the idea that if you know the length of the longest array, everything get simpler : )

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

int maxLength = 0;
for(int i = 0; i < v.length; i++){
    maxLength = v[i].length > maxLength ? v[i].length : maxLength;
}

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

Upvotes: 0

Reed Thorngag
Reed Thorngag

Reputation: 141

I think the code is pretty self explanatory, but if you do have questions, feel free to ask them

public static void printColumns(int[][] array) {
    int largest = 0;
    for (int[] i:array) {
        if (i.length>largest) { // find the largest element in array
            largest = i.length;
        }
    }
    for (int i=0;i<largest;i++) {
        for (int[] intArray : array) {
            if (i < intArray.length) {  // check that you won't get an IndexOutOfBounds exception
                System.out.print(intArray[i]+",");
            }
        }
    }
    System.out.println("\b");  // removes last "," printed
}

with input of

{{1,2,3}, {4,5}, {6,4,3,}, {1}, {7,8,9}}

outputs:

1,4,6,1,7,2,5,4,8,3,3,9 

Upvotes: 0

sprinter
sprinter

Reputation: 27966

here is a stream based solution:

    IntStream.iterate(0, i -> i + 1)
            .mapToObj(i -> IntStream.range(0, v.length)
                    .flatMap(j -> i < v[j].length ? IntStream.of(v[j][i]) : IntStream.empty())
                    .boxed().toList())
            .takeWhile(l -> !l.isEmpty())
            .flatMap(List::stream)

Upvotes: 0

Vojin Purić
Vojin Purić

Reputation: 2376

Here is a simple solution that works, if you need clarification let me know

public static void test(int[][] arr) {
    int i = 0;
    boolean found = true;
    while(found){
        found = false;
        for(int j = 0; j < arr.length; j++){
            if(arr[j].length-1 >= i){
                System.out.println(arr[j][i]);
                found = true;
            }
        }
        i++;
    }
}

Upvotes: 1

Related Questions