Suppose you created a class and put objects of it in a ArrayList when u retrieve from the arraylist u will get Object type not your class types so how will we get our class type from Object types. ----------------------------------------------- See Compile error StudyArrayList.java:25: incompatible types found : java.lang.Object required: PointList PointList pTemp = sI.next();
Make sure you are clear that the object you get and the reference to it are distinct items. The reference type determines what methods you can call. The toUpperCase fails here and the reasons should be clear.
String foo = "boo"; Object bar = foo; bar.toUpperCase();
As far as arrays, you should use Java 5.0 to minimize casting. This really has become "basic" Java now:
Couple things. First use the interface type for references where possible. Next, tell the compiler what type of List you are referring to and create one that is compatible with the reference: hence, String appears twice.
String x = list.get(0); // Cast is no longer required
I recommend using generic collections whenever possible. Since your list contains only instances of Double, you can do this:
The syntax takes some getting used to. The type is no longer "ArrayList" but "ArrayList<Double>", which I pronounce as "ArrayList of Double". The compiler forbids adding anything that isn't instanceof Double, thus catching many bugs. And when you retrieve from the collection, you don't need to cast.
In your case, you extended ArrayList. To use generics, your class declaration becomes:
More in Chapter 8 of "Complete Java 2 Certification".
Hope this helps. -- Phil
Consultant to SCJP team.<br />Co-designer of SCJD exam.<br />Co-author of "Complete Java 2 Certification Study Guide".<br />Author of "Ground-Up Java".