0

I have a 2D int array that I need to delete 1 element from each row, but they aren't necessarily in the same column.

For example, if I have the following array:

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

I would want the following output if I chose to delete the elements 2, 6, 8:

[[1, 3],
[4, 5],
[7, 9]]

The only answer I have found that closely resembles a solution was an answer to delete a whole column from a 2D array. I also understand ArrayLists would make my life extremely easy, but I am using a 2D array to make my life easier in a different area.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
cjnash
  • 1,228
  • 3
  • 19
  • 37
  • What part specifically do you need help with? Do you know how to "remove an element from an array"? – Carcigenicate Nov 15 '18 at 22:53
  • Why not loop on the 2D array twice - the first time for every row: switch between the element you want to delete and the last element. Second step: delete the last column (Unless the order of the element is important and if so please note that in the question) – dWinder Nov 15 '18 at 22:56
  • `IntStream.range(0, arr.length).map(r -> Arrays.stream(arr[r]).filter(x -> x != delete[r]).toArray()).toArray(int[][]::new)` – shmosel Nov 15 '18 at 22:57
  • @DavidWinder That just wouldn't work as I want to keep the order the same. – cjnash Nov 16 '18 at 03:13
  • @Carcigenicate The problem is how I want to keep the order the same within the array but get rid of one of the elements from each row. – cjnash Nov 16 '18 at 03:14
  • Don’t you have the solution you need in the link I posted? – Joakim Danielson Nov 16 '18 at 08:59

0 Answers0