• Post Reply Bookmark Topic Watch Topic
  • New Topic

Remove a row in a 2D Array?  RSS feed

 
Manny Mik
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have a project in my highschool class, and one of the questions is a helper method to remove a row. This is what i have for now...


Having the mat.length-1 in line 3 gives me an out of bound error at line 9. But without it, it prints the array with the row I want removed in the default format.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need a separate 'row' variable for the destination row.
You need to check if you've reached the row to delete *before* entering the column for loop.
Your column for loop should test 'c' against mat[r].length.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I know lots of people talk about 2D arrays, but that isn't what you have. It is an array of arrays, which has a different internal structure, and Java® doesn't have 2D arrays. Don't worry: arrays of arrays give you better functionality.
You cannot remove an element from an array (you might call that element row); you can only replace it with another element (row). That element might be empty, which you can create with new Foo[0], or it might be an element found or made somewhere else, or you can even assign it to null, but it is a bad idea to let nulls loose.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you are doing there is create a new array one smaller than the old array. I am not convinced that your counting is right. Imagine you have an array with 10 elements and you want to remove that at index 6. You will have to iterate the recipient array 0-1-2-3-4-5-7-8-9.
I presume you have been told to write your method the hard way with loops. Otherwise I would suggest System#arraycopy() or one of the copying methods of the Arrays class.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Further to what Campbell said about Java arrays being nested rather than truly multidimensional, you can declare an array like this:

This creates an array with 5 elements, each of which will be an array of int. Nested Java arrays can be jagged, that is, the nested arrays are independent of each other and therefore are not required to have the same dimensions. One flaw in your code is that it assumes that all the nested arrays have the same length. While your actual usage may bear this out, neither Java nor your code inherently enforces this constraint so your code could break if it was given a jagged array.

Also, Java arrays are objects so you can treat them as a single entity. That means you don't have to copy each element individually using a nested for-loop. A single for-loop that copies each nested int[] that should be copied is all you need.
 
Manny Mik
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your replies. I forgot to include that my teacher gave preconditions for this method, which are that...
1.  mat.length>=3
2.  0 <= row <= mat.length-1
3. mat wont be jagged
I got the method to work with these conditions

 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks very complicated. You still seem to be thinking in terms of 2D arrays, but there ain't no such thing in Java®. An array is an object and what appears to be a 2D array is in fact an array of arrays. So a 6×6 matrix comprises seven objects: the matrix and the six included arrays. If you want to miss out one row, all you have to do is copy all other rows into an array one smaller than the source. I suggest you use the clone() method on each included array so you are copying it and not simply creating an alias. You can do that with a single loop and an if nested inside the loop. That sounds the same as what Junilu told you on Sunday.
That method shou‍ld be robust enough to cope with preconditions laxer than what you are being given. I think it will require:-
  • array.length > 0
  • row ≥ 0 ∧ row < array.length
  • ii ≥ 0 ∧ i < array.length ⇒ array[i] ≠ null
  • It will cope with jagged arrays and 0‑length subarrays but not with nulls.
     
    nilesh padmagiriwar
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi,

    try this code -
    first check for validity of 'row' (if its invalid print some error message or throw exception or return null), if its valid then only create new object of 2d array(array of array) of int type and apply remaining logic of the method.
    Don't need 2 for loops , single for loop will do.
    I hope this is helpful to you.

    Thanks,
    Nilesh
     
    Liutauras Vilda
    Sheriff
    Posts: 4928
    334
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    nilesh padmagiriwar, welcome to the Ranch

    Thanks for your input, but we don't provide user's with complete solutions, but rather give them opportunity to learn and come up with own solution - that is the goal of their, isn't it?
    Don't be annoyed, but I removed your suggested solution. Hints are ok, so don't hesitate to give those to OP, or to show small code snippets which represent one or another concept user might need to follow.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!