ArrayList sort for String, int
根据
所以,我的变量是
1 2 3 | India 2 Pakistan 3 USA 1 |
输出变为:
1 2 3 | USA 1 India 2 Pakistan 3 |
我很困惑,它是如何与国际埃多克斯1〔4〕不工作与它。
不能使用类型为的ArrayList
1 |
如果要保存string和int,可以创建一个具有字段名和排名的CountryInfo类。然后创建
1 | ArrayList<CountryInfo> list =new ArrayList<CountryInfo>(); |
然后你可以用
1 |
我创建了一个示例,您可以在其中对数组列表进行排序,即使它包含对象。你可以通读一下,看看是否有帮助。
我已经上了两门课和一门测试课:
头等舱是国家:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | public class Country { private String countryName; private int number; public Country(String countryName, int number){ this.countryName = countryName; this.number = number; } public String getCountryName(){ return countryName; } public void setCountryName(String newCountryName){ countryName = newCountryName; } public int getNumber(){ return number; } public void setNumber(int newNumber){ number = newNumber; } public String toString(){ return getCountryName() + getNumber(); } } |
下一个类是方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | public class Methods { private Country country; private ArrayList<Country> overview = new ArrayList<Country>(); private ArrayList<Country> overviewSorted = new ArrayList<Country>(); int [] test; public void regCountry(String countryname, int numbers){ if(!(countryname =="" && numbers == 0)){ overview.add(new Country(countryname, numbers)); } else { System.out.println("The input was null"); } } public void showRegisteredCountries(){ if(!(overview.size() < 0)){ for(int i = 0; i < overview.size(); i++){ System.out.println("The country:" + overview.get(i).getCountryName() +" has the number:" + overview.get(i).getNumber() +" registered"); } } else { System.out.println("There are no country registered"); } } public void numbersOverFromArrayList(){ if(!(overview.size() < 0)){ test = new int [overview.size()]; for(int i = 0; i < overview.size(); i++){ test[i] = overview.get(i).getNumber(); } } } public void sortArrayAndCopyItBack(){ if(!(test.length < 0)){ java.util.Arrays.sort(test); for(int i = 0; i < test.length; i ++){ for(int j = 0; j < overview.size(); j++){ if(test[i] == overview.get(j).getNumber()){ overviewSorted.add(new Country(overview.get(j).getCountryName(), overview.get(j).getNumber())); } } } } } public void showTableSorted(){ if(!(overviewSorted.size() < 0)){ for(int i = 0; i < overviewSorted.size(); i++){ System.out.println("Country name:" + overviewSorted.get(i).getCountryName() +" with number:" + overviewSorted.get(i).getNumber()); } } else { System.out.println("There are non countrys in table that is sorted"); } } } |
接下来是测试类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public class test2 { public static void main(String [] args){ Methods methodes = new Methods(); for(int i = 0; i < 4; i++){ String inCountry = JOptionPane.showInputDialog("Country:"); String inNumber = JOptionPane.showInputDialog("number:"); String country = inCountry; int number = Integer.parseInt(inNumber); methodes.regCountry(country, number); } methodes.showRegisteredCountries(); methodes.numbersOverFromArrayList(); methodes.sortArrayAndCopyItBack(); methodes.showTableSorted(); } } |
我的输出:
1 2 3 4 5 6 7 8 | The country: Norway has the number: 5 registered The country: Sweden has the number: 2 registered The country: Denmark has the number: 9 registered The country: Finland has the number: 7 registered Country name: Sweden with number: 2 Country name: Norway with number: 5 Country name: Finland with number: 7 Country name: Denmark with number: 9 |
如果您有一个对象想要以多种方式排序,那么您需要为每种类型的排序定义一个Comparator类。
使用OP给出的例子,这里有一种定义对象和比较器的方法。
以下是一个测试结果:
1 2 3 4 5 6 7 | CountryRating [name=India, rating=2] CountryRating [name=Pakistan, rating=3] CountryRating [name=USA, rating=1] CountryRating [name=USA, rating=1] CountryRating [name=India, rating=2] CountryRating [name=Pakistan, rating=3] |
下面是示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class CountryRating { private String name; private int rating; public CountryRating(String name, int rating) { this.name = name; this.rating = rating; } public String getName() { return name; } public int getRating() { return rating; } @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("CountryRating [name="); builder.append(name); builder.append(", rating="); builder.append(rating); builder.append("]"); return builder.toString(); } public static void main(String[] args) { List<CountryRating> list = new ArrayList<CountryRating>(); CountryRating cr1 = new CountryRating("USA", 1); CountryRating cr2 = new CountryRating("India", 2); CountryRating cr3 = new CountryRating("Pakistan", 3); list.add(cr1); list.add(cr2); list.add(cr3); Collections.sort(list, new CountrySort()); printList(list); System.out.println(""); Collections.sort(list, new RatingSort()); printList(list); } private static void printList(List<CountryRating> list) { for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } } } class CountrySort implements Comparator<CountryRating> { @Override public int compare(CountryRating cr1, CountryRating cr2) { return cr1.getName().compareTo(cr2.getName()); } } class RatingSort implements Comparator<CountryRating> { @Override public int compare(CountryRating cr1, CountryRating cr2) { return cr1.getRating() - cr2.getRating(); } } |
你可以排序使用collections.sort(list,comparator实现)
在实现中(这里我使用了匿名实现)重写比较方法
你在哪里获取每个字符串的最后一个字符转换为字符串并进行比较
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | ArrayList<String> a=new ArrayList<String>(); a.add("India 2"); a.add("Pakistan 3"); a.add("USA 1"); Collections.sort(a, new Comparator<String>() { @Override public int compare(String o1, String o2) { Integer i=Integer.valueOf(o1.substring((o1.length() -1),o1.length())); Integer j=Integer.valueOf(o2.substring((o2.length() -1),o2.length())); return i.compareTo(j); } }); |
你可以优化代码
arraylist是一种对象类型的集合。它不像地图那样可以接受两个输入。
因此,有三种选择:1。使用同时包含键和映射并按键或自动排序的树映射
2。使用未排序的映射并用比较器排序-参见按值(Java)对map <键,value >排序
三。使用带有比较器的自定义类的ArrayList。
-
1)使用treemap
treemaps是红黑树的一种实现。请参阅:http://docs.oracle.com/javase/1.5.0/docs/api/java/util/treemap.html
1 2 3 4 5 6 7 8 9 10 11 12 | TreeMap<Integer,String> countries = new TreeMap<Integer,String>(); countries.put(2,"India"); countries.put(1,"USA"); countries.put(3,"Pakistan"); Iterator<Entry<Integer, String>> it = countries.entrySet().iterator(); Entry<Integer, String> entry; while(it.hasNext()) { entry = it.next(); System.out.println(entry.getValue() +"" + entry.getKey()); } |
这就产生了:
1 2 3 | USA 1 India 2 Pakistan 3 |
-
2)使用未排序的图,并使用比较器进行排序。参见:根据值(Java)对地图"键"、"值"进行排序,因为答案是非常写的。
-3)将ArrayList与Country类一起使用
为了支持您的示例,您需要创建一个国家类。您需要执行以下操作:
创建一个自定义比较器,您将为collection.sort调用提供该比较器。
导入java.util.arraylist;导入java.util.collections;导入java.util.inputMismatchException;导入java.util.iterator;
公共类CountrySortExample{
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | public static void main(String[] args) { new CountrySortExample(); } public ArrayList<Country> countries = new ArrayList<Country>(); public CountrySortExample() { countries.add(new Country("India",2)); countries.add(new Country("Pakistan",3)); countries.add(new Country("USA",1)); Collections.sort(countries); Iterator<Country> it = countries.iterator(); Country count; while(it.hasNext()) { count = it.next(); System.out.println(count.CountryName +"" + count.CountryIndex); } } class Country implements Comparable { public String CountryName; public int CountryIndex; public Country(String CountryName,int CountryIndex ) { this.CountryName = CountryName; this.CountryIndex = CountryIndex; } @Override public int compareTo(Object o) { if(! (o instanceof Country)) throw new InputMismatchException("Country is expected"); Country other = (Country)o; if(other.CountryIndex > CountryIndex) return -1; else if(other.CountryIndex == CountryIndex) return 0; else return 1; } } |
}
更多信息请访问:http://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/
那不是一个排序列。用treemap代替。
这样它将自动排序