Results 1 to 4 of 4
- 09-08-2010, 03:17 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 11
- Rep Power
- 0
How do I remove objects from an Array in java?
Hi. I really appreciate it if some one help me with this.
let's say foo is an Array of Strings, and it has the following values:
foo[0]="a";
foo[1]="cc";
foo[2]="a";
foo[3]="dd";
1)What do I have to do to delete/remove all the Strings/Objects equal to "a" in the Array?
2) What do I have to do to delete/remove all the Strings and make my array free of any value?
Thanx!
- 09-08-2010, 03:50 AM #2
Use a loop to search thru the array for the element.1)What do I have to do to delete/remove all the Strings/Objects equal to "a" in the Array?
Then it depends what remove means: Replace its value with a null, or move all the following elements down over the top of the one to replace, leaving the null element at the end of the array.
To empty it, fastest would be do define a completely new array.
- 09-08-2010, 04:03 AM #3
Member
- Join Date
- Sep 2010
- Posts
- 11
- Rep Power
- 0
Thanx for ur quick answer. I'm writing a method which should delete some matching values from the array event from a calendar. Do u think if this method will work?(appreciate it).
public void removeEvent(Event event){
boolean [] deleteItem = new boolean[index];
for(index=0;index<10;index++){
if(events[index].equals(events)){
deleteItem[index]=true;
events[index]="null";
numEvents--;
}
else{
deleteItem[index]=false;
}
}
}
- 09-08-2010, 01:20 PM #4
Best way to get an answer is to compile and execute the code and see what happens.Do u think if this method will work?
I don't know what you intend to do in the code you posted. Could you add comments saying what each statement is supposed to do. For example what is this statement supposed to do:Please use code tags when posting code: Java Forums - BB Code ListJava Code:if(events[index].equals(events)){
Similar Threads
-
Need help with a simple Java thing involving array of objects
By Jeremy8 in forum New To JavaReplies: 5Last Post: 02-25-2009, 07:14 PM -
how to remove an object from the array list
By cecily in forum New To JavaReplies: 3Last Post: 08-02-2007, 02:26 PM -
Remove the bucket represented by the int from array
By barney in forum New To JavaReplies: 1Last Post: 08-01-2007, 08:55 AM -
Array with objects
By toby in forum New To JavaReplies: 1Last Post: 07-25-2007, 09:50 AM -
array of objects
By Jack in forum New To JavaReplies: 2Last Post: 07-02-2007, 05:24 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks