可以结构数据,因为这模型
行类代表行数据
public class Row implements Comparable<Row> {
private int number;
private String country;
private String animal;
private String city;
public Row(int number, String country, String animal, String city) {
super();
this.number = number;
this.country = country;
this.animal = animal;
this.city = city;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getAnimal() {
return animal;
}
public void setAnimal(String animal) {
this.animal = animal;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
// Easy to print and show the row data
@Override
public String toString() {
return "Row [number=" + number + ", country=" + country + ", animal="
+ animal + ", city=" + city + "]";
}
// sort based on column "country"
@Override
public int compareTo(Row o) {
return this.country.compareTo(o.country);
}
}
和测试示例将作为
public static void main(String[] args) {
ArrayList<Row> data = new ArrayList<Row>();
data.add(new Row(1000, "Australia", "Kangaroo", "Canberra"));
data.add(new Row(1002, "India", "Tiger", "Delhi"));
data.add(new Row(1092, "Germany", "Eagle", "Berlin"));
// To sort the data (based on column "country")
Collections.sort(data);
// Print and show the data
for (int i = 0; i < data.size(); i++) {
System.out.println(data.get(i));
}
}
这将是微不足道的提供用户能够做到这一点,使用'JTable'。虽然我想这不是你想要实现的功能。 ;) –
它们是列表或表记录的对象吗?当你正在写或记录你的数据,你可以使用冒泡排序算法来排序 –
这是一个文本文件 – FirmView