0

I want to remove an element after creating the below 2d array.And it should be the last element of the array.

public static void main(String[] args) {
        int i;
        int row = 2;
        int col = 2;
        String[][] queue = new String[row][col];
        Scanner scanner = new Scanner(System.in);
        String[][] queue = new String[row][col];
        Scanner scanner = new Scanner(System.in);
        for (row = 0; row < queue.length; row++) { // Loop through the rows in the 2D array.
            for (col = 0; col < queue[row].length; col++) { 
                System.out.println("Enter the name");{
                    queue[row][col] = scanner.next();
                }

                }
            }
        for (i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                System.out.print(queue[i][j] + " ");
  • Have you tried anything yet? What exactly is the problem? – Mureinik Jul 03 '22 at 14:15
  • Arrays in Java have *fixed* size, so you can't *remove* or *add* anything to it. BUT you can *set* some value at specified index (which sometimes is referred as "*adding*" something to array, which causes confusion in terminology). Now to *simulate* "removing" element from array you can simply *set* at that index some special value which will represent "no value". For instance in case of array of objects it could be `null`, or in case of array of positive numbers set there `-1`. – Pshemo Jul 03 '22 at 14:28
  • From the looks of your code, this seems like a possible XY problem. Why do you want to remove the last element? What if you could prevent it from being put there in the first place? "2D" arrays do not have to be rectangular. Can you provide more detail in your question. – WJS Jul 03 '22 at 14:55

2 Answers2

3

The question is awkward.

  • You can't "remove" an element from a 2D static array - what you can do is empty that array element. This is as simple as queue[row - 1][col - 1] = "".
  • If you want to be able to remove elements the way you describe, your best choice is use dynamic arrays, such as ArrayList.
  • Note though that Java supports 2D arrays with differing number of columns per row, so you can also create a new array, copy everything except the last column in the last row, and put it back in the original array.
Leaderboard
  • 371
  • 1
  • 10
0

You cannot remove a cell from an array. But you can effectively remove it by simply replacing the last row with a new copy of all but the last element.

queue[row-1] = Arrays.copyOf(queue[row-1], queue[row-1].length-1);

You might want to try creating the array like this. Then you won't need to remove or copy anything.

  • define the Array with the number of rows.
  • then assign the row array separately to the "2D" array. For a large number of rows, this could be done in a loop with an int[] array of dimensions for each row.
int rows = 2;
String[][] queue = new String[rows][];
queue[0] = new String[2];
queue[1] = new String[1];
Scanner scanner = new Scanner(System.in);
for (rows = 0; rows < queue.length; rows++) { // Loop through the rows in the 2D array.
    for (int col = 0; col < queue[rows].length; col++) { 
        System.out.println("Enter the name");{
            queue[rows][col] = scanner.next();
        }
    }
}
for (String[] row : queue) {
    System.out.println(Arrays.toString(row));
}
WJS
  • 36,363
  • 4
  • 24
  • 39