使用 Comparable/Compareto 方法从输入文件中对 Arrraylist 进行排序
Sorting Arrraylist from an input file using Comparable/Compareto Methods
我正在尝试创建一个读取输入文件并将其输出三次的程序。最后一个输出显示了输入中的所有值,并使用我的 compareTo 方法进行了排序。
我正在尝试整理我的阵列列表中的人口,但我似乎遇到了错误
到目前为止,这是我的代码:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.lang.Comparable;
public class TestingCode {
/**
* The main method starts the program
*
*@param args is the input file
*/
public static void main(String[] args) {
//error checking for commandline input
if(args.length != 1){
System.out.println("Please enter at least one input file into the argument.");
//terminates the program if more than 1 is entered
System.exit(1);
}
String csvFile = args[0];
String line = "";
String cvsSplitBy = ",";
List<HawaiiNativeForestBirds> listofBirds = new ArrayList<HawaiiNativeForestBirds>();
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
while ((line = br.readLine()) != null) {
// use comma as separator
String[] bird = line.split(cvsSplitBy);
HawaiiNativeForestBirds Hawaiinbird= new HawaiiNativeForestBirds(bird[0],bird[1],bird[2],Integer.valueOf(bird[3]));
listofBirds.add(Hawaiinbird);
}
}
catch (IOException e) {
e.printStackTrace();
}
// First display null values
HawaiiNativeForestBirds[] hbirds=new HawaiiNativeForestBirds[listofBirds.size()];
System.out.println("index " + " element ");
int i=0;
for (HawaiiNativeForestBirds hbird:hbirds){
i++;
System.out.println(i+" "+hbird);
}
// Now display actual values
hbirds= listofBirds.toArray(new HawaiiNativeForestBirds[listofBirds.size()]);
System.out.println("index " + "name "+ " Scientific Name "+ " Color " + " Population");
i=0;
for (HawaiiNativeForestBirds hbird:hbirds){
i++;
System.out.println(i+" "+hbird.toString());
}
//method to sort the array list
Collection.sort(listofBirds);
// Now display actual values but with the added changes to the data/values
hbirds= listofBirds.toArray(new HawaiiNativeForestBirds[listofBirds.size()]);
System.out.println("index " + "name "+ " Scientific Name "+ " Color " + " Population");
i=0;
for (HawaiiNativeForestBirds hbird:hbirds){
i++;
System.out.println(i+" "+hbird.toString2());
}
} // end of main
} //end of class
/**
* Class HawaiianNativeForestBirds stores and displays the data for each HawaiianTheme object
*
*
*/
class HawaiiNativeForestBirds implements java.lang.Comparable<HawaiiNativeForestBirds>{
private String name;
private String scientificname;
private String color;
private Integer population;
public HawaiiNativeForestBirds(){
}
//constructor - used to initialize the four data fields
/**
* Stores the name,scientific name, color and population of the Hawaiian Birds
*
*
* @param name is the name of the birds from the array list
* @param scientificname is the scientific name of the birds in the array list
* @param color is the color of each of the birds in the array list
* @param population is the total number of birds in the array list
*/
public HawaiiNativeForestBirds(String name, String scientificname,
String color, Integer population) {
super();
this.name = name;
this.scientificname = scientificname;
this.color = color;
this.population = population;
}
/**
* Gets each bird's name
*
* @return the birds name
*/
public String getName() {
return name;
}
/**
* Sets each birds name
*
* @param name is the bird's name
*/
public void setName(String name) {
this.name = name;
}
/**
* Gets the bird's scientific name
*
* @return the bird's scientific name
*/
public String getScientificname() {
return scientificname;
}
/**
* Sets the birds scientific name
*
* @param scientificname is the bird's scientific name
*/
public void setScientificname(String scientificname) {
this.scientificname = scientificname;
}
/**
* Gets the bird's color
*
* @return the bird's color
*/
public String getColor() {
return color;
}
/**
* Sets the bird's color
*
* @param color is the bird's color
*/
public void setColor(String color) {
this.color = color;
}
/**
* Gets the bird's population
*
* @return total population of the bird
*/
public Integer getPopulation() {
return population;
}
/**
* Sets the bird's population
*
* @param population is the total population of the bird
*/
public void setPopulation(Integer population) {
this.population = population;
}
/**
* Display the output
*
* @return the name, scientificname, color, and population of the bird
*/
public String toString() {
String output = name +" "+ scientificname + " "+ color +" "+ population;
return output;
}//end of toString
//compareTo method that sorts the population
public int compareTo(HawaiiNativeForestBirds comparestu) {
int comparePopulation=((HawaiiNativeForestBirds)comparestu).getPopulation();
/* For Ascending order*/
return this.population-comparePopulation;
} //end of compareTO
/**
* Display the outputs with changed elements/values
*
* @return the name, scientificname, color, and population of the bird but with changes made
* to the data.
*/
public String toString2() {
population = population + 1;
String output = name.toUpperCase() +" "+ scientificname.toUpperCase() + " "+ color.toUpperCase() +" "+ population;
return output;
}//end of toString2
}// end of class
希望我的比较方法正确,现在唯一的问题是调用排序方法的方法。
错误:
TestingCode.java:72: error: cannot find symbol
Collection.sort(listofBirds);
^
symbol: variable Collection
location: class TestingCode
1 error
我的输入文件:
编辑:
添加了我的输入文件抱歉!
两件事:
- 编译器找不到
Collection
class。那是因为缺少合适的 import
.
sort()
方法在Collections
class,不是Collection
class.
我正在尝试创建一个读取输入文件并将其输出三次的程序。最后一个输出显示了输入中的所有值,并使用我的 compareTo 方法进行了排序。
我正在尝试整理我的阵列列表中的人口,但我似乎遇到了错误
到目前为止,这是我的代码:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.lang.Comparable;
public class TestingCode {
/**
* The main method starts the program
*
*@param args is the input file
*/
public static void main(String[] args) {
//error checking for commandline input
if(args.length != 1){
System.out.println("Please enter at least one input file into the argument.");
//terminates the program if more than 1 is entered
System.exit(1);
}
String csvFile = args[0];
String line = "";
String cvsSplitBy = ",";
List<HawaiiNativeForestBirds> listofBirds = new ArrayList<HawaiiNativeForestBirds>();
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
while ((line = br.readLine()) != null) {
// use comma as separator
String[] bird = line.split(cvsSplitBy);
HawaiiNativeForestBirds Hawaiinbird= new HawaiiNativeForestBirds(bird[0],bird[1],bird[2],Integer.valueOf(bird[3]));
listofBirds.add(Hawaiinbird);
}
}
catch (IOException e) {
e.printStackTrace();
}
// First display null values
HawaiiNativeForestBirds[] hbirds=new HawaiiNativeForestBirds[listofBirds.size()];
System.out.println("index " + " element ");
int i=0;
for (HawaiiNativeForestBirds hbird:hbirds){
i++;
System.out.println(i+" "+hbird);
}
// Now display actual values
hbirds= listofBirds.toArray(new HawaiiNativeForestBirds[listofBirds.size()]);
System.out.println("index " + "name "+ " Scientific Name "+ " Color " + " Population");
i=0;
for (HawaiiNativeForestBirds hbird:hbirds){
i++;
System.out.println(i+" "+hbird.toString());
}
//method to sort the array list
Collection.sort(listofBirds);
// Now display actual values but with the added changes to the data/values
hbirds= listofBirds.toArray(new HawaiiNativeForestBirds[listofBirds.size()]);
System.out.println("index " + "name "+ " Scientific Name "+ " Color " + " Population");
i=0;
for (HawaiiNativeForestBirds hbird:hbirds){
i++;
System.out.println(i+" "+hbird.toString2());
}
} // end of main
} //end of class
/**
* Class HawaiianNativeForestBirds stores and displays the data for each HawaiianTheme object
*
*
*/
class HawaiiNativeForestBirds implements java.lang.Comparable<HawaiiNativeForestBirds>{
private String name;
private String scientificname;
private String color;
private Integer population;
public HawaiiNativeForestBirds(){
}
//constructor - used to initialize the four data fields
/**
* Stores the name,scientific name, color and population of the Hawaiian Birds
*
*
* @param name is the name of the birds from the array list
* @param scientificname is the scientific name of the birds in the array list
* @param color is the color of each of the birds in the array list
* @param population is the total number of birds in the array list
*/
public HawaiiNativeForestBirds(String name, String scientificname,
String color, Integer population) {
super();
this.name = name;
this.scientificname = scientificname;
this.color = color;
this.population = population;
}
/**
* Gets each bird's name
*
* @return the birds name
*/
public String getName() {
return name;
}
/**
* Sets each birds name
*
* @param name is the bird's name
*/
public void setName(String name) {
this.name = name;
}
/**
* Gets the bird's scientific name
*
* @return the bird's scientific name
*/
public String getScientificname() {
return scientificname;
}
/**
* Sets the birds scientific name
*
* @param scientificname is the bird's scientific name
*/
public void setScientificname(String scientificname) {
this.scientificname = scientificname;
}
/**
* Gets the bird's color
*
* @return the bird's color
*/
public String getColor() {
return color;
}
/**
* Sets the bird's color
*
* @param color is the bird's color
*/
public void setColor(String color) {
this.color = color;
}
/**
* Gets the bird's population
*
* @return total population of the bird
*/
public Integer getPopulation() {
return population;
}
/**
* Sets the bird's population
*
* @param population is the total population of the bird
*/
public void setPopulation(Integer population) {
this.population = population;
}
/**
* Display the output
*
* @return the name, scientificname, color, and population of the bird
*/
public String toString() {
String output = name +" "+ scientificname + " "+ color +" "+ population;
return output;
}//end of toString
//compareTo method that sorts the population
public int compareTo(HawaiiNativeForestBirds comparestu) {
int comparePopulation=((HawaiiNativeForestBirds)comparestu).getPopulation();
/* For Ascending order*/
return this.population-comparePopulation;
} //end of compareTO
/**
* Display the outputs with changed elements/values
*
* @return the name, scientificname, color, and population of the bird but with changes made
* to the data.
*/
public String toString2() {
population = population + 1;
String output = name.toUpperCase() +" "+ scientificname.toUpperCase() + " "+ color.toUpperCase() +" "+ population;
return output;
}//end of toString2
}// end of class
希望我的比较方法正确,现在唯一的问题是调用排序方法的方法。
错误:
TestingCode.java:72: error: cannot find symbol
Collection.sort(listofBirds);
^
symbol: variable Collection
location: class TestingCode
1 error
我的输入文件:
编辑: 添加了我的输入文件抱歉!
两件事:
- 编译器找不到
Collection
class。那是因为缺少合适的import
. sort()
方法在Collections
class,不是Collection
class.