gpt4 book ai didi

java - 将对象添加到数组列表

转载 作者:行者123 更新时间:2023-12-01 18:15:44 25 4
gpt4 key购买 nike

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 a 中呢?在每种情况下我都会获得一个空的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;

}

}

最佳答案

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

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

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

如果您确定 list 中包含车辆,那么唯一的答案是没有车辆返回小于或等于 14 的 int

关于java - 将对象添加到数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29711079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com