问题
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: 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.
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
The method add(int, Student) in the type ArrayList is not applicable for the arguments (String, null, String, String)
回答1:
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
.
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
回答2:
You need to add the object of Student
class in your ArrayList
students.add(new Student(String, String, String, String))
回答3:
You cannot add multiple arguments to an ArrayList
, at least not in that form. It looks like what you really want to do is:
students.add(new Student(/*whatever info*/));
If you really want multiple elements in an ArrayList
, do something like this:
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));
来源:https://stackoverflow.com/questions/21201922/arraylist-with-multiple-arguments-user-input