我想向中添加一个对象ArrayList,但是每次我向ArrayList具有3个属性的新对象添加时objt(name, address,
contact),都会出现错误。
import java.util.ArrayList;
import java.util.Scanner;
public class mainClass {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
System.out.println("Plz enter Name : ");
String name = input.nextLine();
System.out.println("Plz enter Address : ");
String address = input.nextLine();
System.out.println("Plz enter ContactNo : ");
String contact = input.nextLine();
ArrayList Contacts = new ArrayList();
Data objt = new Data();
Contacts.add(objt.Data(name, address, contact));
}
}
在这里,Data我正在尝试创建其对象并将其传递给的类ArrayList。
public class Data {
private String name = "";
private String address = "";
private String cell = "";
public void Data(String n, String a, String c){
name = n;
address = a;
cell = c;
}
public void printData(){
System.out.println("Name\tAddress\tContactNo");
System.out.println(name + "\t" + address + "\t" + cell);
}
}