aspose file tools*
The moose likes Java in General and the fly likes Problem in passing ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem in passing ArrayList" Watch "Problem in passing ArrayList" New topic
Author

Problem in passing ArrayList

Gaurav Jain
Ranch Hand

Joined: Mar 11, 2004
Posts: 108
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?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12805
    
    5
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
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Or you build a Wrapper and use an ArrayList inside:


http://home.arcor.de/hirnstrom/bewerbung
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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).


"I'm not back." - Bill Harding, Twister
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608


Tony Morris
Java Q&A (FAQ, Trivia)
Warren Dew
blacksmith
Ranch Hand

Joined: Mar 04, 2004
Posts: 1332
    
    2
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

Joined: Sep 24, 2003
Posts: 1608
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
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in passing ArrayList