0

I have created 6 college objects that I add to an ArrayList of college objects. I am able to print each object no problem. I would like to know if it is possible to print out certain instance variables of the objects(the name in this case) I tried to use the getter method in different ways but was getting errors. Any advice would be helpful, thanks! P.S. the method that is at the bottom wasn't working when called upon when declared in the College class? Why?

import java.util.ArrayList ;

public class TheArrayList

{

    public static void main(String[] args)
{

        ArrayList<College> schoolList = new ArrayList<College>(3);

        College schoolOne = new College("Palomar", "San Marcos", 26000, 1943) ;
        College schoolTwo = new College("Mira Costa", "Oceanside", 19000, 1934) ;
        College schoolThree = new College("Grossmont", "El Cajon", 18000, 1968) ;
        College schoolFour = new College("SaddleBack", "Mission Viejo", 26000, 1934) ;
        College schoolFive = new College("Mesa", "San Diego", 25000, 1958) ;
        College schoolSix = new College("Southwestern", "Chula Vista", 27000, 1961) ;



        schoolList.add(schoolOne) ;
        schoolList.add(schoolTwo) ;
        schoolList.add(schoolThree) ;
        schoolList.add(1, schoolFour) ;
        schoolList.add(2, schoolFive) ;


        showSchools(schoolList) ;

    }
//Method in The Array List
    private static void showSchools(ArrayList<College> list){
        System.out.println(list) ;
    }


}




    public class College
{
    private String name = "" ;
    private String city = "" ;
    private int students = 0 ;
    private int established = 1900 ;

        public College(String name, String city, int students, int established){
            this.name = name ;
            this.city = city ;
            this.students = students ;
            this.established = established ;

        }

//Getters
    public String getName(){
        return name ;
    }

    public String getCity(){
        return city ;
    }

    public int getStudents(){
        return students ; 
    }

    public int getestablished(){
        return established ;
    }
//Setters
    public void setName(String newName){
        name = newName ;
    }

    public void setCity(String newCity){
        city = newCity ;
    }

    public void setStudents(int newStudents){
        students = newStudents ;
    }

    public void setEstablished(int newEstablished ){
        established = newEstablished ;
    }

    public String toString() {
        return name + " " + city + " " + students + " " + established ; 
    } 
}
Ryan Corry
  • 11
  • 8
  • `private static void showSchools(ArrayList list){ for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i).toString()); }` – John Joe Apr 19 '18 at 07:01
  • It calls the toString automatically, dont know why, but what if I wanted just the name variable itself! – Ryan Corry Apr 19 '18 at 07:10
  • `for (College d : list) { System.out.println("name is " + d.getName()); }` – John Joe Apr 19 '18 at 07:32

0 Answers0