मैं एक 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?
}
}
किसी भी मदद या स्पष्टीकरण काफी सराहना की है ।