由于某种原因,我无法从主类中的其余方法访问我在程序的主方法中创建的 arrayList。
public static void main(String[] args) {
//creating array list for vehicles
ArrayList<motorVehicle> vehicleList = new ArrayList<motorVehicle>();
getAndCheckMainSelection();
在我的对象的输入集合结束时,我去创建并添加值,它在 netbeans 中给出了一个错误,指出找不到变量,突出显示了vehicleList..
vehicleList.add(new car(wheels, engineSize, valueOfPS, date, serialNumber, doors, color));
最佳答案
vehicleList
是局部变量,您只能在声明它们的方法中访问局部变量,如果您想在另一个方法中使用 vehicleList
您应该将其声明为实例变量、类变量或将其作为参数传递。您可以在 docs 中阅读有关它们的更多信息。
关于java - 无法在方法中添加到 arrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27001697/