I am new to Java and I am starting to work with ArrayLists
. What I am trying to do is create an ArrayList
for students. Each student has differen
You instantiate a Student
object by passing the appropriate values to the constructor.
Student s = new Student("Mr. Big", 31);
You place elements into an ArrayList
(or List
) by using the .add()
operator.*
List studentList = new ArrayList();
studentList.add(s);
You retrieve user input via the use of a Scanner
bound to System.in
.
Scanner scan = new Scanner(System.in);
System.out.println("What is the student's name?");
String name = scan.nextLine();
System.out.println("What is their ID?");
int id = scan.nextInt();
You repeat this with a loop. That portion shall be left as an exercise to the reader.
*: There are other options, but add()
simply adds it to the end, which is typically what you want.