這是我所擁有的:
package victor;
import java.security.PrivateKey;
import java.util.PrimitiveIterator;
public class Car {
private String mark;
private String model;
private int year;
private String color;
public Type type;
public Car(String mark, String model, int year, String color, Type type) {
this.mark = mark;
this.model = model;
this.year = year;
this.color = color;
this.type = type;
}
public String getMark() {
return mark;
}
public void setMark(String mark) {
this.mark = mark;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
@Override
public String toString() {
return "Car{"
"mark='" mark '\''
", model='" model '\''
", year=" year
", color='" color '\''
", type=" type
'}';
}
}
現在我必須回傳串列中最舊的。這是我的代碼:
package victor;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
//Create 4 cars (mark,model, production yrs, colour, sedan/coupe/combi/cabrio)
Car car1 = new Car("BMW", "M5", 2020, "Black", Type.SEDAN);
Car car2 = new Car("Audi", "SQ8", 2021, "Red", Type.COUPE);
Car car3 = new Car("Fiat", "Abarth", 2019, "Blue", Type.COUPE);
Car carTest = new Car("Fiat", "Abarth", 2019, "Blue", Type.COUPE);
Car car4 = new Car("Ferrari", "Pista", 2020, "Pink", Type.CABRIO);
List<Car> carList = List.of(car1, car2, car3, car4, carTest);
List<Car> oldCars = getOldCar(carList);
System.out.println("This is the oldest car/s: " oldCars);
}
public static List<Car> getOldCar(List<Car> carList) {
List<Car> oldestCars = new ArrayList<>();
Car oldCar = carList.get(0); //M5
for (int i = 0; i < carList.size(); i ) {
// if i put in the if statment <= it seems i get also the BMW
if (carList.get(i).getYear() <= oldCar.getYear()) {
oldCar = carList.get(i);
oldestCars.add(carList.get(i));
}
}
return oldestCars;
}
}
我試圖只得到 2 輛最舊的汽車,但顯然我也得到了第一輛車,索引為 0,我不知道如何只得到 2019 年的 2 輛菲亞特汽車。
這是我的代碼的輸出:
這是最古老的汽車:[Car{mark='BMW', model='M5', year=2020, color='Black', type=SEDAN},Car{mark='Fiat', model='Abarth ', year=2019, color='Blue', type=COUPE},Car{mark='Fiat', model='Abarth', year=2019, color='Blue', type=COUPE}]
uj5u.com熱心網友回復:
在現有代碼中,當檢測到一輛 yonger 汽車時,應清除/重新創建現有的舊汽車串列以累積與最小年份相關的汽車:
public static List<Car> getOldCar1(List<Car> carList) {
List<Car> oldestCars = new ArrayList<>();
int minYear = 3000;
for (Car car : carList) {
if (car.getYear() <= minYear) {
if (car.getYear() < minYear) {
minYear = car.getYear();
oldestCars.clear();
}
oldestCars.add(car);
}
}
return oldestCars;
}
使用 Stream API 的類似解決方案可以使用按年份按汽車分組Collectors.groupingBy,并使用最小鍵獲取值Collectors.minBy:
public static List<Car> getOldCar(List<Car> carList) {
return carList.stream()
.collect(Collectors.groupingBy(Car::getYear)) // Map<Integer, List<Car>>
.entrySet().stream()
.collect(Collectors.minBy(Map.Entry::getKey)) // Optional<Map.Entry>
.map(Map.Entry::getValue) // List<Car>
.orElse(Collections.emptyList());
}
在線演示 輸出:
這是最古老的汽車:[Car{mark='Fiat', model='Abarth', year=2019, color='Blue', type=COUPE}, Car{mark='Fiat', model='Abarth ', year=2019, color='藍色', type=COUPE}]
uj5u.com熱心網友回復:
在迭代汽車串列時,在三種情況下,下一輛車的年份與迄今為止最舊汽車的當前年份之間的關系。根據這三種情況,您決定是要將汽車添加到當前最舊汽車的串列中,還是需要創建新串列。決定是這樣的:
- 下一輛車的年份 > 當前最老的汽車年份- 什么都不做,車太年輕了...
- 下一輛車的年份 == 當前最舊的汽車年份- 將汽車添加到最舊的汽車串列
- 下一輛車的年份 < 當前最舊的汽車年份- 創建一個新的最舊汽車串列,從當前汽車開始。
該getOldCar()方法的代碼可能如下所示:
public static List<Car> getOldCar(List<Car> carList) {
List<Car> oldestCars = new ArrayList<>();
// always start with the first car in the list
oldestCars.add(carList.get(0));
// start at i=1, as the first car is already in the list
for (int i = 1; i < carList.size(); i ) {
Car currentCar = carList.get(i);
int yearLimit = oldestCars.get(0).getYear();
int currentCarYear = currentCar.getYear();
if (currentCarYear > yearLimit) {
// younger, not relevant
} else if (currentCarYear == yearLimit) {
// it fits in the current year, add it
oldestCars.add(currentCar);
} else {
// it is older than every other car before, start with a new list
oldestCars = new ArrayList<>();
oldestCars.add(currentCar);
}
}
return oldestCars;
}
uj5u.com熱心網友回復:
如果您使用java 8或以上:
在您的主要功能中:
將您的carlist存盤在 ArrayList 中,以便compareTo可以使用:
然后 Fist 對您的串列進行排序,然后get(0)對最舊的和get(size() - 1)最新的汽車進行排序。
public class Main
{
public static void main (String[]args)
{
Car car1 = new Car ("BMW", "M5", 2020, "Black");
Car car2 = new Car ("Audi", "SQ8", 2021, "Red");
Car car3 = new Car ("Fiat", "Abarth", 2019, "Blue");
Car carTest = new Car ("Fiat", "Abarth", 2019, "Blue");
Car car4 = new Car ("Ferarri", "Pista", 2020, "Pink");
ArrayList < Car > carList = new ArrayList <> ();
carList.add (car1);
carList.add (car2);
carList.add (car3);
carList.add (car4);
carList.add (carTest);
Collections.sort (carList,
(a, b)->Integer.compare (a.getYear (), b.getYear ()));
ArrayList < Car > oldCars =
findUsingEnhancedForLoop (carList.get (0).getYear (), carList);
System.out.println ("This is the oldest car/s: " oldCars.toString ());
}
public static ArrayList < Car > findUsingEnhancedForLoop (int year,
List < Car > cars)
{
ArrayList < Car > oldestCar = new ArrayList <> ();
for (Car car:cars)
{
if (car.getYear () == year)
{
oldestCar.add (car);
}
}
return oldestCar;
}
}
uj5u.com熱心網友回復:
您可以嘗試“針對汽車串列中的每輛車”的高級 for 回圈
int oldestCarsYear = 0;
Car oldestCar;
for(Car car : carList){
if(car.getYear() <= oldestCarsYear || oldestCarsYear == 0){
oldestCarsYear = car.getYear();
oldestCar = car;
}
}
oldestCars.add(oldestCar);
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/398314.html
上一篇:如何使用子串列重塑串列?
下一篇:從R中的串列中獲取資料框的名稱
