gpt4 book ai didi

java - 根据字段对自定义类的实例数组进行排序

转载 作者:行者123 更新时间:2023-12-02 01:22:35 25 4
gpt4 key购买 nike

我现在在一个项目中遇到一个问题,我只是为了了解更多关于java的知识,所以我有一个txt文件,里面有奇迹,我必须阅读它,然后将不同的信息存储在一个对象(名称国家、高度深度、可达性、敌意、距伦敦公里数、温度、奇迹值和有关奇迹的事实),然后我必须按敌意率排序并输出所有奇迹

这是奇迹的txt文件:

Amazonia,Brazil,60,10,10,8100,30,4,The Amazon rainforest occupies some 5.5 million square kilometres of South America.
Iguassu Falls,Brazil|Argentina,82,11,50,10064,25,4,Two hundred and seventy waterfalls flow along nearly 3 kilometres of rivers.
Niagra Falls,USA|Canada,50,18,69,5804,20,4,Adventurous tourists can take a cruise on the river below into the falls' mist.
Giant's Causeway,Northern Ireland,12,19,17,592,15,2,Legend has it that is is a former 'walkway' to Scotland.
Great Barrier Reef,Australia,60,10,10,15292,28,4,Over 30 species of whale and 15000 species of fish live here.
Mount Everest,Tibet|Nepal,8840,7,95,7424,-10,4,The highest mountain on Earth is located in the Himalayas.
Mount Vesuvius,Italy,1281,18,95,1630,30,1,The only volcano in mainland Europe to have erupted in the last 100 years.
Old Faithful,USA,55,14,65,7432,118,2,Over half of the world's geysers are located in Yellowstone national park.
Sahara Desert,African Union,3445,14,84,3800,35,3,Covers part of North Africa and is almost the same size as the USA.
Great Rift Valley,African Union,1470,14,12,5887,25,4,The valley was formed by activity between tectonic plates 35 million years ago.
Gobi Desert,Mongolia|China,2700,9,84,7279,30,3,The desert has been the location of many fossil finds.
Ngorongoro Crater,Tanzania,610,14,19,5804,30,2,Home to almost 25000 animals including masses of flamingoes around Lake Magadi.
Perito Morena Glacier,Argentina,60,9,82,13230,-5,3,The 5 kilometre wide glacier is well known for its process of rupturing.
Mount Fuji,Japan,3776,13,69,8671,25,2,Although classed as active its last eruption was in 1707.
Mont Blanc,Italy|France,4808,18,85,808,0,2,The highest mountain in Europe and a popular skiing destination.
The Dead Sea,Israel|Jordan,418,13,91,3666,25,1,The lowest point on the surface of the Earth and is really two large lakes.
The Matterhorn,France|Italy|Switzerland,4478,17,85,840,-5,2,Not the highest mountain in the Alps but perhaps the most breathtaking.
Uluru,Australia,346,10,70,14993,35,4,A massive monolith made from sandstone infused with minerals that reflect in the sunlight.
Lake Baikal,Russia,1637,8,55,6613,-30,2,An immense depth of 1637 metres and contains 20% of the worlds fresh water.
Kilauea,The Hawaiian Islands,1247,9,65,11783,30,3,Hawaiian legend considers the island to be the home of a volcano goddess.
Guilin Caves,China,220,9,16,9101,30,2,The people of Guilin have had to take refuge in them during times of conflict.
Giant Sequoia,USA,84,15,2,8570,30,2,The largest species of tree found only in California.
Mount Erebus,Antarctica,3794,4,95,17059,-49,3,Has continually erupted since 1972 and has a permanent lava lake within its summit.
Grand Canyon,USA,1600,15,80,8296,30,4,A vast and breathtaking spectacle stretching across the Arizona desert.

所以我创建了一个像这样的对象

public class Wonders {
String name;
List countries = new ArrayList();
int heightDepth;
int accessibility;
int hostility;
int kmFromLondon;
int temperature;
int wonderValue;
String fact;
List compare = new ArrayList();

然后我像这样存储它

     String unftxt[];
unftxt = new String[24];
Wonders w[] = new Wonders[unftxt.length];



try (Scanner sfile = new Scanner(new File(fName))) {

while(sfile.hasNextLine()){
unftxt[ctrl] = sfile.nextLine();
ctrl++;
}//end while .hasnextLine()
}//try

for(int i=0;i<unftxt.length;i++){
String[] parts = unftxt[i].split(",");
w[i] = new Wonders();
w[i].name = parts[0];
w[i].countries.add(Arrays.toString(parts[1].split("\\|")));
w[i].heightDepth = Integer.parseInt(parts[2]);
w[i].accessibility = Integer.parseInt(parts[3]);
w[i].hostility = Integer.parseInt(parts[4]);
w[i].kmFromLondon = Integer.parseInt(parts[5]);
w[i].temperature = Integer.parseInt(parts[6]);
w[i].wonderValue = Integer.parseInt(parts[7]);
w[i].fact = parts[8];

}

我是编程新手,现在已经被困了一段时间,所以我的问题是如何按敌意对这个数组进行排序

最佳答案

在 Java 中定义自定义排序的常规方法是通过 Comparator实例。该对象可以插入到例如 Arrays 的排序例程中。或Collections .

例如,在 Java 8 中,您可以像这样对数组进行排序:

Arrays.sort(wonderArray, Comparator.comparingInt(wonder -> wonder.hospitality));

在Java 7或更早版本中,您可以使用匿名类来做到这一点:

Arrays.sort(wonderArray, new Comparator<Wonder>() {
@Override public int compareTo(Wonder w1, Wonder w2) {
return Integer.compare(w1.hospitality, w2.hospitality);
}
});

关于java - 根据字段对自定义类的实例数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57421552/

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