Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Typecast Object type to user defined type

 
K Sujit
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();

-------------------------------------------
//My Code

import java.util.*;

class PointList extends ArrayList{
}

class SegmentList extends ArrayList{
}

public class StudyArrayList{
public static void main(String args[]){
System.out.println("Hello");

SegmentList s = new SegmentList();
PointList p = new PointList();

p.add(new Double(492661.56250));
p.add(new Double(496868.65625));
s.add(p);
p = new PointList();
p.add(new Double(426870.80903));
p.add(new Double(-432341.89723));
s.add(p);

Iterator sI = s.iterator();
while (sI.hasNext()) {
System.out.println("Next Segment");
PointList pTemp = sI.next();
Iterator pI = pTemp.iterator();
while(pI.hasNext()){
System.out.println(pI.next());
}
}
}
}
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to explicitly cast the object back to its correct type...

PointList pTemp = (PointList)sI.next();

Also, please use code tags to keep formatting intact...
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

List<String> list = new ArrayList<String>();

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
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rick O'Shay:
Make sure you are clear that the object you get and the reference to it are distinct items...

That's a good point. To be clear, I probably should have said, "You need to explicitly cast the reference of type Object back to its correct type..."
 
Philip Heller
author
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you also use the new JDK 1.5 "for each" construct to shorten the code to something like this?

Is this equivilent to the double iterator usage in the code above?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic