Results 1 to 7 of 7
- 11-20-2011, 09:13 PM #1
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Why can not I remove an object from an arrayList?
Hi guys this is my first question in this forum, thanks for the great work.
Here is my question:
I have an ArrayList called shipGridPlaces full of integers
if I use this code:
if(shipGridPlaces.contains(k))
{
int j = setup.shipGridPlaces.indexOf(k);
System.out.println("A hit!");
setup.shipGridPlaces.remove(j);
}
I can remove the integer k from the ArrayList with no problems.
But how I can directly remove an integer from an ArrayList?
Why doesn't this work:
if(shipGridPlaces.contains(k))
{
setup.shipGridPlaces.remove(k);
}
This code actually removes the index number k, not the k itself. So it looks for say k= 45, tries to remove the 45.th element in the ArrayList? Is there any way to remove the object 45 from the ArrayList directly without going with my first solution?
I hope my question is clear,
Thanks for your helps.
- 11-20-2011, 09:27 PM #2
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: Why can not I remove an object from an arrayList?
I found this:
import java.util.ArrayList;
public class RemoveArrayListElement {
public static void main(String[] args) {
ArrayList<String> arlist=new ArrayList<String>();
//<E> it is return type of ArrayList
arlist.add("First Element"); // adding element in ArrayList
arlist.add("Second Element");
arlist.add("Third Element");
arlist.add("forth Element");
arlist.add("fifth Element");
// remove array list element by index number
arlist.remove(3);
// remove ArrayList element by Object value
arlist.remove("fifth Element");
// get elements of ArrayList
for(int i=0;i<arlist.size();i++)
{
System.out.println("ArrayList Element "+i+" :"+arlist.get(i));
}
}
}
But what if my object is an integer itself? How do I remove that directly?
- 11-20-2011, 09:37 PM #3
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: Why can not I remove an object from an arrayList?
I changed the type of k from int to Integer,
solved my problem.
Thanks guys.
- 11-20-2011, 09:42 PM #4
Re: Why can not I remove an object from an arrayList?
ArrayLists can only contain objects. You would not be able to put an int into an ArrayList.what if my object is an integer itself? How do I remove that directly?
You would use one of the wrapper classes: Integer.
What is "object 45"? What type of object?Is there any way to remove the object 45
There is a confusion caused by java's autoboxing feature. The compiler will automatically convert an int to an Integer when the syntax requires an object. But it does not convert an int if an int value could be valid because there is an overloaded method.
anArrayList.add(23); // converts 23 to new Integer(23) and adds that object
anArrayList.remove(23); // this will NOT covert the 23 to an Integer. It will remove the 23rd element
You might think add and remove could use the same arguments and work the same way but you'll be fooled by autoboxing.
- 11-20-2011, 10:18 PM #5
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: Why can not I remove an object from an arrayList?
Hi Norm thanks for the answer.
Actually when the k in arraylist.remove(k) is an Integer (not an int), then it removes k really not the kth element in the arraylist.
Thanks.
- 11-20-2011, 10:51 PM #6
Re: Why can not I remove an object from an arrayList?
Yes there is a difference between the two overloaded methods as I tried to explain:
remove(int)
vs
remove(object)
- 11-21-2011, 01:37 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,466
- Rep Power
- 16
Similar Threads
-
ArrayList remove an element from the list
By ralf in forum New To JavaReplies: 37Last Post: 07-12-2011, 07:08 PM -
ArrayList and remove()
By Pragz in forum New To JavaReplies: 1Last Post: 04-21-2011, 01:42 AM -
need help with the remove method on arrayList
By ShinTec in forum New To JavaReplies: 5Last Post: 02-16-2010, 09:38 AM -
how to pass an arraylist from an object back to the parent object that it was created
By george_a in forum New To JavaReplies: 1Last Post: 03-04-2009, 06:14 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks