This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes ArrayList update and remove Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList update and remove" Watch "ArrayList update and remove" New topic
Author

ArrayList update and remove

K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2615
    
    9

Hello I have 2 arraylists (aryList1 contains 1s and 0s indicating indices that need updating or removing; and aryList2 contains the items that are to be updated or removed). The 2 arylists' size ARE the same.

My update code


I found if I leave out the remove(i) line, the arraylist size after update increases by the number of items you are updating. Eg aryList2 original size is 3, after update becomes 5 if I update 2 items. Why is that? Once I add the remove(i) line the size becomes consistent.

As for the remove code, I found that it WILL prompt IndexOutOfBoundsException if I don't start removing from the first item.
Suppose I have 3 items in aryList2. I want to remove 2nd and 3rd items (index 1 and 2 of aryList2).
My remove code


IDEAL logic:
original size is 3
i=0 prints index0 !=1 continue
i=1 prints index1 ==1 remove(1) and prints size=2
i=2 prints index2 ==1 remove(2) and prints size=1

BUT when i=2 IndexOutOfBoundsException. I understand this exception prompts if index>=size which it is when i=2. So how to avoid this?

Any comments are welcome. Thanks


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Hello K,

All you need to know is how does add() method work?
add(int index,String element);

Whenever you call this method it will add (not Update) an entry at location pointed by index.And the rest of the entries will be shifted down.
You can check it by printing the list.

[ UD: removed unnecessary quotation of original question ]
[ April 26, 2008: Message edited by: Ulf Dittmer ]
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Try to know how does remove() method work? What happens with your list after removing an element.
This is your homework.

Once you get it , you will get the answer as well.
If you find any doubt paste it here.

[ UD: removed unnecessary quotation of original question ]
[ April 26, 2008: Message edited by: Ulf Dittmer ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

Instead of removing and re-adding, you can use the set(int index, Object value) instead, to really replace the value.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2615
    
    9

I understand the add() method perfectly fine. Yet I never considered the set() method earlier. Thanks Rob.

As for the remove() method I will check it out more and let Vishal know I do later.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2615
    
    9

I got the remove problem fixed. The answer was to reverse the index
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList update and remove