• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList Equivalent of Vector Enumeration  RSS feed

 
Pat Flickner
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am converting code to use ArrayList instead of Vector. I have successfully replaced all the code (addElement=add, elementAt=get, firstElement=get(0), etc.), including the insertElement with some code (I'll include it because I'm not sure if it's the cleanest way to do this), but I am stuck with the Vector.elements(). I am thinking it should be something like this:



And this is what I did to replace the insertElement():


So, am I making more work for myself? Is there an easier way?

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.util.Iterator is the replacement for the legacy class java.util.Enumeration. Class ArrayList has an iterator() method to get an iterator.

However, if you're using Java 5 or newer, you can make the code even cleaner by using the foreach loop syntax. It would be better if you used generics, so that you can avoid type casting (which is not type safe). I'd write your code something like this:

Note, for the foreach loop to work like that, the method getBusinessObjects() must use generics, i.e. it must return a List<BusinessObjectIF> or something similar, not a plain List.

And what does the second piece of code do, insert an element at a specific index in the list? That can be done with a single method call, no looping necessary:
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can simply replace insertElement(x, index) with add(index, x). As for elements(), you can use iterator() in its place. That returns an Iterator instead of an Enumeration, but there too is a simple mapping:
Enumeration.hasMoreElements() -> Iterator.hasNext()
Enumeration.nextElement() -> Iterator.next()
nothing at all -> Iterator.remove()

But if you read the Javadoc page of java.util.Vector you'll see all these mappings - the methods refer to their replacements:
copyTo(Object[]) -> toArray(T[])
element() -> iterator()
elementAt(int) -> get(int)
setElementAt(E, int) -> set(int, E)
removeElementAt(int) -> remove(int)
insertElementAt(E, int) -> add(int, E)
addElement(E) -> add(E)
removeElement(Object) -> remove(Object)
removeAllElements() -> clear()
 
Pat Flickner
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!!!
 
Pat Flickner
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just following up - I was able to successfully move everything into ArrayList objects thanks to you. I really appreciate it!
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!