数组长度为0跟数组为空的区别
int[]a; //未被初始化
int[]b=null; //将数组指向null 打印length爆出空指针异常
int []c=new int[0];
// c[0]=1; //数组越界
System.out.println(c.length);//可以打印输出数组长度为0
System.out.println(c); //这个数组长度为0的是可以打印地址
System.out.println(b); //null型数组打印输出null
Collection col=new ArrayList();
System.out.println(col);// [] 打印输出一个空集合
col.add("a"); //可以调用接口中的add方法添加元素
for (int i = col.size()-1; i >0 ; i--) { //不要在for循环里面添加元素,集合个数会发生变化
((ArrayList) col).add(i,"b"); //直接跳过for循环
}
/* ((ArrayList) col).add(0,"b");//有索引的是Arraylist方法里面特有的
col.add("c"); //如果用索引的 必须保证目前集合有足够多的元素*/
System.out.println(col);
Collection集合里的retainAll方法 保留相同的元素
//retain方法是集合的方法
//retain(collection c)//
//保留调用者跟参数集合元素相同的元素;交集
//返回类型是boolean,若改变返回true
Collection col1=new ArrayList();
col1.add("123");
col1.add("456");
col1.add("789");
col1.add("abc");
Collection col2=new ArrayList();
col2.add("1123");
col2.add("1456");
col2.add("789");
col2.add("abc");
System.out.println(col2.retainAll(col1));
System.out.println(col2);
集合遍历的三种方式
1.将集合转换为Object类型的数组,使用集合.toArray转换。
//增强for循环遍历Object数组,即集合以数组形式遍历
Object[]o =collection.toArray();
System.out.println(o); //Object类型的数组
for (Object ob:o
) {
System.out.print(ob+" ");
}
2.将集合转换为迭代器Iterator,使用集合.iterator()方法返回一个迭代器
调用迭代器的判断函数和返回下一个元素的函数进行遍历
利用 hasNext判断
利用next()获取下一个集合的元素;
//集合变为迭代器,进行遍历
Iterator it = collection.iterator(); //迭代器是接口,只能调用集合方法返回一个迭代器
//使用while遍历循环
while (it.hasNext()){ // 判断集合是否还有下一个元素 初始是-1;
Object object=it.next(); //next返回集合下一个元素。 都是Object类型
System.out.print(object+" "); //循环打印输出集合每一个元素 自动调用toString方法
}
3.利用集合独有的特性,即通过集合元素的个数进行普通for循环
根据size()和集合名.get(int index)获取集合的每一个元素
set(int index)是对集合索引上的每一个位置进行赋值!!!!!
注意get 和 set的用法
//利用集合的元素个数进行普通for循环
for (int i = 0; i <collection.size() ; i++) {
Object oo=((ArrayList) collection).get(i); //ArrayList根据索引获取的元素值(Object)
System.out.print(oo+" ");
迭代器添加元素到集合报错
Iterator iterator =col1.iterator(); //集合添加元素 但是迭代器不知道呀
while (iterator.hasNext()){
Object ooo=iterator.next();
if ("456".equals(ooo)){ //ConcurrentModificationException修改并发异常
col1.add("hhhh");
}
}
System.out.println(col1);
使用ListIterator 迭代器
/ListIterator 是含有add方法的,可以加元素,千万不要用集合的添加
// 一定得要用迭代器的添加方法!!!!!!
//Iterators是没有add方法的,添加发生异常
ListIterator iterator =col1.listIterator(); //集合添加元素 但是迭代器不知道呀
while (iterator.hasNext()){
String ooo= (String) iterator.next();
if (ooo.equals("456")){ //ConcurrentModificationException修改并发异常
iterator.remove(); //注意 只有往迭代器加入数据 集合才有添加
}
注意通过迭代器来添加元素,得使用List的迭代器
1.使用list迭代器
2.使用迭代器的add和remove方法 iterator.remove()
3.千万别使用集合的add方法和remove方法,不然出现并发异常
对集合元素内容进行部分剪辑
1.采用remove(索引)来进行删除,一定得用while
//可以利用while循环对集合每个元素(字符串)进行匹配,如何删除元素中的一部分 再添加
int i=0;
while (i<list.size()){
String a = (String) list.get(i);
if (a.contains("def")){
String b= a.replaceAll("def","");
list.remove(i); //删除这个整体元素
list.add(i,b);//set //再回加这个Object类型的字符串回去到原来位置
}
else {
i++;
}
2.利用for循环对每个元素进行遍历,转成字符串
利用list.set(索引)将改变后的字符串返回给集合第i个位置的元素
利用spilt分割。
//可以利用while循环对集合每个元素(字符串)进行匹配,如何删除元素中的一部分 再添加
int i=0;
while (i<list.size()){
String a = (String) list.get(i);
if (a.contains("def")){
String b= a.replaceAll("def","");
list.remove(i); //删除这个整体元素
list.add(i,b);//set //再回加这个Object类型的字符串回去到原来位置
}
else {
i++;
}
使用正则表达式来进行匹配和分割。
来源:https://blog.csdn.net/weixin_44165237/article/details/99688852