java - create new object in arraylist with attributes -
i new java , starting work arraylists
. trying create arraylist
students. each student has different attribute associated them (name, id
). trying figure out how add new student object attributes. here have:
arraylist < student > studentarray; public student(string name, int id) { this.fname = name; this.stid = id; } public stromg getname() { return fname; } public int getid() { return stid; } public boolean setname(string name) { this.fname = name; return true; } public boolean setidnum(int id) { this.stid = id; return true; }
what need following:
import java.util.*; class teststudent { public static void main(string args[]) { list<student> studentlist= new arraylist<student>(); student tempstudent = new student(); tempstudent.setname("rey"); tempstudent.setidnum(619); studentlist.add(tempstudent); system.out.println(studentlist.get(0).getname()+", "+studentlist.get(0).getid()); } } class student { private string fname; private int stid; public string getname() { return this.fname; } public int getid() { return this.stid; } public boolean setname(string name) { this.fname = name; return true; } public boolean setidnum(int id) { this.stid = id; return true; } }
Comments
Post a Comment