• Post Reply Bookmark Topic Watch Topic
  • New Topic

arraylist assigned to VO object  RSS feed

 
Christian Nash
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

What happens when a VO object is assigned with an arraylist object such as
Iterator it = arraylist.iterator()
SomeVo vo=(SomeVo)it.next();

Now if the arraylist contains some Vo objects what happens during this assigning?
 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't quite follow but here's a shot...

If the ArrayList does not have a VO object it will throw a cast class exception. If it does have a VO object (or null), nothing about the object will change, it will just not be referencable from the "vo" pointer. If you were to use an Object pointer, the object would still be the same, but the methods of the SomeVo class would be hidden since as a pointer, java does believe you have access to these methods without a cast (or reflection).
 
Ramasubbhu Allur Kuppusamy
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The rule of thumb is to always wrap the it.next() statement inside a while(it.hasNext()) block and typecast the it.next() to the appropriate Class type.
[ June 24, 2006: Message edited by: Ramasubbhu Allur Kuppusamy ]
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
typecast the it.next()

Not any more. In Java5 and later, you have a generics syntax. If you objects are of class Foo with a method bar(), you would set up both the ArrayList and Iterator generically. The simplest way to do this is:-
And it is usually best to have Iterators as local variables; they throw ConcurrentModificationExceptions if they are used as fields.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!