Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

deleting from an array  RSS feed

 
tan kian
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, i'm trying to delete an item from an array by setting target item to null, and push all other items forward to take up the empty space. when i try to add something into the array, it gives me a NullPointer exception error.

for (int i=0; i<pos; i++) //gets position for id found
{
if(id.compareTo(acct1[i].find()) == 0) //when it finds a matching id
{ //to delete, get the location
deleteItem(id); //& deletes item
for (int x=i; x<array_pos; x++)
acct1[x] = acct1[x+1]; // try to push everything so that the gap
} //will be filled up
}
what other code do i need to solve the nullpointer error? thanks.
 
Ray Stojonic
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Based on your code I'll assume the following:
1- acct1 is an array of Objects (type is inconsequential, probably String)
2- deleteItem() finds the matching item in the array and sets the Object at that index to null.
Suggestions for improvment:
1- use equals() instead of compareTo().
2- use an ArrayList instead of an array.
3- use remove() on the ArrayList to remove the item.
 
Jeffrey Hunter
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code is probably breaking with the call to find(). Are you sure each Object in the acct1 array is valid? If you attempt to call find(), and the Object is null, you will get a NullPointerException.
 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hiya Tan,
Don't know if the following is any use:
//This method returns the inputArray but with element n removed (so
//the outputArray is one element shorter than the input array)
private String[] removeNthElementFromStringArray(String[] inputArray, int n) {
int inputArrayLength = inputArray.length;
String[] outputArray = new String[inputArrayLength - 1];
System.arraycopy(inputArray, 0, outputArray, 0, n);
System.arraycopy(inputArray, n + 1, outputArray, n, inputArrayLength - n - 1 );
return outputArray;
}
... I think you could easily change it to work with objects other than Strings.
Cheers,
James
 
tan kian
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks everyone. i will try it out and reply if i have any more problems.
 
tan kian
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
james: your code worked, but it made me think that my idea of how the system.arraycopy works was wrong all along. i didnt really understand the last sentence:
System.arraycopy(inputArray, n + 1, outputArray, n, inputArrayLength - n - 1 );
i'm ok with inputArray and outputArray, but the place where lets u input integers, well i'm confused over that. can someone explain to me what it does?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tai,
It would be a very good idea to learn how to use the API provided by Sun. The answer to your question can be found here. The method being used is System.arraycopy. so, search for System in the bottom left panel. click on it, and you'll see all the methods for the System class. There, you can find everythign you need to know about arrayCopy, and what all those parameters are.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!