集合框架(list)
- ArrayList(数组集合)查询与修改快
迭代器
@SuppressWarnings({ "rawtypes", "unchecked" })
public class Demo1_Collection {
public static void main(String[] args) {
ArrayList<Student> list = new ArrayList<>();
list.add(new Student("张三", 23));
list.add(new Student("李四", 24));
list.add(new Student("王五", 25));
Iterator<Student> iterator = list.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
- 迭代器并发修改异常解决方案
@SuppressWarnings({ "rawtypes", "unchecked" })
public class Demo1_Collection {
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.add("a");
list.add("b");
list.add("c");
ListIterator listIterator = list.listIterator();
while(listIterator.hasNext()){
String string = (String)listIterator.next();
if("c".equals(string)){
listIterator.add("JavaEE");
}
}
System.out.println(list);
}
}
- LinkedList 增删快
@SuppressWarnings({ "rawtypes", "unchecked" })
public class Demo1_Collection {
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.add("a");
list.add("b");
list.add("c");
ListIterator listIterator = list.listIterator();
while(listIterator.hasNext()){
String string = (String)listIterator.next();
if("c".equals(string)){
listIterator.add("JavaEE");
}
}
System.out.println(list);
}
}
- ArrayList去除重复内容 当我们重写数据模型equals方法后也可以通过这种方式去重引用类型内容
@SuppressWarnings({ "rawtypes", "unchecked" })
public class Demo1_Collection {
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.add("a");
list.add("b");
list.add("b");
list.add("c");
list.add("c");
list.add("b");
list.add("b");
list.add("a");
System.out.println(getSingle(list));
}
public static ArrayList getSingle(ArrayList list) {
ArrayList newList = new ArrayList<>();
Iterator it = list.iterator();
while (it.hasNext()) {
Object obj = (Object)it.next();
if (!newList.contains(obj)) {
newList.add(obj);
}
}
return newList;
}
}
网友评论