英文:
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<Car> {
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 + ", speed: " + this.speed + "km/h, price: " + this.price + " USD, year: " + 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());
}
}
}
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论