(ภาษาจาวา language)ว่าฉันสามารถพิมพ์เป็นวัตถุของข้อมูลเมื่อตอนที่วัตถุมันอยู่ ArrayList? [ที่ซ้ำกัน]

0

คำถาม

ฉันมี ArrayList อยู่ในหลักและฉันมีเรียนกับ constructor ข้างในมันและวิธีการที่จะพิมพ์ข้อมูลออกมา ฉันเพิ่มเป็นคนใหม่วัตถุกับข้อมูลใหม่ตอนที่โทรมาเพิ่มมันไป 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?
        
    }
}

ช่วยหรือช่วยทำให้ชัดเจนคือ greatly นเกียรติอย่างยิ่ง

arraylist java printing
2021-11-24 04:07:52
3
1

คุณต้องเปลี่ยนแปลงความหมายของ listOfObj จาก:

ArrayList listOfObj = new ArrayList();

ไปยัง:

ArrayList<student> listOfObj = new ArrayList<>();

คนแรกจะจะสร้าง ArrayList ของ Object คลาสวัตถุ.

ที่สองที่จะสร้าง ArrayList ของ student คลาสวัตถุ.

อีกสองสามปัญหาในที่ทำงานของคุณรหัส:

  1. ตั้งแต่คุณเป็นการอ่านชื่อของใช้ nextLineคุณอาจจะต้องข้ามใหม่หลังจากการอ่านคลอดปีเหมือน:
...
int birthYear = sc.nextInt();
sc.nextLine();  // Otherwise in the next loop iteration, it will skip reading input and throw some exception
...
  1. คุณเลือกตัวเลือกสำหรับการเป็นกรรมการนักเรียนการแสดงแต่ตัวเลือกนี้หาก 1 สร้างดัชนีค้นหาแล้ว ArrayList ร้าน 0 สร้างดัชนีค้นหาดังนั้นคุณควรจะเปลี่ยนเส้นไป sc.nextInt() - 1:
int chosenStudent = sc.nextInt() - 1; // Choose a number that correlates to a student
  1. Scanner อาจจะโยนข้อยกเว้นในกรณีที่คุณป้อนตัวอย่างเช่นเป็นข้อความแทนที่จะเป็น int ได้. งั้นทำให้แน่ใจว่าคุณกำลังจัดการข้อยกเว้นเหมาะสมโดยใช้ try-catch บล็อก.
2021-11-24 04:26:42
1
  • คุณเปลี่ยน ArrayList defination และเพิ่ม toString()ในของคุณ studen ห้องเรียน
  • และเพื่อพิมพ์ทั้งหมดนักเรียนวัตถุ insted ของใช้สำหรับเรื่องใช้แค่ หนึ่ง sop.

แฟนเก่าขอ:-

import java.util.*;

class Student {
    private String name;
    private int age;
    private int birthYear;

    public Student() {
        super();
    }

    public Student(String name, int age, int birthYear) {
        super();
        this.name = name;
        this.age = age;
        this.birthYear = birthYear;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getBirthYear() {
        return birthYear;
    }

    public void setBirthYear(int birthYear) {
        this.birthYear = birthYear;
    }

    @Override
    public String toString() {
        return "Student [age=" + age + ", birthYear=" + birthYear + ", name=" + name + "]";
    }

}

public class DemoArrayList {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<Student>();

        Scanner scan = new Scanner(System.in);

        int n = scan.nextInt();

        for (int i = 0; i < n; i++) {
            scan.nextLine();
            String name = scan.nextLine();
            int age = scan.nextInt();
            int birthYear = scan.nextInt();
            list.add(new Student(name, age, birthYear));
        }

        System.out.println(list);
    }
}

โอ/P:-

2
joy 
10
2003
jay
20
2005
[Student [age=10, birthYear=2003, name=joy], Student [age=20, birthYear=2005, name=jay]]
2021-11-24 04:14:02

สวัสดี,คุณสามารถแก้ไขคำตอบของคุณและเพิ่มการหัสเช่นกันดังนั้นปฏิบัติการสามารถเข้าใจมันดีขึ้นเหรอ?
kiner_shah

โอเครอก่อบางครั้ง
Batek'S

ตอนนี้เห็นมันรหัสมอส
Batek'S
0

คุณไม่สามารถประกาศประเภทของ ArrayList ตอนเริ่มใช้งาน listOfObj แล้ว names ใน ArrayList.

นี่คือทางที่ถูกต้องจะประกาศ ArrayList:

ArrayList<type> list = new ArrayList();

และอีกปัญหาคือตอนที่คุณดึงข้อมูลจาก ArrayList. ตัวแปร chosenStudent ดูจากตำแหน่งของผู้ใช้และให้ข้อมูลจาก listOfObj แต่ดัชนีคือเริ่มจาก 0 ไม่ 1.

ตัวอย่างเช่นข้อมูลของคุณคือ 2 งั้น,ArrayList ได้ข้อมูลจากตำแหน่งที่สองแต่ดัชนีของคุณคือเริ่มจาก 1 ขณะพิมพ์ข้อมูล คุณต้องเอา chosenStudent - 1 เพื่อให้ถูกต้องข้อมูลออกมา

listOfObj.get(chosenStudent - 1).printStudentInformation() // chosenStudent = 2 - 1 = 1 

ที่นี่ลงของฉันคือรหัส:

ArrayList<student> listOfObj = new ArrayList<>();
ArrayList<String> names = new ArrayList<>();
Scanner sc = new Scanner(System.in);
    for(int i = 0; i < 3; i++){
        System.out.println("New Student Information:");
        String name = sc.next();
        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));
     }
     int chosenStudent = sc.nextInt();
        
     listOfObj.get(chosenStudent - 1).printStudentInformation();
2021-11-24 04:41:01

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่