Results 1 to 20 of 38
- 07-08-2011, 09:22 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
ArrayList remove an element from the list
Java Code:This is the error code I get when I insert a number into the parameter java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 at java.util.ArrayList.RangeCheck(ArrayList.java:547) at java.util.ArrayList.remove(ArrayList.java:387) at Auction.removeLot(Auction.java:155) This is the method where I try to remove an element. Has any one an idea of where the problem is? /* * Remove the lot with the given lot number * @param number The number of the lot to be removed. * @return The lot with the given number, or null if * there is no such lot. */ public Lot removeLot(int number) { if(number < 0) { System.out.println("This number does not exist " + number); } else{ Iterator it = lots.iterator(); while(it.hasNext()) { Lot lot = (Lot) it.next(); //number, description, gethighestbid if(number == lot.getNumber()) { lots.remove(number); } return lot; } } return null; }
- 07-08-2011, 09:37 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Looks to me there is no guarantee that the lot number corresponds to the index of said object within the array. Call remove on the object rather than its lot number, or use a Map keyed with the lot number and indexed with the object, which would let you quickly get the Object and remove it from the List
- 07-08-2011, 09:41 PM #3
The iterator can freeze updating of the container it is iterating over.
Another technique is to use a regular for loop and go thru the list in reverse order. Then when you remove an item it is past where the next item will come from and not cause a problem
- 07-08-2011, 09:47 PM #4
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
Hello,
The first element in the arraykist is given number 1 an so on. And I insert a number into the parameter to remove the element with the same number.
I am not shore what you mean "remove the object rather than its number"?
- 07-08-2011, 09:48 PM #5
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
Hello,
The first element in the arraykist is given number 1 an so on. And I insert a number into the parameter to remove the element with the same number.
I am not shore what you mean "remove the object rather than its number"?
- 07-08-2011, 09:52 PM #6
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Indexes for arrays start at 0, for it the first element 'number' is one, you cannot remove from a list an element that does not exist. To remove the object, just pass the object to remove to the remove method. Further, take note of Norm's advice. Modifying a List while iterating can result in a ConcurrentModificationException
- 07-09-2011, 05:43 PM #7
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
Hello,
I have tryed for (int i = 0; i > lots-1; i > 0; i--) after an example on internet, but I can't get it to work.
Any idea of what I am doing wrong?
- 07-09-2011, 05:55 PM #8
No. Not without seeing your code.but I can't get it to work.
Any idea of what I am doing wrong?
What error messages do you get with this?Java Code:for (int i = 0; i > lots-1; i > 0; i--)
Do you know the syntax of the for statement?
It looks like you need to go to your textbook and review how to write a for statementLast edited by Norm; 07-09-2011 at 05:57 PM.
- 07-09-2011, 06:22 PM #9
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
Hello,
with this code I am able to loop through the list, but not to remove the number(element) from the list
Java Code:public Lot removeLot(int number) { if(number <=0) { System.out.println("This number does not exist " + number); } else{ for(number = 0; number < lots.size(); number++) if(number == lots.size()) { lots.remove(number); //System.out.println(lots.get(number)); } // return lot; } return null; }
- 07-09-2011, 06:45 PM #10
Please explain what happens and why you are not able to remove the element from the list.able to loop through the list, but not to remove the number
What is the purpose of this statement?Java Code:if(number == lots.size())
Is it ever true? Did your println ever print anything?
- 07-09-2011, 07:08 PM #11
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
You clearly didn't read the ArrayList API docs properly before using the remove(..) method. If you do, you'll see that when an object is removed, all subsequent objects are shifted down one position to fill the gap. This will mean the indexes in your objects no longer match the ArrayList indexes. So if you try to remove the last object by its index, you'll be trying to use an index past the end of the list.
When using an unfamiliar class or method always read the API docs carefully.
- 07-09-2011, 07:14 PM #12
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
Hello,
I have tryed many different ways to solve this, I realize that the former code I sent was bad code.Please explain what happens and why you are not able to remove the element from the list.
I have written a new code which seems more correct, but it won't work.
It did not print anything. I am trying to match the number from the input with the number from the list. It is wrong way to do it, but I can't figure out what else I should doif(number == lots.size())
Java Code:public Lot removeLot(int number) { if(number <=0) { System.out.println("This number does not exist " + number); } else{ for(int num = number; num < lots.size(); num++) if(num == lots.size()) { lots.remove(number); //System.out.println(lots.get(number)); } // return lot; } return lot; //This one won't compile. It can't find lot }
- 07-09-2011, 07:18 PM #13
Where is the "number from the list"?I am trying to match the number from the input with the number from the list
Is it the contents of one of the objects in the list?
Or is the the index of the object in the list? Remember the indexes of items in the list start at 0
What is the purpose of this statement?Java Code:if(num == lots.size())
That means that the statement is never true. If it is not true then the remove is not executed.It did not print anything
- 07-09-2011, 07:22 PM #14
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
Yes, I now that the first index in an array/list start at zero, but every element in the arraylist in the class has been given a number. So the element at index 0 has been given the number 1. I thought that I could remove the element which has been given number 1 with looping through the list to find number 1 and the remove it.
public Lot removeLot(int number)// Here I can insert a number
every element has been given a number when they was added to the list
I can insert number 5 which maybe is on index 4. If number 5 is found in the list then remove it, it does not matter on which index it is.
Do I think wrong? is it impossible to search after a given number like that I am trying to do?Last edited by ralf; 07-09-2011 at 07:32 PM.
- 07-09-2011, 07:24 PM #15
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
I just told you why your method doesn't work, and doWhile has already told you how to remove the correct object. Find the object in the ArrayList that contains the number you're looking for, and pass that object to the ArrayList.remove(..) method.
Again, if you'd read the ArrayList API docs carefully, this would be clear. It's not rocket surgery...Last edited by dlorde; 07-09-2011 at 07:26 PM.
- 07-09-2011, 07:37 PM #16
Your code does NOT look at the contents of any the elements in lots. It is not searching for anything.is it impossible to search after a given number like that I am trying to do?
- 07-09-2011, 07:41 PM #17
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
- 07-09-2011, 08:28 PM #18
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
Hello again
I am sorry that I bother you
With this code I am able to remove elements from the list
When I insert number 1 to remove element 1, it removes the element from index 1 and not the element with number 1Java Code:public Lot removeLot(int number) { if(number <=0) { System.out.println("This number does not exist " + number); } else{ Iterator it = lots.iterator(); while(it.hasNext()) { Lot lot = (Lot) it.next(); //number, description, gethighestbid if(number == lot.getNumber()) { lots.remove(number); } return lot; } } return null; }
How should the code be changes so I can remove the correct element?
This is the output: I have added three elements to the list. I insert number 1 and number 2 on index 1 is removed and not the element 1 on index 0
1: AAAA (No bid) // index 0
2: BBBBB (No bid) // index 1
3: CCCCC (No bid)// index 2
1: AAAA (No bid) //index 0
3: CCCCC (No bid) // index 1
- 07-09-2011, 08:42 PM #19
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
Hello Thank for your Help! Norm, dlorde and dowhile
This code work now. It remove the correct element
Java Code:public Lot removeLot(int number) { if(number <=0) { System.out.println("This number does not exist " + number); } else{ Iterator it = lots.iterator(); while(it.hasNext()) { Lot lot = (Lot) it.next(); //number, description, gethighestbid if(number == lot.getNumber()) { lots.remove(lot); } return lot; } } return null; }
- 07-09-2011, 08:51 PM #20
Be sure to test your code with different input.
I ran a test with similar code:
And get this error:Java Code:ArrayList<String> lots = new ArrayList<String>(); lots.add("First"); lots.add("Second"); lots.add("Third"); System.out.println("before: " + lots); Iterator it = lots.iterator(); while(it.hasNext()) { String lot = (String) it.next(); //number, description, gethighestbid if(lot.equals("First")) { lots.remove(lot); System.out.println("lot=" + lot); } } System.out.println("after: " + lots);
before: [First, Second, Third]
lot=First
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification( AbstractList.java:372)
at java.util.AbstractList$Itr.next(AbstractList.java: 343)
at TestCode4.main(TestCode4.java:450)
Similar Threads
-
ArrayList copy some of the element from one arraylist tnto another arraylist
By ralf in forum New To JavaReplies: 12Last Post: 07-07-2011, 08:49 PM -
ArrayList and remove()
By Pragz in forum New To JavaReplies: 1Last Post: 04-21-2011, 01:42 AM -
remove element contents
By oliveira in forum XMLReplies: 5Last Post: 12-07-2010, 08:44 AM -
Copy a specified element from one array list to another arraylist
By TaxpayersMoney in forum New To JavaReplies: 1Last Post: 05-20-2010, 10:17 PM -
[SOLVED] ArrayList element to int
By grahamb314 in forum New To JavaReplies: 9Last Post: 11-22-2008, 05:09 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks