• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Problem in passing ArrayList

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont want to pass an ArrayList from one class to another as i dont want the client to explicitly type cast the objects present in the ArrayList that is the client should not be bothered to know that what type of object does the ArrayList contains.
What are the other options available that is what can i pass instead of ArrayList?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure I understand what you want, but I find it convenient to use the toArray( exampleArray ) method. For instance, this fragment reads lines to an ArrayList but returns a String[]

If you hand toArray an array of the right size, it fills in the elements, casting to the array type.
Bill
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or you build a Wrapper and use an ArrayList inside:
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or use Java 1.5 to return a List<Foo> (a List whose contents are all Foo instances and do not need to be cast to Foo before you use them).
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
blacksmith
Posts: 1332
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim, do you have more information or a link on generics in Java 1.5? How similar are they to C++ templates?
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a particular distinction to made between 1.5 generics and C++ templates.

http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
http://www.jcp.org/aboutJava/communityprocess/review/jsr014/
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well Tony's already given links to the nitty gritty details; excellent. There's also a good overview of 1.5 changes here. and note that JDK 1.5 beta is out now, so you can try out generics yourself. (Along with lots of other changes.)
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic