Java 带有多个参数的 ArrayList(用户输入)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21201922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ArrayList with multiple arguments (User input)
提问by user2693620
I am trying to create a App base on user input with ArrayList. But there is some problem that I need to understand first.
我正在尝试使用 ArrayList 根据用户输入创建一个应用程序。但是有一些问题我需要先了解。
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.
PS:在进入用户输入之前,我想先尝试使用正常值。这只是一个粗略的草图,让我了解如何在数组列表中使用多个参数。
This is the Student
这是学生
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;
}
}
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*/);
}
}
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
如果需要多个参数,如何将值添加到数组列表中?(字符串、日期、字符串[]、字符串)。因为当我尝试在 student.add 中使用所需的正确参数添加一些值时,它会向我显示一条错误消息
The method add(int, Student) in the type ArrayList is not applicable for the arguments (String, null, String, String)
ArrayList 类型中的 add(int, Student) 方法不适用于参数 (String, null, String, String)
采纳答案by Sanket Meghani
What you are trying to do is add wrong elements in a list.
您要做的是在列表中添加错误的元素。
The list you students
is a list of Student
. So as pointed out earlier, students.add()
would only accept objects of type Student
.
你的列表students
是一个列表Student
。所以如前所述,students.add()
只接受类型为 的对象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
回答by user2693620
You need to add the object of Student
class in your ArrayList
您需要Student
在您的ArrayList
students.add(new Student(String, String, String, String))
回答by Justin
You cannot add multiple arguments to an ArrayList
, at least not in that form. It looks like what you really want to do is:
您不能将多个参数添加到ArrayList
,至少不是那种形式。看起来你真正想做的是:
students.add(new Student(/*whatever info*/));
If you really want multiple elements in an ArrayList
, do something like this:
如果你真的想要一个中的多个元素ArrayList
,请执行以下操作:
ArrayList<Object[]> list = new ArrayList<>(); // or if you know all of them are Strings, String[]
list.add(new Object[]{arg1, arg2, arg3, arg4});
Better than that is to create a special class, something like:
比这更好的是创建一个特殊的类,例如:
class Data{
private String[] data;
public Data(String s1, String s2, String s3, String s4){
data = {s1, s2, s3, s4};
}
public String getElement(int index){
return data[index];
}
}
Then, use it like this:
然后,像这样使用它:
ArrayList<Data> list = new ArrayList<>();
list.add(new Data(arg1, arg2, arg3, arg4));