1、Vector定义。
vector可实现自动增长的对象数组。
2、Vector与ArrayList的区别与联系。
联系:都是通过数组实现的。
区别:(1)vector支持线程的同步,即某一时刻只有一个线程能够写vector,避免多线程同时写而引起的不一致性,但实现同步需要很高的花费,因此,访问它比访问ArrayList慢。(2)当数组的大小不够的时候,需要重新建立数组,然后将元素拷贝到新的数组内,ArrayList和Vector的扩展数组的大小不同。
3、Vector的排序
public class SortVector {
static Scanner cin = new Scanner(System.in);
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
Vector<Integer> vector = new Vector<Integer>();
Random random = new Random();
for(int i = 0; i < 10; i++) {
vector.add(random.nextInt(10));
}
System.out.println(vector);
Collections.sort(vector,Collections.reverseOrder());
System.out.println(vector);
Collections.sort(vector);
System.out.println(vector);
}
}
[9, 3, 4, 2, 6, 5, 1, 0, 4, 0]
[9, 6, 5, 4, 4, 3, 2, 1, 0, 0]
[0, 0, 1, 2, 3, 4, 4, 5, 6, 9]
4、ArrayList里面的数据的排序
public class SortArrayListAscDes {
private ArrayList arrayList;
public SortArrayListAscDes(ArrayList arrayList) {
this.arrayList = arrayList;
}
public ArrayList getArrayList() {
return arrayList;
}
public ArrayList sortAscending(){ //升序排序方法
Collections.sort(this.arrayList);
return this.arrayList;
}
public ArrayList sortDescending(){ //降序排序方法
Collections.sort(this.arrayList,Collections.reverseOrder());
return this.arrayList;
}
}
public class SortArrayListAscDecTest {
public static void main(String[] args) {
ArrayList stringArrayListist = new ArrayList<>();
stringArrayListist.add("Itlay");
stringArrayListist.add("China");
stringArrayListist.add("America");
stringArrayListist.add("Russian");
SortArrayListAscDes sortArrayListAscDes = new SortArrayListAscDes(stringArrayListist);
System.out.println("没有经过排序的数组: "+stringArrayListist);
System.out.println("升序排序: "+ sortArrayListAscDes.sortAscending());
System.out.println("降序排序: "+ sortArrayListAscDes.sortDescending());
}
}
没有经过排序的数组: [Itlay, China, America, Russian]
升序排序: [America, China, Itlay, Russian]
降序排序: [Russian, Itlay, China, America]