带有多个参数的 ArrayList(用户输入) [英] ArrayList with multiple arguments (User input)

查看:36
本文介绍了带有多个参数的 ArrayList(用户输入)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ArrayList 根据用户输入创建一个应用程序.但是有一个问题我需要先了解一下.

I am trying to create a App base on user input with ArrayList. But there is some problem that I need to understand first.

PS:在进入用户输入之前,我想先尝试使用正常值.这只是一个粗略的草图,让我了解如何在数组列表中使用多个参数.

PS: I want to try with normal value first before going into user input. And this is just a rough sketch up to make me understand how to use multiple arguments in a arraylist.

这是学生

package test;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Student {
    private String name;
    private Date DOB;
    private String[] friend;
    private String school;

    public Student(String name, Date DOB, String[] friend, String school) {
        this.name = name;
        this.DOB = DOB;
        this.friend = friend;
        this.school = school;

    }

    public void display() {
        System.out.println("Name : " + name);
        SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        System.out.println("Released Date : " + df.format(DOB));
        System.out.println("Best Friend : " + friend[0] + ", " + friend[1]);
        System.out.println("Director : " + school);

    }

    public String getName() {
        return name;
    }

}

这是学生应用程序

package test;

import java.util.ArrayList;

public class StudentApp {
    private ArrayList<Student> students = new ArrayList<Student>();

    public void start() {
        addStudent();
    }

    public void addStudent() {
        System.out.println("- Add Student Info -");
        students.add(/* Need to ask the user to input their information like their name,DOB,friend,school*/);
    }

}

如果需要多个参数,我如何将值添加到数组列表中?(字符串、日期、字符串[]、字符串).因为当我尝试将一些带有所需的正确参数的值添加到 student.add 中时,它会显示一个错误说

how can I add in values to the arraylist if there is multiple arguments needed? (String, Date, String[], String). Because when I tried adding some value with the correct arguments needed into the students.add, it will show me an error saying

ArrayList 类型中的 add(int, Student) 方法不是适用于参数 (String, null, String, String)

The method add(int, Student) in the type ArrayList is not applicable for the arguments (String, null, String, String)

推荐答案

您要做的是在列表中添加错误的元素.

What you are trying to do is add wrong elements in a list.

students的列表是Student的列表.所以如前所述,students.add() 只接受 Student 类型的对象.

The list you students is a list of Student. So as pointed out earlier, students.add() would only accept objects of type Student.

您必须执行以下操作:

System.out.println("- Add Student Info -");
String name = ""; //Get name from user
Date dob = new Date(); //Get DOB from user
String[] friends = new String[]{"Friend1", "Friend2"}; //Get a list of friends from user
String school = ""; //Get school from user

students.add(new Student(name, dob, friends, school));
//OR students.add(0, new Student(name, dob, friends, school)); //Replace 0 with any index

这篇关于带有多个参数的 ArrayList(用户输入)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆