Java 将对象从另一个类添加到 ArrayList

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19743126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 19:52:15  来源:igfitidea点击:

Adding objects to ArrayList from another class

javaarraylist

提问by AnjunaTom

I'm very new to programming and currently trying to write a car showroom application. I've got a vehicle class, showroom class and I'm trying to use a showroom driver for the input.

我对编程很陌生,目前正在尝试编写汽车陈列室应用程序。我有一个车辆类,陈列室类,我正在尝试使用陈列室驱动程序进行输入。

I'm having problems adding vehicle objects to my arraylist. Could someone point me in the correct direction.

我在将车辆对象添加到我的数组列表时遇到问题。有人可以指出我正确的方向。

My code:

我的代码:

public class Vehicle {

    private String manufacturer;
    private String model;
    private String custName;
    private String vin;
    private String dateMan;
    private String dateSold;
    private Boolean sold;
    private char tax;
    private double cost;

    public Vehicle(String a, String b, String c, String d) {
        manufacturer = a;
        model = b;
        vin = c;
        dateMan = d;
    }

    public String toString() {
        String s = "Manufacturer: " + manufacturer + "  Model: "
                + model + "  vin: " + vin + "Date Manufactured:" + dateMan
                + "Cost: " + cost;
        return s;
    }

    public void buyVehicle(String a, String b) {  //buy method for the vehicle
        a = dateSold;
        b = custName;
        sold = true;
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public String getModel() {
        return model;
    }

    public String getCustName() {
        return custName;
    }

    public String getVin() {
        return vin;
    }

    public String getDateMan() {
        return dateMan;
    }

    public String getDateSold() {
        return dateSold;
    }

    public Boolean getSold() {
        return sold;
    }

    public char getTax() {
        return tax;
    }

    public double getCost() {
        return cost;
    }
}

.

.

import java.util.ArrayList;

public class Showroom {

    private ArrayList<Showroom> theVehicles;

    public Showroom() {
        theVehicles = new ArrayList<Showroom>();
    }

    public boolean addVehicle(Showroom newVehicle) {
        theVehicles.add(newVehicle);
        return true;
    }
}

.

.

import java.util.*;

public class ShowroomDriver {

    public static void main(String[] args) {
        Vehicle v1 = new Vehicle("renault", "clio", "12", "290890");
        Showroom.addVehicle(v1);
    }
}

Basically, I'm confused to how I add vehicle objects to the arraylist within the showroom class. If anyone could point me in the right direction I would really appreciate it.

基本上,我对如何将车辆对象添加到陈列室类中的数组列表感到困惑。如果有人能指出我正确的方向,我将不胜感激。

Thanks in advance.

提前致谢。

采纳答案by Aubin

You have to instantiate the class Showroom to use its properties and methods

您必须实例化 Showroom 类才能使用其属性和方法

The collection theVehicles is of Vehicle not Showroom.

集合 theVehicles 是 Vehicle 而不是 Showroom。

package cars;

import java.util.ArrayList;
import java.util.List;

public class Showroom {

   private final List<Vehicle> theVehicles = new ArrayList<>();

   public boolean addVehicle( Vehicle newVehicle ) {
      theVehicles.add( newVehicle );
      return true;
   }

   public static void main( String[] args ) {
      final Showroom showroom = new Showroom();
      final Vehicle v1 = new Vehicle( "renault", "clio", "12", "290890" );
      showroom.addVehicle( v1 );
   }
}

in Vehicle class, a mistake around '=' operator, I suppose you want to memorize the sold value and the customer name:

在 Vehicle 类中,围绕 '=' 运算符的错误,我想您想记住销售价值和客户名称:

public void buyVehicle( String a, String b ) { // buy method for the vehicle
   dateSold = a;
   custName = b;
   sold = true;
}

回答by Paul Samsotha

I Think this

我认为这

private ArrayList <Showroom> theVehicles;

Shoulde be this

应该是这个

private ArrayList <Vehicle> theVehicles;
theVehicles = new ArrayList <Vehicle> ();

And this

和这个

public boolean addVehicle( Showroom newVehicle )

Should be

应该

public boolean addVehicle( Vehicle newVehicle )

Don't you want an ArrayListof Vehicles and not ShowRooms?

你不想要一个ArrayListof Vehicles 而不是ShowRooms 吗?

回答by slessans

Your problem is that you declared an ArrayList of ShowRoom objects, but what you want is an ArrayList of Vehicle objects.

您的问题是您声明了 ShowRoom 对象的 ArrayList,但您想要的是 Vehicle 对象的 ArrayList。

private ArrayList<Vehicle> theVehicles;

public boolean addVehicle(Vehicle v) {
    theVehicles.add(v);
    return true;
}