我想创建一个具有三个int和一个String的自定义对象,并将该对象存储在arrayList中,但是我似乎遇到了问题,并且无法在线找到确切的文档。我在fac.add上遇到错误。这是代码

**对代码进行了一些更改

package facility;
import dal.DataAccess;

public class FacilityInfo implements Facility {

private int buildingNo, roomNo, capacity;;
private String type; //classroom, conference room, office, etc.

FacilityInfo(){}//default constructor

FacilityInfo(int b, int r, int c, String t){
    this.buildingNo = b;
    this.roomNo = r;
    this.capacity = c;
    this.type = t;
}
package dal;
import java.util.*;

import facility.FacilityInfo;

public class DataAccess {
    List<FacilityInfo> fac = new ArrayList<FacilityInfo>();
    fac.add(new FacilityInfo (1,2,10,conference));//changed code here
}

最佳答案

这是由于两个主要原因。

首先,1,2,10,conference不是FacilityInfo对象。您不能将FacilityInfo的参数添加到List,而必须添加一个实际的对象。

其次,您不能在代码块之外有语句,并且当前正在直接在类主体中调用fac.add(...);

尝试类似的东西:

public class DataAccess {
    List<FacilityInfo> fac = new ArrayList<FacilityInfo>();

    public void initializeFac() {
        fac.add(new FacilityInfo(1,2,10,"conference"));
        // etc.
    }
}

07-24 20:12