Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arraylist set(index i, Object o) method Exception

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i get IndexBound Exception.

of course I know I can use ArrayList.add(int index, Object o) method but I then shift the values one level . say I have an arraylist : 0 "0", 1 "1", 2 "2" (String object within the ArrayList Object references) and when I call alist.add(1,"new addition"); --> I will have gotten in the end : 0 "0", 1 "new addition", 2 "1", 3 "2".
any easyly implementable solution is to be appreciated!

bye,
R�stem..
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at LinkedList instead of ArrayList. It preserves the order things were added. Is that what you need?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem here is that you don't have an array list even similar to the example you gave. In the code you gave above, you create an empty ArrayList and then try to set() an element. However, since the ArrayList is emtpy (i.e. it has no elements) you can't set() any elements. You have to use add() to first create elements in the list, then you can later replace the existing elements with set(). Maybe an example will help:


This should work. However, I haven't compiled nor run it, so I don't know for sure.

HTH

Layne
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Layne, I donot want to add anything at the beginning, it wont help in my case.
Plus: the elements to be set come in unspecified order; say first I set the 3rd elt, then the 5th then 6th then the first...

Stan: yes it is the thing I was looking for. But according to http://java.sun.com/j2se/1.4.2/docs/api/java/util/LinkedList.html#add(int, java.lang.Object), add() in LinkedList also shifts the ever existing elements to the right...

otherwise I will be using just an Object objArray [] ...
[ January 14, 2005: Message edited by: R�stem-� Zal� ]
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by R�stem-� Zal�:
Plus: the elements to be set come in unspecified order; say first I set the 3rd elt, then the 5th then 6th then the first...
You're describing a sparse array where some elements are considered "empty." You can achieve the same thing by adding a lot of null values to create spaces in which to "set" elements as they arrive.

Check out ArrayList's source. It has a capacity (the size of the underlying array) and a size (the number of elements with values) where size <= capacity at all times. In order to set an element's value, the index must be less than size.

To say this another way, you must add() elements to create slots to hold values before you can set() values into those slots. Of course, add() sets the slot to the value you specify as well.I also note that java.util.Arrays has an asList(array) method that you could use to replace the for loop. I don't know when it was added, but it should do the trick since you're starting with null values.
[ January 14, 2005: Message edited by: David Harkness ]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thx David,
that is what I have needed!

Still one question: in List interface void clear(); returns Exception, so i have used list = Arrays.asList ( new Object [] ); but imo this is not very efficient in terms of gc, explicitly setting the object refs to null would have been better..

ciao,
R�stem
[ January 14, 2005: Message edited by: R�stem-� Zal� ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, you may look at a Collection that implements the Map interface (such as HashMap or TreeMap) instead of ArrayList. Maps use a key to look up a value, much like an array. The difference is that the key can be any Object and don't have to be in any order. This is an advantage since you don't have to use extra memory for the "empty" null elements like you do in David's suggestion above. However, there is a cost (as is typically with most solutions to programming problems): Since the key is an Object, you will have to use wrapper classes (such as Integer) instead of primitive types (such as int). With Java 1.5, some of the tedium in writing the code here is alleviated with generics, auto-boxing, and auto-unboxing, but the run-time overhead still exists. These are a whole other topic, though, so I won't get into them here.

Anyway, I thought I'd suggest this alternative so you can consider it. IMO, it is useful to have several alternatives when solving a problem and compare the advantages and disadvantages to decide which solution is more appropriate to the problem at hand.

Keep Coding!

-Layne
[ January 14, 2005: Message edited by: Layne Lund ]
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by R�stem-� Zal�:
in List interface void clear(); returns Exception, so i have used list = Arrays.asList ( new Object [] ); but imo this is not very efficient in terms of gc, explicitly setting the object refs to null would have been better..
I'm not sure I follow. How does List.clear() enter the picture here? Oh, note that clear() removes the elements; it doesn't set them to null. Thus you'd still have to add() null elements to create slots.

Also, Arrays.asList() wraps the array you give it inside a non-resizable version of ArrayList (private class inside Arrays.java for just this purpose, so there's nothing being wasted. You can set() elements on it (so it's not entirely immutable), but you cannot add() or remove() elements.

Finally, Layne makes a good point. An a Map would be even more helpful if you didn't know the range of indexes up front or if the array was going to be very sparse.
[ January 15, 2005: Message edited by: David Harkness ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic