1

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?

4
  • 1
    this seems to be one dimensional array. am I missing something? Commented Jun 23, 2011 at 15:43
  • No its a two dimensional array. The array size may increase. Commented Jun 23, 2011 at 15:55
  • two dimensional array does not mean size my increase. two dimensional array means you index an element of the array by a first and second index. Commented Jun 23, 2011 at 16:03
  • As many have said, you might be better off just storing it as a 1-dim array and only showing it as 2-dim at print out. Depending on the other operations you are performing on your arrays, you should decide what is the best internal representation. Is this homework? It looks like an artificial problem. Commented Jun 23, 2011 at 16:10

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

In the given example we used 4 by 4 matrix the matrix size may increase. So we have to consider it as two dimensional array.
well in the example you show above, it's no longer a matrix once you delete items. It's not 4 arrays of non-equal size.
0

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).

Comments

0

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[][])

Comments

0

Treat it as a list and iterate backward and delete all items divisible by 8 and you will get the result you are hoping to get , and in case you want in array format , you can convert the its to array or 2D array

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.