如何在java程序中插入并显示arraylist?

How to insert and display arraylist in java program?

这是程序的输出:

Enter your last name: Dela Cruz
Enter your first name: Juan
Enter your course: BSCS
Enter your year: 4th Year

显示数组列表

[0] - Dela Cruz | Juan | BSCS | 4th Year

Add More (y/n): if yes it will add more entry...

使用以下代码。

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class IO {
    public static void main(String[] args) {

        Scanner scn = new Scanner(System.in);

        String continueAdd = "y";
        List<Student> studentList = new ArrayList<>();

        Student student;
        while ("y".equalsIgnoreCase(continueAdd)) {
            student = new Student();
            System.out.println("Enter your last name:");
            student.lastName = scn.nextLine();
            System.out.println("Enter your first name:");
            student.firstName = scn.nextLine();
            System.out.println("Enter your course:");
            student.course = scn.nextLine();
            System.out.println("Enter your year:");
            student.year = scn.nextLine();

            studentList.add(student);

            System.out.println("Add More (y/n): ");
            continueAdd = scn.nextLine();
        }
        int i = 0;
        for(Student studentTemp : studentList){

            System.out.println("["+i+"] - " + studentTemp);
            i++;
        }

    }


}

class Student {
    String firstName;
    String lastName;
    String course;
    String year;

    @Override
    public String toString(){
        return lastName+" | "+ firstName +" | "+course+" | "+year;

    }

}

拥有一个 class 并使用对象可能会更好,但这里使用的是 List。

boolean flag = true;
List<String> list = new ArrayList<>();
int i = 0;
while (flag) {

    String lastname, firstname, code, year;
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter lastname");
    lastname = sc.nextLine();
    System.out.println("Enter firstname");
    firstname = sc.nextLine();
    System.out.println("Enter code");
    code = sc.nextLine();
    System.out.println("Enter year");
    year = sc.nextLine();
    list.add(i, lastname + " | " + firstname + " | " + code + " | " + year);
    System.out.println(list.get(i));
i++;
    System.out.println("add  more? y/n");
    if (!"y".equals(sc.next())) {
        flag = false;
    }
}

你需要:

public class Entries {
    private String lname;
    private String fname;
    private String course;
    private String year;

    public String getLname() {
        return lname;
    }
    public void setLname(String lname) {
        this.lname = lname;
    }
    public String getFname() {
        return fname;
    }
    public void setFname(String fname) {
        this.fname = fname;
    }
    public String getCourse() {
        return course;
    }
    public void setCourse(String course) {
        this.course = course;
    }
    public String getYear() {
        return year;
    }
    public void setYear(String year) {
        this.year = year;
    }


}

然后是您的主应用程序:

public class MainApp {
public static void main(String[] args) {
    List<Entries> students = new ArrayList<Entries>();
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter a student:(y/n)");
    String option = scan.nextLine();
    while(option.equalsIgnoreCase("y")||option.equalsIgnoreCase("yes")){
        Entries stud = new Entries();
        System.out.println("Enter student's first name");
        stud.setFname(scan.nextLine());
        System.out.println("Enter student's last name");
        stud.setLname(scan.nextLine());
        System.out.println("Enter student's course");
        stud.setCourse(scan.nextLine());
        System.out.println("Enter student's year");
        stud.setYear(scan.nextLine());
        students.add(stud);
        System.out.println("Enter another student:(y/n)");
        option = scan.nextLine();
    } 
    if(option.equalsIgnoreCase("n")||option.equalsIgnoreCase("no")){
        System.out.println("Do you want to dispay all students? (y/n");
        String display = scan.nextLine();
        if(display.equalsIgnoreCase("y")||display.equalsIgnoreCase("yes"))
        for (Entries entrie : students) {
            System.out.println(entrie.getLname()+" |"+
                    entrie.getFname()+" |"+
                    entrie.getCourse()+" |"+
                    entrie.getYear());
        } else {
            System.exit(0);;
        }
    } else {
        System.out.println("option not availabe");
        System.exit(0);
    }

}

使用此代码

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

public class hi {
    static String n,c,y;
    static String option="";
    static ArrayList al= new ArrayList();
    static Scanner s=new Scanner(System.in);
    public static void input(){


        System.out.println("Enter the name");
        n=s.next();
        System.out.println("Enter the course");
        c=s.next();
        System.out.println("Enter the year");
        y=s.next();



         al.add(n+"|"+c+"|"+y);



         System.out.println("Add More (y/n)");
            option=s.next();
            System.out.println(option);

            if(option.equals("y")|| option.equals("Y")){
                input();
            }
            else{

                System.out.println(al);
                //System.exit(0);
            }



        }

public static void main(String args[]) throws InstantiationException, IllegalAccessException{


    input();



}
}