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

查看:78
本文介绍了具有多个参数的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;
    }

}

这是StudentApp

This is the StudentApp

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*/);
    }

}

如果需要多个参数,如何将值添加到arraylist中?(字符串,日期,字符串[],字符串).因为当我尝试将一些具有正确参数的值添加到students.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)不是适用于参数(字符串,空值,字符串,字符串)

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.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.

您将必须执行以下操作:

You will have to do something like:

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天全站免登陆