Home > Net >  Remove a Column from a 2D Array
Remove a Column from a 2D Array

Time:05-29

I have little problem with removing col from a 2d array.

The goal is to remove from every row specific index "2" and give it back.

Its how it need to be

I made it but got little problem with 0 at the end.

Its how I got

private static void removeEntry(int[][] workArray, int col) {
    int row = workArray.length;
    //largest row count
    int max = 0;
    int tempNum = 0;
    for (int[] ints : workArray) {
        tempNum = 0;
        for (int j = 0; j < ints.length; j  ) {
            tempNum  ;
            if (tempNum > max) {
                max = tempNum;
            }
        }
    }
    int [][] newArray = new int[row][max];
    for(int i = 0; i < row; i  ) {
        for(int j = 0; j < max; j  ) {
            if(j < col && j < workArray[i].length) {
                newArray[i][j] = workArray[i][j];
            }else if (j == col) {
                // Do nothing
            } else if (j > col && j < workArray[i].length) {
                newArray[i][j - 1] = workArray[i][j];
            }
        }
    }
    for (int i = 0; i < workArray.length; i  ) {
        for (int j = 0; j < workArray[i].length; j  ) {
            workArray[i][j] = newArray[i][j];
        }
    }
    

Then I tried to remove 0 but don't work

int remIndex = 0;
    for (int i = 0; i < workArray.length; i  ) {
        for (int j = remIndex; j < workArray[i].length-1; j  ) {
            if(workArray[i][j] == remIndex){
                workArray[i][j] = workArray[i][j   1];
            }
        }
    }

CodePudding user response:

Here is my approach on this. I used System.arraycopy() to copy the array up to the removed column and from directly after the removed column to the end. This way, we remove the column completely from the array.

private static int[][] removeEntry(int[][] workArray, int col) {
        int[][] resultArray = new int[workArray.length][];
        int index = 0;
        for (int[] row : workArray) {
            if (row.length - 1 < col) {
                resultArray[index] = row;
                index  ;
                continue;
            }

            int[] arrayCopy = new int[row.length - 1];
            System.arraycopy(row, 0, arrayCopy, 0, col);
            System.arraycopy(row, col   1, arrayCopy, col, row.length - col - 1);

            resultArray[index] = arrayCopy;
            index  ;
        }

        return resultArray;
    }

CodePudding user response:

Arrays are of fixed size. At the time of initializing array, a default value is stored in it. Here, 0 is stored as default. So I would suggest you to not initialize "col" as "max" but rather do it in for loop like this :-

int [][] newArray = new int[row][];

for(int i = 0; i < row; i  ) {
    if(col <= workArray[i].length){
        newArray[i] = new int[workArray[i].length-1]; //initialize it here
    }
    for(int j = 0; j < workArray[i].length; j  ) {
        if(j < col) {
            newArray[i][j] = workArray[i][j];
        }else if (j == col) {
            // Do nothing
        } else if (j > col) {
            newArray[i][j - 1] = workArray[i][j];
        }
    }
}
return newArray;

You are also supposed to return modified array as we can't changed the size of array and thus we had to create a new one ( newArray ). So do change the method definition as :-

private static int[][] removeEntry(int[][] workArray, int col)

Finally, whole method would look like :-

private static int[][] removeEntry(int[][] workArray, int col) {
    int row = workArray.length;
    //largest row count
    int [][] newArray = new int[row][];

    for(int i = 0; i < row; i  ) {
        if(col <= workArray[i].length){
            newArray[i] = new int[workArray[i].length-1]; //initialize it here
        }
        for(int j = 0; j < workArray[i].length; j  ) {
            if(j < col) {
                newArray[i][j] = workArray[i][j];
            }else if (j == col) {
                // Do nothing
            } else if (j > col) {
                newArray[i][j - 1] = workArray[i][j];
            }
        }
    }
    return newArray;
}

and you can use it like :-

workArray = removeEntry(workArray, 2);

CodePudding user response:

An array is a container of data that occupies a contiguous block of memory, its size should be defined when the array is being instantiated and can't be changed.

If you need an array of smaller or greater length, then you need to create a new one and copy all previously added elements that should be retained.

There's also no such thing in Java as "2D" arrays, we can create a nested array, i.e. an array containing other arrays.

And similarly to how we can reassign an integer value at a particular index in a plain array int[], we can change a reference in the array of arrays, i.e. we can make it point to another (newly created) array.

And that's what is required according to do in your assignment since the method is void. I.e. the given array needs to be changed by replacing all of its "rows", that greater or equal in length than the given column to remove col, with a new array that will retain all the previous element except for the one at index col.

private static void removeEntry(int[][] workArray, int col) {
    
    for (int row = 0; row < workArray.length; row  ) {
        
        if (workArray[row].length <= col) { // no need to change anything
            continue;
        }
        
        int newLength = workArray[row].length - 1;
        int[] newRow = new int[newLength]; // creating a new row shorter by 1
        
        for (int oldCol = 0, newCol = 0; oldCol < workArray[row].length; oldCol  , newCol  ) {
            if (oldCol == col) { // skipping the target column
                newCol--;        // newCol would be incremented automatically at the end of the iteration, but we want it to remain the same
                continue;
            }
            newRow[newCol] = workArray[row][oldCol];
        }
        
        workArray[row] = newRow; // reassigning the row
    }
}

main()

public static void main(String[] args) {
    int[][] testArr =
            {{1, 2},
            {1, 2, 3},
            {1, 2, 3, 4}};

    removeEntry(testArr, 2);

    // printing the testArr
    for (int[] arr: testArr) {
        System.out.println(Arrays.toString(arr));
    }
}

Output:

[1, 2]
[1, 2]
[1, 2, 4]

A link to the Online Demo

If you've made the method to be void by mistake, you are required to return a new array, i.e. the return type int[] (check the requirements of your assignment carefully). Then a minor change needs to be applied to the logic explained and implemented above: every array should be replaced with a new one and then placed into the newly created resulting array.

Note Arrays.copyOf() allows creating a duplicate of the hole array, and System.arraycopy() can help you to copy the elements in the given range from one array into another, but because you are working on an assignment I suggest you to do it manually with loops because you are expected to demonstrate the knowledge on how to manipulate with arrays, not the knowledge of special utility features (unless otherwise specified in the assignment).

private static int[][] removeEntry(int[][] workArray, int col) {
    int[][] result = new int[workArray.length][];

    for (int row = 0; row < workArray.length; row  ) {

        int newLength = col < workArray[row].length ? workArray[row].length - 1 : workArray[row].length;
        int[] newRow = new int[newLength];

        for (int oldCol = 0, newCol = 0; oldCol < workArray[row].length; oldCol  , newCol  ) {
            if (oldCol == col) {
                newCol--;
                continue;
            }
            newRow[newCol] = workArray[row][oldCol];
        }

        result[row] = newRow; // reassigning the row
    }
    
    return result;
}

main()

public static void main(String[] args) {
    int[][] testArr =
            {{1, 2},
            {1, 2, 3},
            {1, 2, 3, 4}};

    int[][] newArr = removeEntry(testArr, 2);

    // printing the testArr
    for (int[] arr: newArr) {
        System.out.println(Arrays.toString(arr));
    }
}

Output:

[1, 2]
[1, 2]
[1, 2, 4]

A link to the Online Demo

  • Related