가 ArrayList 에서 주요하고 내가 있는 클래스를 생성자 그 안에 메서드를 인쇄하는 데이터입니다. 추가로 새로운 개체로,새로운 정보를 호출할 때,그리고 그것을 추가 ArrayList 에 그것을 유지합니다. 내가 무슨 힘든 시간이 구문을 인쇄하는 정보입니다. 나는 그것을 시도했으로 일반 편지를 사용하 ArrayList. 필요를 얻을 수 있 인덱스의 특정 개체에 인쇄하는 개체의 정보입니다. 예를 들어,아래 코드는 지난 몇 줄:
import java.util.ArrayList;
import java.util.Scanner;
public class student{
String name;
int age;
int birthYear;
public student(String name, int age, int birthYear){
this.name = name;
this.age = age;
this.birthYear = birthYear;
}
public void printStudentInformation(){
System.out.println(name);
System.out.println(age);
System.out.println(birthYear);
}
}
public class Main{
public static void main(String[] args){
ArrayList listOfObj = new ArrayList();
ArrayList names = new ArrayList();
Scanner sc = new Scanner(System.in);
for(int i = 0; i < 3; i++){
System.out.println("New Student Information:"); // Three student's information will be saved
String name = sc.nextLine();
int age = sc.nextInt();
int birthYear = sc.nextInt();
student someStudent = new student(name, age, birthYear);
listOfObj.add(someStudent);
names.add(name);
}
System.out.println("What student's information do you wish to view?");
for(int i = 0; i < names.size(); i++){
System.out.println((i + 1) + ") " + names.get(i)); // Prints all students starting from 1
}
int chosenStudent = sc.nextInt(); // Choose a number that correlates to a student
// Should print out chosen student's object information
listOfObj.get(chosenStudent).printStudentInformation(); // This is incorrect, but I would think the syntax would be similar?
}
}
어떤 도움 또는 설명이 크게 감사합니다.