在Java中对列表进行排序后,如何访问第一个元素?

huangapple go评论73阅读模式
英文:

After sorting a list in Java, how could I access the first element?

问题

我创建了一个名为Car的类,具有这些属性和方法。我使用了一个列表来存储多个对象。

package com.company;
import java.util.ArrayList;
import java.util.Collections;

class Car implements Comparable<Car> {
    private String manufacturer;
    private int speed;
    private double price;
    private int year; // 制造年份

    public Car(String manufacturer, int speed, double price, int year) {
        this.manufacturer = manufacturer;
        this.speed = speed;
        this.price = price;
        this.year = year;
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public int getSpeed() {
        return speed;
    }

    public double getPrice() {
        return price;
    }

    public int getYear() {
        return year;
    }

    public String toString() {
        return this.manufacturer + ", 速度:" + this.speed + "公里/小时,价格:" + this.price + "美元,年份:" + this.year;
    }

    public int compareTo(Car c) {
        if (this.price == c.price)
            return 0;
        else if(this.price > c.price)
            return 1;
        else
            return -1;
    }
}

public class Solution {
    public static void main(String[] args) {
        List<Car> cars = new ArrayList<Car>();
        cars.add(new Car("Audi", 280, 35300, 2018));
        cars.add(new Car("Mercedes-Benz", 155, 80000, 2009));
        cars.add(new Car("Toyota", 155, 18750, 2011));
        cars.add(new Car("Fiat", 140, 22500, 2000));
        cars.add(new Car("Land Rover", 209, 90900, 2014));
        for (Car iterator: cars) {
            System.out.println(iterator);
        }

        Collections.sort(cars);
        for (Car iterator: cars) {
            System.out.println(iterator.getPrice());
        }
    }
}

在使用Comparable接口之后,我想访问排序后列表的第一个元素,因为我想获取最便宜的汽车,例如。我该如何做到这一点?

英文:

I've created a class Car, with these attributes and methods. I've used a list to store several objects.

package com.company;
import java.util.ArrayList;
import java.util.Collections;
class Car implements Comparable&lt;Car&gt; {
private String manufacturer;
private int speed;
private double price;
private int year; // manufacture year
public Car(String manufacturer, int speed, double price, int year) {
this.manufacturer = manufacturer;
this.speed = speed;
this.price = price;
this.year = year;
}
public String getManufacturer() {
return manufacturer;
}
public int getSpeed() {
return speed;
}
public double getPrice() {
return price;
}
public int getYear() {
return year;
}
public String toString() {
return this.manufacturer + &quot;, speed: &quot; + this.speed + &quot;km/h, price: &quot; + this.price + &quot; USD, year: &quot; + this.year;
}
public int compareTo(Car c) {
if (this.price == c.price)
return 0;
else if(this.price &gt; c.price)
return 1;
else
return -1;
}
}
public class Solution {
public static void main(String[] args) {
List&lt;Car&gt; cars = new ArrayList&lt;Car&gt;();
cars.add(new Car(&quot;Audi&quot;, 280, 35300, 2018));
cars.add(new Car(&quot;Mercedes-Benz&quot;, 155, 80000, 2009));
cars.add(new Car(&quot;Toyota&quot;, 155, 18750, 2011));
cars.add(new Car (&quot;Fiat&quot;, 140, 22500, 2000));
cars.add(new Car (&quot;Land Rover&quot;, 209, 90900, 2014));
for (Car iterator: cars) {
System.out.println(iterator);
}
Collections.sort(cars);
for (Car iterator: cars) {
System.out.println(iterator.getPrice());
}
}
}

After using Comparable interface, I would like to access the first element of the sorted list, because I want to obtain the cheapest car, for example. How could I do that?

答案1

得分: 5

对于 ArrayList,您可以通过以下方式访问第一个元素:

cars.get(0);

对于 LinkedList

cars.getFirst();
英文:

For ArrayList you can access the first element by this way:

cars.get(0);

For LinkedList:

cars.getFirst();

答案2

得分: 1

cars.get(0);

但不要忘记你可能有许多价格最低的车

Collections.sort(cars);

double minPrice = cars.get(0).price;

for (Car car : cars) {
    if (Double.compare(car.gePrice(), minPrice) != 0)
        break;

    System.out.println(car);
}
英文:
cars.get(0);

But do not forget that you could have many thay one cars with the minimum price:

Collections.sort(cars);
double minPrice = cars.get(0).price;
for (Car car : cars) {
if (Double.compare(car.gePrice(), minPrice) != 0)
break;
System.out.println(car);
}

答案3

得分: 0

你可以使用下面的代码来实现这一点。

英文:

You can use below code to achieve that.

cars.get(0);

huangapple
  • 本文由 发表于 2020年10月19日 03:20:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64417404.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定