• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help with an ArrayList  RSS feed

 
Steven Back
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!