Home > database >  How to create an new one dimensional array with elements of a 2d array and return it?
How to create an new one dimensional array with elements of a 2d array and return it?

Time:10-08

I need tp create a new array with the elements of a 2d array in java. However, it only adds the last number of the 2d array which results in an array of the same value.

public class MyClass {

public static int[] collect(int[][] array) {
    int[] nA = new int[10];

    for (int i = 0; i < 7; i  ) {

        for (int[] y : array) {

            for (int x : y) {

                nA[i] = x;

            }

        }
    }
    
    return nA;
}

public static void main(String args[]){

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

    int[] z = collect(array);

    for (int x : z) {
        System.out.print(x    " ");
    }}
}

CodePudding user response:

public class MyClass {

public static int[] collect(int[][] array) {
    int[] nA = new int[10];
    int k=0;
    for (int i = 0; i < array.length; i  ) {
        
        for (int j=0; j< array[i].length;j  ) {
                nA[k] = array[i][j];
                k  ;

        }
    }
    
    return nA;
}

public static void main(String args[]){

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

    int[] z = collect(array);

    for (int x : z) {
        System.out.print(x    " ");
    }}
}

Change your code as above. However to work this solution you have know the size of new array beforehand. No need of 3 for loops. Variable k use to identify the new array current index.

CodePudding user response:

You have an outer loop for (int i = 0; i < 7; i ) that is causing the issues.

The value of "i" does not change and the whole array is traversed '7' times and the last element is persisted at the index specified by 'i'. And the whole thing repeats for "i 1". Ultimately what is printed is the last element of the last sub-array multiple times. That is not what you want.

Please look at the following code. Note, we also get the length of the array dynamically from the argument (never hardcode for you would like your method to be suitable for all sizes).

public static int[] collect(int[][] array) {

    // calculate length
    int length = 0;
    for (int a[] : array) {
        length  = a.length;
    }

    int[] nA = new int[length];

    // for (int i = 0; i < 7; i  ) {
        int i=0;
        for (int[] y : array) {

            for (int x : y) {

                nA[i  ] = x;
            }
        }
    // }

    return nA;
}

public static void main(String args[]) {

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

    int[] z = collect(array);

    for (int x : z) {
        System.out.print(x   " ");
    }
}

Prints:

1 2 3 4 5 6 7 8 9 10 11 

CodePudding user response:

You only need two for loops to go through a 2D Array.

Furthermore it is not a good idea to hard code the length of the array you return. If you don't want to use any other data structures in addition, I think you have no other chance for calculating the length of the new array than iterating over the whole 2D array to calculate the number of elements, initialize the new one with that size and than iterating again over the 2D array to fill the new one.

In code:

public static int[] collect(int[][] array) {
    
    int numberOfElements = 0;

    for(int i = 0; i < array.length; i  )
        numberOfElements  = array[i].length;

    int[] nA = new int[numberOfElements];
    
    int fillingCounter = 0;
    for (int i = 0; i < array.length; i  ) {

        int[] current = array[i];

        for (int j = 0; j < current.length; j  ) {
            nA[fillingCounter] = array[i][j];
            fillingCounter  ;
        }

    }
    
    return nA;
}

Alternatively, you can use a dynamic data structure (for example java.util.LinkedList) and then convert it to an array (for example with the toArray method)

  • Related