简体   繁体   中英

how to delete an element in a two dimensional array in java?

let the array be

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

in the above shown array i need to delete the last element of even rows (2,4rows). So that my new array looks like

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

Please help how to do this with java code?

It looks like you are trying to treat this 2d array as a single array which is just being displayed in 2d. Maybe you should just use a single ArrayList and remove the elements normally.

Maybe you have to use a internal ArrayList (Single dimension) and have

  • a method that returns an bidimensional array
  • a methos that removes the x position in line y

    Your class must have the dimension size (maybe in the constructor).

You should assign the last element of your 2D array to be a new 1D array containing only the elements you want to keep:

arr[3] = new int[] {arr[3][1], arr[3][3]};

(assuming your array arr is of type int[][] )

将其视为列表并向后迭代,然后删除所有可被8整除的项目,您将得到希望得到的结果,并且如果要使用数组格式,可以将其转换为数组或2D数组

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