This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in passing ArrayList

 
Gaurav Jain
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • 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?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • 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
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you build a Wrapper and use an ArrayList inside:
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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).
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Warren Dew
blacksmith
Ranch Hand
Posts: 1332
2
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic