| Author |
Need help with an ArrayList
|
Steven Back
Greenhorn
Joined: Feb 01, 2012
Posts: 1
|
|
|
Hi, I have a program that I am writing. It allows the user to track player stats through a series of input and then stores those stats to an arraylist. Currently, I have added two two more sports so now I am up to three and I need to store all of these stats into an arraylist. I need to do it with polymorphism and not by having three seperate arraylists in the main method. How would I go about changing this so I am only using one array list.
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 11642
|
|
Hello Steven, welcome to the Ranch!
I think what they mean with that you have to do it with polymorphism is this: you create a superclass, for example a class named Sport, and then you create subclasses of that for your specific kinds of sports. For example Baseball extends Sport, etc.
Then you create one ArrayList which holds elements of type Sport: List<Sport> sports = new ArrayList<Sport>();. Because Baseball is a Sport, you can put Baseball objects into this list, as well as any other objects of which the class extends Sport.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
 |
|
|
subject: Need help with an ArrayList
|
|
|