java - Number of player objects more than 30 -
lets have array list of players , want players above age of 30. players have usual attributes name , age , getname , getage methods. i'm new java here appreciated. thanks.
for(player p: players){ if(p.getage() > 30){ system.out.println(p.getname()); }}
here possible solution you. assumption: have class player , can read players , add in allplayers arraylist.
import java.util.*; public class game { //holds list of players. arraylist<player> allplayers = new arraylist<player>(); //holds list of players older 30 years. arraylist<player> olderthan30 = new arraylist<player>(); public game() { //read players list , fill in above arraylist players have. //for example, allplayers = getallplayers(); can use io methods read external files. //get players older 30 years of age calling method get30olderplayers(). olderthan30 = get30olderplayers(); } private arraylist<player> get30olderplayers() { //an arraylist save player list return. arraylist<player> olderthan30 = new arraylist<player>(); //loops through players 1 one. (player p : allplayers) { //if player's age more 30.... if(p.getage() > 30) { //...add arraylist olderthan30. olderthan30.add(p); } } //at end return arraylist holds players older 30. return olderthan30; } }
good luck.
Comments
Post a Comment