将对象添加到arraylist

时间:2015-04-17 23:31:46

标签: java arraylist

public ArrayList<Vehicle> getVehiclesSoldRecently()
{

    ArrayList<Vehicle> a=new ArrayList<>();
    ListIterator<Vehicle> it= list.listIterator();

    while(it.hasNext())
    {

        Vehicle b=it.next();

        if(b.getAgeSolding()<=14)
        {
            a.add(b);
        } 
    }
    return a;

}

我创建了这个方法是为了制作过去14天内售出的车辆的ArrayList,我遇到了问题。方法getAgeSolding完美无缺,并且在我应用此方法的情况下,if中的条件也得到了验证。

但是如果条件得到验证,为什么没有b添加到ArrayList中?我在每种情况下都获得一个空的ArrayList。为什么呢?

车辆类

package model;
import java.time.*;
import java.time.temporal.ChronoUnit;


  public class Vehicle 
  {
 private String manufacturer, model, VIN;
private LocalDate datemanuf, datesold;
private Customer cust;
private boolean sold;
private final char taxband;
private final int price;

public Vehicle(String manufacturer, String model, String VIN, LocalDate datemanuf, char taxband, int price) 
{
    this.manufacturer = manufacturer;
    this.model = model;
    this.VIN = VIN;
    this.datemanuf = datemanuf;
    this.taxband = taxband;
    this.price = price;
    this.cust=null;
    this.datesold=null;
    this.sold=false;
}

public String getManufacturer() 
{
    return manufacturer;
}

public String getModel() 
{
    return model;
}

public Customer getCust() 
{
    return cust;
}

public String getVIN() 
{
    return VIN;
}

public LocalDate getDatemanuf() 
{
    return datemanuf;
}

public LocalDate getDatesold() 
{
    return datesold;
}

public boolean isSold() 
{
    return sold;
}

public char getTaxband() 
{
    return taxband;
}

public int getPrice() 
{
    return price;
}

public void buy(Customer cust, LocalDate datesold)
{
    this.cust=cust;
    this.datesold=datesold;
    this.sold =true;
}

public long getAgeOfTheVehicle()
{
    LocalDate Now=LocalDate.now();

    long a=datemanuf.until(Now,ChronoUnit.WEEKS);

    return a;
}

public long getAgeSolding()
{
    LocalDate Now=LocalDate.now();

    long a=datesold.until(Now,ChronoUnit.DAYS);

    return a;
}

@Override
public String toString() 
{
    String str1="";
    String str2;

    if(sold==true)// TODO code application logic here
    {
        str1="Vehicle owned by "+cust.getName()+" since "+datesold;
    }

    switch(taxband)
    {
        case 'A':
            str2="0-100";
            break;
        case 'B':
            str2="101-110";
            break;
        case 'C':
            str2="111-120";
            break;
        case 'D':
            str2="121-130";
            break;
        case 'E':
            str2="131-140";
            break;
        case 'F':
            str2="141-150";
            break;
        case 'G':
            str2="151-160";
            break;
        default:
            str2="";
    }


    return "Manufacturer: "+manufacturer+"\n"+"Model: "+model+"\n"+"VIN: "+VIN+"\n"+"Date of manufacture: "+datemanuf+"\n"+"Price :"+price+" £\n"+"Tax Band: "+str2+"\n"+"Age of Vehicle: "+this.getAgeOfTheVehicle()+" weeks.\n"+str1+"\n";
}

}

陈列室课程

public class Showroom 
{
   private ArrayList<Vehicle> list;
   private int position;

public Showroom() 
{
    this.list =new ArrayList<>();
    this.position=1;
}

public int getPosition() 
{
    return position;
}



public ArrayList<Vehicle> getList() 
{
    return list;
}

public boolean add(Vehicle v)
{
    list.add(v);

    return true;
}

public Vehicle find(String VIN)
{
    ListIterator<Vehicle> it= list.listIterator();
    int n=1;

    while(it.hasNext())
    {
      Vehicle a=it.next();
      if(a.getVIN().equalsIgnoreCase(VIN))
      {
          this.position=n;
          return a;
      } 
      n++;

    }
    return null;
}

public Vehicle next()
{
    int n=this.position;
    ListIterator<Vehicle> it= list.listIterator(n);

    Vehicle a=it.next();
    position++;
    return a;
}

public Vehicle previous()
{
    int n=this.position;
    ListIterator<Vehicle> it= list.listIterator(n-1);

    Vehicle a=it.previous();
    position--;
    return a;
}

 public Vehicle current()
{
    int n=this.position;
    ListIterator<Vehicle> it= list.listIterator(n);

    Vehicle a=it.previous();
    return a;
}

 public ArrayList<Vehicle> getVehiclesSoldRecently()
 {

     ArrayList<Vehicle> a=new ArrayList<>();
     ListIterator<Vehicle> it= list.listIterator();

   while(it.hasNext())
    {

     Vehicle b=it.next();

     if(b.getAgeSolding()<=14)

      {

          a.add(b);
      } 


     return a;

 }

}

2 个答案:

答案 0 :(得分:3)

您应该使用foreach循环而不是while循环。 Foreach循环非常适合查看列表中的每个项目。

for(Vehicle vehicle : list){
    if(vehicle.getAgeSolding()<=14){
        a.add(vehicle);
    }
}

你做的方式理论上也应该有效。但是尝试将其转换为foreach循环,看看你是否能说出它为什么不起作用,因为这是一种更自然的方式。

如果你确定list里面有车辆,那么唯一的答案就是没有车辆返回int小于或等于14的车辆。

答案 1 :(得分:1)

您的代码运行良好:

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

public class Main {

    static List<Vehicle> list;

    public static void main(String[] args) {
        list=new ArrayList<>();
        list.add(new Vehicle(10));
        list.add(new Vehicle(20));
        System.out.println(getVehiclesSoldRecently().get(0).getAgeSolding());
    }

    public static ArrayList<Vehicle> getVehiclesSoldRecently()
    {

        ArrayList<Vehicle> a=new ArrayList<>();
        ListIterator<Vehicle> it= list.listIterator();

        while(it.hasNext())
        {

            Vehicle b=it.next();

            if(b.getAgeSolding()<=14)

            {

                a.add(b);
            }
        }
        return a;

    }

}

问题必须在别处。

但你可以在Java8中优雅地

public static List<Vehicle> getVehiclesSoldRecently2(){
    return list.stream()
            .filter(x->x.getAgeSolding()<=14)
            .collect(Collectors.toList());
}