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

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

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

问题

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

  1. package com.company;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. class Car implements Comparable<Car> {
  5. private String manufacturer;
  6. private int speed;
  7. private double price;
  8. private int year; // 制造年份
  9. public Car(String manufacturer, int speed, double price, int year) {
  10. this.manufacturer = manufacturer;
  11. this.speed = speed;
  12. this.price = price;
  13. this.year = year;
  14. }
  15. public String getManufacturer() {
  16. return manufacturer;
  17. }
  18. public int getSpeed() {
  19. return speed;
  20. }
  21. public double getPrice() {
  22. return price;
  23. }
  24. public int getYear() {
  25. return year;
  26. }
  27. public String toString() {
  28. return this.manufacturer + ", 速度:" + this.speed + "公里/小时,价格:" + this.price + "美元,年份:" + this.year;
  29. }
  30. public int compareTo(Car c) {
  31. if (this.price == c.price)
  32. return 0;
  33. else if(this.price > c.price)
  34. return 1;
  35. else
  36. return -1;
  37. }
  38. }
  39. public class Solution {
  40. public static void main(String[] args) {
  41. List<Car> cars = new ArrayList<Car>();
  42. cars.add(new Car("Audi", 280, 35300, 2018));
  43. cars.add(new Car("Mercedes-Benz", 155, 80000, 2009));
  44. cars.add(new Car("Toyota", 155, 18750, 2011));
  45. cars.add(new Car("Fiat", 140, 22500, 2000));
  46. cars.add(new Car("Land Rover", 209, 90900, 2014));
  47. for (Car iterator: cars) {
  48. System.out.println(iterator);
  49. }
  50. Collections.sort(cars);
  51. for (Car iterator: cars) {
  52. System.out.println(iterator.getPrice());
  53. }
  54. }
  55. }

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

英文:

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

  1. package com.company;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. class Car implements Comparable&lt;Car&gt; {
  5. private String manufacturer;
  6. private int speed;
  7. private double price;
  8. private int year; // manufacture year
  9. public Car(String manufacturer, int speed, double price, int year) {
  10. this.manufacturer = manufacturer;
  11. this.speed = speed;
  12. this.price = price;
  13. this.year = year;
  14. }
  15. public String getManufacturer() {
  16. return manufacturer;
  17. }
  18. public int getSpeed() {
  19. return speed;
  20. }
  21. public double getPrice() {
  22. return price;
  23. }
  24. public int getYear() {
  25. return year;
  26. }
  27. public String toString() {
  28. return this.manufacturer + &quot;, speed: &quot; + this.speed + &quot;km/h, price: &quot; + this.price + &quot; USD, year: &quot; + this.year;
  29. }
  30. public int compareTo(Car c) {
  31. if (this.price == c.price)
  32. return 0;
  33. else if(this.price &gt; c.price)
  34. return 1;
  35. else
  36. return -1;
  37. }
  38. }
  39. public class Solution {
  40. public static void main(String[] args) {
  41. List&lt;Car&gt; cars = new ArrayList&lt;Car&gt;();
  42. cars.add(new Car(&quot;Audi&quot;, 280, 35300, 2018));
  43. cars.add(new Car(&quot;Mercedes-Benz&quot;, 155, 80000, 2009));
  44. cars.add(new Car(&quot;Toyota&quot;, 155, 18750, 2011));
  45. cars.add(new Car (&quot;Fiat&quot;, 140, 22500, 2000));
  46. cars.add(new Car (&quot;Land Rover&quot;, 209, 90900, 2014));
  47. for (Car iterator: cars) {
  48. System.out.println(iterator);
  49. }
  50. Collections.sort(cars);
  51. for (Car iterator: cars) {
  52. System.out.println(iterator.getPrice());
  53. }
  54. }
  55. }

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,您可以通过以下方式访问第一个元素:

  1. cars.get(0);

对于 LinkedList

  1. cars.getFirst();
英文:

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

  1. cars.get(0);

For LinkedList:

  1. cars.getFirst();

答案2

得分: 1

  1. cars.get(0);
  2. 但不要忘记你可能有许多价格最低的车
  3. Collections.sort(cars);
  4. double minPrice = cars.get(0).price;
  5. for (Car car : cars) {
  6. if (Double.compare(car.gePrice(), minPrice) != 0)
  7. break;
  8. System.out.println(car);
  9. }
英文:
  1. cars.get(0);

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

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

答案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:

确定