File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes ArrayList - how to store objects of certain kind Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList - how to store objects of certain kind" Watch "ArrayList - how to store objects of certain kind" New topic

ArrayList - how to store objects of certain kind

Patricia Smith

Joined: Feb 19, 2005
Posts: 3
I'm new and I am trying to store an ArrayList of Term objects. The Term objects represent terms in a polynomial. (A polynomial is a math expression like 4x^3 + 3x^2 - 2x + 4.) I have 4 files. One is a Term class, one is a Polynomial class, one is a PolynomialInterface and the last is a tester file program.

My Terms are simply objects with 2 instance variables: coefficient and exponent. In it, I have 2 constructors (one that takes no parameters and one which does). I have the getters and setters to extract and change the coefficient and exponent variables. Finally, I have a toString method.

In the PolynomialInterface (which I was given and must use to solve this problem), I have a myriad of unimplemented methods.

In the Polynomial class, I created it to inherit from ArrayList and implement 2 interfaces: PolynomialInterface and Cloneable

My first constructor takes no arguments and looks like this:

Later on down the code, I began to try and implement an add() method which is supposed to take in a Polynomial object and add it to the current Polynomial object. However, there's this error in my code:
Type mismatch: cannot convert from Object to Term

Here's how I began to implement the add() method.

So, then I thought "Ok, this.get is returning an Object and not a Term. I don't want to bandaid it with a type-cast. How do I construct the Polynomial ArrayList so that it only holds Term objects?"

And that's my question in a nutshell. If I left anything out, let me know!

And THANK YOU for being here to support us newbies!

Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Patricia, Welcome to JavaRanch

An ArrayList (or any collections class for that matter) can only hold an item of type Object. This is so polymorphism can work its magic, since all classes untimately inherit from type Object, an ArrayList can ultimately hold any type of object. If the ArrayList class took specific types of objects, they would have to have an ArrayList class to hold every type of Class in the JDK. And then that wouldn't even be enough, because then how would developers store their custom objects?

Therefore, in Java v1.4.2 and below, you always need to cast the object you get out of a collections class back to the type of Object it is. This is not considered a band-aid; it is considered proper coding and using polymorphism to its fullest. (If you look through the source code of the JDK itself, you will see lots of usage of casting.) In Java 5.0 (a.k.a. v1.5), when creating a collection, such as an ArrayList, you can predefine what type of Object the ArrayList will hold. This is called Generics, and as mentioned, it was added in v5.0. Then when you pull the Object out, it comes out as the type of Object you defined that collections class to hold. Ultimately, however, the ArrayList is still holding items of type Object and a cast is still being done. When you compile the code in Java 5, it "inserts" all the appropriate casting for you in the final bytecode. The main advantage is that you eliminate the Chance of a ClassCastException since the validity of the casting is checked at compile time rather then runtime.

I won't get into the details of Generics here, since I do not know what version of Java you are using (or are required to use), plus it's a big topic. You can Google it to find some articles or tutorials on it, and then ask some additional question on it if that is the route you want to go.

I hope that helps.

[ February 19, 2005: Message edited by: Mark Vedder ]
marc weber

Joined: Aug 31, 2004
Posts: 11343

Mark: Excellent response!

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
I agree. Here's the link:
subject: ArrayList - how to store objects of certain kind
jQuery in Action, 3rd edition