1.数组
长度不可变, 可以声明任意类型数组, 一旦确定数据类型则,只能存储相同的类型或者是自动类型转换过去的数据
引用数据类型 容器
集合:容器 , 长度可变, 可以存储任意类型对象(*****),引用数据类型
public class Test {
public static void main(String[] args) {
Collection coll = new ArrayList();
coll.add("a");
coll.add(new Object());
coll.add(1);
coll.add(true);
coll.add(6);
System.out.println(coll.size());
Collection coll2 = new ArrayList();
coll2.add(6);
coll2.add(7);
coll2.add(9);
coll.addAll(coll2);
System.out.println(coll);
//判断的方法
//判断集合中是否包含4
boolean b = coll.contains(7);
System.out.println(b);
//clear清空
// coll.clear();
//判断集合中是否为空
boolean c = coll.isEmpty();
System.out.println(c);
//remove删除
// coll.forEach(6);有多个符合条件的也只删除第一个
System.out.println(coll);
//会将集合中所有符合条件的全部删除
coll.removeAll(coll2);
System.out.println(coll);
//间接遍历循环集合的一种方式
Object[] array = coll.toArray();
for (Object object : array) {
System.out.println(object);
}
//直接遍历集合
for (Object object : array) {
System.out.println(object);
}
// Iterator迭代器
//遍历集合
//1.先得到迭代器的对象
Iterator it = coll.iterator();
//2.判断是否有下一个元素
while (it.hasNext()) {
//3.next()获取下一个元素,并且强制向后移动一位
Object obj = it.next();
System.out.println(obj);
}
}
}
ConcurrentModificationException并发修改异常
利用增强for便利的
1.献给集合创建一个副本clone,真正遍历的是副本
2.遍历副本过程中 修改了原来集合
3.在遍历过程中不要删除元素,或者新增元素
public class Test2 {
public static void main(String[] args) {
Collection coll = new ArrayList();
coll.add(1);
coll.add(2);
coll.add(14);
coll.add(3);
coll.add(15);
coll.add(6);
for (Object object : coll) {
// 判断object是不是Integer,避免ClassCastException
if (object instanceof Integer) {
Integer integer = (Integer) object;
int i = integer;//自动拆箱
if(i>10) {
//修改了原来集合的长度
coll.remove(object);
}
}
}
//适用于遍历过程中删除元素
//1.得到迭代器对象
Iterator it = coll.iterator();
//2.循环执行hasNext
while (it.hasNext()) {
//3.next()获取下一个元素
Object obj = it.next();
if(obj instanceof Integer) {
Integer integer = (Integer) obj;
int i = integer;//自动拆箱
if(i>10) {
//删除的是副本的元素
it.remove();
}
}
}
System.out.println(coll);
}
}
2.List
有序并且允许重复
ArrayList: 有序并且允许重复
LinkedList: 有序并且允许重复
有序: 展示的顺序和存入的顺序是相同的, 就是有序
ArrayList: 底层是大小可变的数组
初始长度是10 , 如果有特殊需求 开始就要存储大量数据, 则通过构造方法单独指定初始容量
public class Test3 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>(1000);
list.add("z");
list.add("a");
list.add("c");
list.add("b");
list.add("b");
System.out.println(list);
//指定下标位置进行插入元素
list.add(1,"zhangsan");
System.out.println(list);
//指定下标位置删除
list.remove(1);
System.out.println(list);
ArrayList<Integer> list2 = new ArrayList<>();
list2.add(2);
list2.add(3);
list2.add(11);
list2.add(5);
list2.remove(new Integer(5));
System.out.println(list2);
//get(int index)获取指定下标上的元素
for (int i = 0; i < list.size(); i++) {
String str = list.get(i);
System.out.println(str);
}
//set指定下标进行修改
list.set(1, "www");
System.out.println(list);
//找到第一次出现的下标和最后一次出现的下标
list.indexOf("b");
list.lastIndexOf("b");
//截取集合中一部分元素
List<String> list2 = list.subList(1, 4);//左闭右开
System.out.println(list2);
}
}
3.垃圾回收
public class Test {
public static void main(String[] args) {
//退出jvm
// System.exit(0);
System.out.println("hehe");
System.out.println("xixi");
//标准输入
// System.in;
long l = System.currentTimeMillis();
System.out.println(l);
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < 10000; i++) {
buffer.append("aaa-");
}
long l2 = System.currentTimeMillis();
System.out.println("消耗时间:"+(l2-l));
Runtime runtime = Runtime.getRuntime();
//获取空闲内存
System.out.println("gc前"+runtime.freeMemory());
//运行垃圾回收期
System.gc();
System.out.println("gc后:"+runtime.freeMemory());
}
}