-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVectorTest.java
More file actions
68 lines (57 loc) · 2.13 KB
/
VectorTest.java
File metadata and controls
68 lines (57 loc) · 2.13 KB
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
package CollectionTest;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;
public class VectorTest {
public static void main(String[] args) {
Vector vector = new Vector();
/*
Vector 是线程安全的
在构造函数里边可以提供初始容量,容量增长指数,默认每一次调整都会增长一倍
* */
vector.add(1);
vector.add(2);
vector.add(3);
Enumeration enumeration = vector.elements();
System.out.println(enumeration.nextElement());
System.out.println(enumeration.nextElement());
/*
public Enumeration<E> elements() {
return new Enumeration<E>() { 匿名类的使用
int count = 0;
public boolean hasMoreElements() {
return count < elementCount;
}
public E nextElement() {
synchronized (Vector.this) {
if (count < elementCount) {
return elementData(count++); // Count++
}
}
throw new NoSuchElementException("Vector Enumeration");
}
};
}
* */
//toArray()
/*
public synchronized <T> T[] toArray(T[] a) {
if (a.length < elementCount) //传入的数组的尺寸如果小于vector的长度,就返回一个扩大了的新数组
return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass());
System.arraycopy(elementData, 0, a, 0, elementCount);
if (a.length > elementCount)
a[elementCount] = null;
return a;
}
* */
Integer[] Array = new Integer[3];
vector.toArray(Array);
System.out.println(Array[2]);
//subList 继承了list的subList方法,相当于String的subString,截止到toIndex的前一位
List list = vector.subList(0, 2);
System.out.println(list);
//遍历方式
//1.interator遍历 2 随机访问 3.for循环 4.enumeration遍历
}
}