文章目录
- ArrayList和Vector的区别
- ArrayList的常用方法
- 遍历打印ArrayList元素
- 添加删除元素
- ArrayList转化为数组的方法
- 其他方法(取元素、替换元素、排序、判断是否存在、是否为空等)
- Vector常用方法
- 遍历打印Vector元素
- 添加删除插入元素
- Vector转化为数组的方法
- 其他方法(取元素、替换元素、索引等)
ArrayList和Vector的区别
初始大小和超过容量时扩容
public class ArrayListAndVector {
public static void main(String[] args) throws Exception {
ArrayList<Object> arrayList = new ArrayList<Object>();
Vector<Object> vector = new Vector<Object>();
for (int i = 0; i < 50; i++) {
print(arrayList);
arrayList.add(i);
print(vector);
vector.add(i);
}
}
@SuppressWarnings("rawtypes")
static void print(List list) throws Exception {
Class c = list.getClass();
Field elementDataField = c.getDeclaredField("elementData");
elementDataField.setAccessible(true);
Object[] arr = (Object[]) elementDataField.get(list);
System.out.println(c + ".length = " + arr.length);
}
}
部分代码来源
构造方法
ArrayList<Object> arrayList1 = new ArrayList<>();
ArrayList<Object> arrayList2 = new ArrayList<>(10);
ArrayList<Object> arrayList3 = new ArrayList<>(list);
Vector<Object> vector1 = new Vector<>();
Vector<Object> vector2 = new Vector<>(15);
Vector<Object> vector3 = new Vector<>(20, 2);
Vector<Object> vector4 = new Vector<>(list);
ArrayList的常用方法
遍历打印ArrayList元素
String[] strings = new String[] {"first", "second", "third", "fourth"};
List<String> list = Arrays.asList(strings);
ArrayList<Object> arrayList = new ArrayList<>(list);
System.out.println("迭代器遍历:");
Iterator<Object> iterator = arrayList.iterator();
while (iterator.hasNext()) {
System.out.print(iterator.next() + ",");
}
System.out.println();
System.out.println("通过索引遍历:");
for (int i = 0; i < arrayList.size(); i++) {
System.out.print(arrayList.get(i) + ",");
}
System.out.println();
System.out.println("for循环遍历:");
for (Object object : arrayList) {
System.out.print(object + ",");
}
System.out.println();
System.out.println("forEach遍历:");
arrayList.forEach(object -> System.out.print(object + ","));
System.out.println();
添加删除元素
System.out.println("ArrayList 添加元素方法:");
arrayList.add(0, "first element");
System.out.println("在0号位添加‘first element’:" + arrayList);
arrayList.add("second element");
System.out.println("在末尾添加‘second element’:" + arrayList);
arrayList.addAll(1, list);
System.out.println("在1号位添加list:" + arrayList);
arrayList.addAll(list);
System.out.println("在末尾添加list:" + arrayList);
System.out.println();
System.out.println("ArrayList 删除元素方法:");
arrayList.remove(2);
System.out.println("删除2号位元素:" + arrayList);
arrayList.remove("first");
System.out.println("删除第一个‘first’位元素:" + arrayList);
arrayList.removeAll(list);
System.out.println("删除list中含有的元素:" + arrayList);
arrayList.clear();
ArrayList转化为数组的方法
System.out.println("toArray的使用:");
Object[] objects = arrayList.toArray(new Object[0]);
for (Object object : objects) {
System.out.print(object + ",");
}
System.out.println();
Object[] objects2 = new Object[arrayList.size()];
arrayList.toArray(objects2);
for (Object object : objects2) {
System.out.print(object + ",");
}
System.out.println();
其他方法(取元素、替换元素、排序、判断是否存在、是否为空等)
System.out.println("复制后生成的Object:" + arrayList.clone());
System.out.println("arrayList的第二个元素是:" + arrayList.get(1));
System.out.println("第一个‘second’在arrayList的位置是:" + arrayList.indexOf("second"));
System.out.println("最后一个‘second’在arrayList的位置是:" + arrayList.lastIndexOf("second"));
arrayList.retainAll(list);
System.out.println("仅保留list中有的元素后:" + arrayList);
arrayList.set(1, "2");
System.out.println("替换1号位元素为‘2’后:" + arrayList);
System.out.println("ArrayList有" + arrayList.size() + "个元素");
arrayList.sort(new Comparator<Object>() {
public int compare(Object o1, Object o2) {
return (o1.toString().charAt(0) - o2.toString().charAt(0));
};
});
System.out.println("排序后:" + arrayList);
System.out.println("arrayList里面是否含有'first':" + arrayList.contains("first"));
System.out.println("arrayList是否为空:" + arrayList.isEmpty());
System.out.println("截取0~2的ArrayList元素:" + arrayList.subList(0, 2));
arrayList.trimToSize();
Vector常用方法
遍历打印Vector元素
Vector<Object> vector = new Vector<>(list);
System.out.println("迭代器遍历:");
Iterator<Object> iterator1 = vector.iterator();
while (iterator1.hasNext()) {
System.out.print(iterator1.next() + ",");
}
System.out.println();
System.out.println("通过索引遍历:");
for (int i = 0; i < vector.size(); i++) {
System.out.print(vector.get(i) + ",");
}
System.out.println();
System.out.println("for循环遍历:");
for (Object object : vector) {
System.out.print(object + ",");
}
System.out.println();
System.out.println("forEach遍历:");
vector.forEach(object -> System.out.print(object + ","));
System.out.println();
添加删除插入元素
System.out.println("Vector 添加元素方法:");
vector.add(0, "first element");
System.out.println("在0号位添加‘first element’:" + vector);
vector.add("second element");
System.out.println("在末尾添加‘second element’:" + vector);
vector.addElement("addElement");
System.out.println("用addElement方法:" + vector);
vector.addAll(1, list);
System.out.println("在1号位添加list:" + vector);
vector.addAll(list);
System.out.println("在末尾添加list:" + vector);
System.out.println();
System.out.println("Vector 插入元素方法:");
vector.insertElementAt("这是插入的元素", 1);
System.out.println("在1号位插入元素:" + vector);
System.out.println();
System.out.println("vector 删除元素方法:");
vector.remove(2);
vector.removeElementAt(2);
System.out.println("删除2号位元素:" + vector);
vector.remove("first");
vector.removeElement("first");
System.out.println("删除第一个‘first’位元素:" + vector);
vector.removeAll(list);
System.out.println("删除list中含有的元素:" + vector);
vector.clear();
vector.removeAllElements();
Vector转化为数组的方法
System.out.println("toArray的使用:");
Object[] objects3 = arrayList.toArray(new Object[0]);
for (Object object : objects3) {
System.out.print(object + ",");
}
System.out.println();
Object[] objects4 = new Object[arrayList.size()];
arrayList.toArray(objects4);
for (Object object : objects4) {
System.out.print(object + ",");
}
System.out.println();
其他方法(取元素、替换元素、索引等)
vector.capacity();
vector.get(1);
vector.elementAt(1);
vector.firstElement();
vector.lastElement();
vector.size();
vector.setSize(10);
vector.indexOf("first");
vector.indexOf("first", 2);
vector.lastIndexOf("last");
vector.lastIndexOf("last", 2);
vector.set(1, "这是一号位");
vector.setElementAt("这是一号位", 1);
vector.contains("first");
vector.containsAll(list);
vector.equals(new Vector<>());
vector.retainAll(list);
vector.subList(2, 3);
vector.clone();
vector.copyInto(new Object[0]);
vector.toString();
vector.isEmpty();
vector.trimToSize();