Java ArrayList??????????
???????????? ???????[ 2016/1/21 14:01:04 ] ??????????????? Java
?????????書(shū)???????????ArrayList?? Java ?????????????????????????ArrayList ????? List ??????????????????????????洢??????? ArrayList ?????????????????????ж?????????????????????????ù? ArrayList?????????????????????????? ArrayList ??????????????ο????? API ??????????????????? ArrayList ????л??????????
????In this post?? I will discuss one of the most important operation on ArrayList that you will most likely require implementing during enterprise application development. It’s sorting the elements of an ArrayList.
??????????????У???????? ArrayList ??????????????????????п?????????????????????????????? ArrayList ????????
?????????????????? ArrayList
??????????? ArrayList ?洢??????????????????????country name??????????? ArrayList ????????????????? Collections.sort()???????????????????? ArrayList ???????????????????????????????????????????????????????????????д??δ???
????SortArrayListAscendingDescending.java
package guru.springframework.blog.sortarraylist.ascendingdescending;
import java.util.ArrayList;
import java.util.Collections;
public class SortArrayListAscendingDescending {
private ArrayList arrayList;
public SortArrayListAscendingDescending(ArrayList arrayList) {
this.arrayList = arrayList;
}
public ArrayList getArrayList() {
return this.arrayList;
}
public ArrayList sortAscending() {
Collections.sort(this.arrayList);
return this.arrayList;
}
public ArrayList sortDescending() {
Collections.sort(this.arrayList?? Collections.reverseOrder());
return this.arrayList;
}
}
??????????????У?????????????г????????? ArrayList ?????? sortAscending()?????У?????????? Collections.sort()?????????????????????? ArrayList????????????????????? ArrayList???? sortDescending()?????У????????????? Collections.sort()?????????????????????????汾?? Collections.sort()????ArrayList??????????????????????? Collections.reverseOrder()????????? Comparator ???????????????????????????????? Comparator????????????????????д??β??????
????SortArrayListAscendingDescendingTest.java
package guru.springframework.blog.sortarraylist.ascendingdescending;
import org.junit.Test;
import java.util.ArrayList;
import static org.junit.Assert.*;
public class SortArrayListAscendingDescendingTest {
<a href="http://www.jobbole.com/members/madao">@Test</a>
public void testSortAscendingDescending() throws Exception {
ArrayList countryList = new ArrayList<>();
countryList.add("France");
countryList.add("USA");
countryList.add("India");
countryList.add("Spain");
countryList.add("England");
SortArrayListAscendingDescending sortArrayList = new SortArrayListAscendingDescending(countryList);
ArrayList unsortedArrayList = sortArrayList.getArrayList();
System.out.println("Unsorted ArrayList: " + unsortedArrayList);
ArrayList sortedArrayListAscending = sortArrayList.sortAscending();
System.out.println("Sorted ArrayList in Ascending Order : " + sortedArrayListAscending);
ArrayList sortedArrayListDescending = sortArrayList.sortDescending();
System.out.println("Sorted ArrayList in Descending Order: " + sortedArrayListDescending);
}
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11