wood burning stoves*
The moose likes Java in General and the fly likes removing a row from an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "removing a row from an array" Watch "removing a row from an array" New topic
Author

removing a row from an array

Ron Landreth
Greenhorn

Joined: Oct 04, 2006
Posts: 3
I am working on an accounting system that calls for rows to be deleted out of arrays.

Say for instance that I know the indices of the row I wish to delete. How would I go about writing code to do this? I am trying to delete rows out of a 2D array.

Here's how I'm trying to go about this:
- create a copy of the 2D array
- copy the 2D array back into the original array, skipping the row that I don't want copied back (i.e. deleting the row)

The code below just makes the values in the cells to be deleted (the cells in the row to be deleted) all zeroes (the default values for the primitive type double), instead of actually getting rid of the row.

Look at the code below for what I'm talking about:

The code in the if block finds the row that I want to delete. I would use an easier method to do this, but my teacher wants it done this way.

LRRMasterArray = new double[LRRNumberofRows][]; // this needs to be LRRMasterArray = new double[LRRNumberofRows - 1][], but that doesn't compile for some reason

for (int row = 0; row < LRRdblTempArray.length; row++)
{
if (LRRdblTempArray[row][0] == LRRgetCustomerID())
{
LRRMasterArray[row] = new double[LRRdblTempArray[row].length];
} // end if
else
{
LRRMasterArray[row] = new double[LRRdblTempArray[row].length];
for (int col = 0; col < LRRMasterArray[row].length; col++)
{
LRRMasterArray[row][col] = LRRdblTempArray[row][col];
}
}

Thank you for reading this and any help from anyone would be greatly appreciated.
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Hi, Ron. Welcome to Java Ranch.

Since array size is inmutable, there is not an acutal way to delete an element from an array. You could, though, set the value of an index to null (if your array is holding objects). This way, when an cell of the array is equal to null you could say that is deleted.

To actually delete an element from the array you would have to copy the array into a new smaler array, overlooking the element(s) that you want to delete.

For this purpose you could use System.arrayCopy() method.

Another option is that you create a wrapper class over the array that keeps track of the its actual size (that is to say: the actual number of elements that it holds, which is a subset of its array's lenght).

When a new element is added, you look for the first available position in the array (that whose value is null) and add the new element there.

Another option is that, whenever you delete an element, you can move the other elements toward the generated gap, leaving available cells on the top of the array. All new elements are added at size-1 position, because whenever you delete an element you move the other objects toward the gap.

When you remove an element, you decrease the size variable, when you add an element you increase the size variable.
sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148
Do you have to use arrays? There are more sophisticated containers for storing data that allow you to add and remove the elements at will, e.g. an ArrayList.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: removing a row from an array
 
Similar Threads
totaling array rows and columns
need help with calculation in my spreadsheet array
problems with displaying..
2D to 1D
2d Array problem gasprices