如何从arraylist输出值

时间:2014-03-10 12:26:34

标签: java arraylist constructor

我的节目是:

public class Vehicle {

    String colors[] = {"Red", "White", "Blue", "black", "Silver"};
    private String name;
    private String model;
    double cost_price;
    int year;
    int quantity;
    double selling_price;
    String registration_no;

    Vehicle(String name, String model, int year, double cost_price) {
        this.name = name;
        this.model = model;
        this.year = year;
        this.cost_price = cost_price;
        this.quantity = 0;
        this.selling_price = 0;
    }

    Vehicle(String name, String model, int quantity, double cost_price, String registration_no) {
        this.name = name;
        this.model = model;
        this.quantity = quantity;
        this.cost_price = cost_price;
        this.selling_price = cost_price;
        this.registration_no = registration_no;
    }

    public void setname(String name) {
        this.name = name;
    }

    public void setmodel(String model) {
        this.model = model;
    }

    public String getname() {
        return name;
    }

    public String getmodel() {
        return model;
    }

    public double calSellingPrice(int markup) {

        return cost_price + ((cost_price * markup) / 100);
    }

    public int updateQuantity(int amount) {

        return quantity;
    }

    public String tostring() {
    }
    /**
     * @param args the command line arguments
     */
}

class Vehicletest {

    public static void main(String[] args) {
        // TODO code application logic here

        Vehicle v1 = new Vehicle("Ferrari", "Enzo", 2011, 250000);
        v1 = new Vehicle("Ferrari", "Enzo", 6, 250000, "PK07LVD");
        Vehicle v2 = new Vehicle("Audi", "R8", 2008, 550000);
        v2 = new Vehicle("Audi", "R8", 9, 550000, "ADDY104");
        Vehicle v3 = new Vehicle("RangeRover", "Evoque", 2010, 578000);
        v3 = new Vehicle("RangeRover", "Evoque", 3, 578000, "OHZ2692");
        Vehicle v4 = new Vehicle("Lamborghine", "Aventador", 2013, 980000);
        v4 = new Vehicle("Lamborghine", "Aventador", 5, 980000, "BB03813");
        Vehicle v5 = new Vehicle("Porsche", "Carrera", 2006, 675000);
        v5 = new Vehicle("Porsche", "Carrera", 15, 675000, "BD51SMR");
        ArrayList uwiMotors = new ArrayList();

        uwiMotors.add(v1);
        uwiMotors.add(v2);
        uwiMotors.add(v3);
        uwiMotors.add(v4);
        uwiMotors.add(v5);
        System.out.println(v1.colors);
        System.out.println(v1.cost_price);
        System.out.println(v1.quantity);
        System.out.println(v1.registration_no);
        System.out.println(v1.selling_price);
        System.out.println(v1.year);

        for (int i = 0; i < uwiMotors.size(); i++) {
            System.out.println(uwiMotors.get(i));


        }
    }
}

我做错了吗?

我对两个构造函数感到困惑。因为如果我将引用变量初始化为两个构造函数,那么值是否会被更改?如何从Arraylist输出值? 我是否必须覆盖toString()方法?如果是,那我该怎么做?

5 个答案:

答案 0 :(得分:1)

您的代码中存在多个问题。首先,您应该阅读constructors。你只得到控制台上的哈希,因为你告诉Java输出一个对象。由于您的对象没有正确的toString方法,因此无法知道输出的外观。

三个提示:

Vehicle(String name, String model, int year, double cost_price) {
   //as you have it above
}
//add year to this constructor
Vehicle(String name, String model, int year, int quantity, double cost_price, String registration_no) {
    this(name, model, year, cost_price);
    this.quantity = quantity;
    this.selling_price = cost_price;
    this.registration_no = registration_no;
}

@Override
public String toString() { //camelCase is important!
    return this.name + ", " + this.model; //adopt this part as you want it to
}

最后:

    //Vehicle v1 = new Vehicle("Ferrari", "Enzo", 2011, 250000);
    //you cannot call two different constructors
    Vehicle v1 = new Vehicle("Ferrari", "Enzo", 2011, 6, 250000, "PK07LVD");

答案 1 :(得分:1)

  

我对两个构造函数感到困惑。因为如果我将引用变量初始化为两个构造函数,则不会更改值吗?

是的,实际上您正在用新的对象替换第一个对象,因此v1-v5的初始化可以简化为以下内容:

Vehicle v1;
v1 = new Vehicle("Ferrari", "Enzo", 6, 250000, "PK07LVD");
Vehicle v2;
v2 = new Vehicle("Audi", "R8", 9, 550000, "ADDY104");
Vehicle v3;
v3 = new Vehicle("RangeRover", "Evoque", 3, 578000, "OHZ2692");
Vehicle v4;
v4 = new Vehicle("Lamborghine", "Aventador", 5, 980000, "BB03813");
Vehicle v5;
v5 = new Vehicle("Porsche", "Carrera", 15, 675000, "BD51SMR");

你应该写一个新的构造函数,你可以在其中传递初始化所需的所有参数,或者使用其中一个构造函数,然后使用setter来处理剩下的内容。

  

如何从Arraylist输出值?我是否必须覆盖toString()方法?如果是,那我该怎么做?

有很多方法可以解决这个问题。覆盖toString将是最简单的。以下是你将如何做到这一点:

public String tostring() {
    return "name=" + this.name + ", model=" + this.model + ", cost_price=" + this.cost_price + ", year=" + this.year + ", quantity=" + this.quantity + selling_price=" + this.selling_price+ ", registration=" + registration;
}

随意修改此选项以仅包含您需要的字段。

然后你可以通过你的arraylist循环来编写它,这样System.out就像这样:

for (Vehicles v : uwiMotors)
{
    System.out.println(v);
}

答案 2 :(得分:1)

你的代码会给你一个'哈希'列表(它实际上不是哈希码),因为这是toString的默认实现。

代码的基本布局应该是这样的(我删除了一些字段以保持长度,并使用Apache commons-lang来帮助equalshashCodetoString方法):

public class Vehicle 
{
  private String name;
  private String model;
  private String registration;

  public Vehicle(String name, String model) 
  {
    this.name = name;
    this.model = model;
  }

  public Vehicle(String name, String model, String registration) 
  {
    this(name, model);
    this.registration = registration;
  }

  public String getName() 
  {
    return name;
  }

  public String getModel() 
  {
    return model;
  }

  public String getRegistration() 
  {
    return registration;
  }

  public String getRegistration() 
  {
    return registration;
  }

  @Override
  public boolean equals(Object obj)
  {
    return this == obj || obj instanceof Vehicle &&
      new EqualsBuilder()
        .append(getName(), ((Vehicle)obj).getName())
        .append(getModel(), ((Vehicle)obj).getModel())
        .append(getRegistration(), ((Vehicle)obj).getRegistration())
        .isEquals();
  }

  @Override
  public int hashCode()
  {
    return new HashCodeBuilder()
      .append(getName())
      .append(getModel())
      .append(getRegistration())
      .toHashCode();
  }

  @Override
  public String toString()
  {
    return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
      .append("name", getName())
      .append("model", getModel())
      .append("registration", getRegistration())
      .toString();
  }   
}

答案 3 :(得分:0)

暂时让我们忽略代码中的错误编码做法。我希望以下代码是您正在寻找的?

      for (int i = 0; i < uwiMotors.size(); i++) {
                    Vehicle vehicle  = (Vehicle)uwiMotors.get(i);
//                 System.out.println(vehicle.cost_price+","+vehicle.quantity);
System.out.println(vehicle);// If vehicle object overrides the toString method


                }

答案 4 :(得分:0)

是的,两个构造函数将更改变量的值,只是因为您只是创建新对象并将其分配给变量。正如@ Christian Kullmann所写的你应该考虑使用'this(arg goes here)'来调用另一个构造函数。

打印Arraylist元素您只需循环迭代它们并逐个打印(如您所做)。但是在覆盖toString的情况下你只需像这样写

public String toString(){
 return "some text";
}