• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Need help with an ArrayList

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic