24

我想将一个对象添加到 an ArrayList,但是每次我将一个新对象添加到ArrayList具有 3 个属性的 an 时: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<Data> Contacts = new ArrayList<Data>();
        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);
        }
}
4

7 回答 7

47

new创建对象时需要使用操作符

Contacts.add(new Data(name, address, contact)); // Creating a new object and adding it to list - single step

要不然

Data objt = new Data(name, address, contact); // Creating a new object
Contacts.add(objt); // Adding it to the list

并且您的构造函数不应包含void. 否则,它将成为您班级中的一种方法。

public Data(String n, String a, String c) { // Constructor has the same name as the class and no return type as such
于 2013-10-27T10:22:56.770 回答
1

试试这个:

Data objt = new Data(name, address, contact);
Contacts.add(objt);
于 2013-10-27T11:00:28.020 回答
1
Contacts.add(objt.Data(name, address, contact));

这不是调用构造函数的完美方式。构造函数在对象创建时自动调用。如果没有构造函数 java 类创建自己的构造函数。

正确的方法是:

// object creation. 
Data object1 = new Data(name, address, contact);      

// adding Data object to ArrayList object Contacts.
Contacts.add(object1);                              
于 2016-01-06T05:40:44.723 回答
0

您必须在此处使用 new 运算符进行实例化。例如:

Contacts.add(new Data(name, address, contact));
于 2014-05-30T11:57:45.587 回答
0

调用“Data()”方法时不会返回数据对象。

将“数据()”方法更改为:

    public Data Data(String n, String a, String c){
        name = n;
        address = a;
        cell = c;
        return this;
    }
于 2021-11-06T12:09:27.137 回答
0

将数据传递给 List 类的最佳方式,

private final List<electronic> Electronic = new ArrayList<electronic>();

public List<electronic> getElectronic() {
    return Electronic;
}

// creating list to store and display the Electronic items
public void StoreAndDisplayElectronic() {
    String[] EelctronicName = { "Laptop", "Television", "ElectricBike" };
    String[] ElectronicID = { "091", "202", "151" };
    Double[] EPrice = { 340.00, 280.00, 600.00 };
    int[] EAmt = { 5, 12, 4 };

    for (int i = 0; i < ElectronicID.length; i++) {
        this.Electronic.add(new electronic(EelctronicName[i], ElectronicID[i], EPrice[i], EAmt[i]));
    }
}
于 2021-08-14T06:03:40.690 回答
-1

将日期更改为括号之间的对象

于 2015-02-22T03:25:19.893 回答