• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

arraylist assigned to VO object

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic