このモデルとしてデータを構造化できます
行データを表す行クラス
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));
}
}