List集合
1、Collection方法、迭代器
集合框架的由来:数据多了用对象进行存储,对象多了用集合来进行存储。
Collection常用方法 如图所示:
迭代器 Iterator
举个例子:把夹娃娃机比作集合;那么夹娃娃的夹子就可以看成集合中的迭代器;夹娃娃必须通过娃娃机内部的夹子;
而钩子的外挂、移动方式等等都不相同,所以只能定义一个规则,而这个规则就可以看成几何内部的一个迭代器接口Iterator;
for循环遍历
public class CollectionDemo{
public static void main(String[] args) {
Collection c=new ArrayList<>();
c.add(22);
c.add(23);
c.add(25);
c.add(28);
c.add(55);
//增强for循环
for (Object obj : c) {
System.out.println(obj);
}
}
}
迭代器是集合所特有的遍历方式
public class CollectionDemo{
public static void main(String[] args) {
Collection c=new ArrayList<>();
c.add(22);
c.add(23);
c.add(25);
c.add(28);
c.add(55);
Iterator it=c.iterator();
while(it.hasNext()) {
int num=(int) it.next();
if(num%2==0) {
System.out.println(it.next());
}
}
}
}
2、ArrayList特有方法、特有迭代器、具体对象特点、增长因子论证
集合与数组的区别
- list的长度可变,数组长度固定
- list可以存放各类元素对象,而数组一旦,只能存放对应的类型
public class ListDemo {
public static void main(String[] args) {
List c=new ArrayList<>();
c.add(22);
c.add(23);
c.add(25);
c.add(28);
c.add(55);
Iterator it=c.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
while(it.hasPrevious()) {
System.out.println(it.previous());
}
}
}
- List:凡是可以操作角标的方法都是该体系所特有的方法
- 增
- Add(index,element)
- Add(index,Collection)
- 删
- Remove(index)
- 改
- Set(index,element)
- 查
- Get(index)
- subList(from,to)
- listIterator()
- index(element)
论证增长因子:
1,Arraylist 数组结构 增删慢,查询快 有连续下标 线程不同步 增长因子为1.5
2,vector 数组结构 增删改查都慢 有连续下标 线程同步 增长因子 2
3,Linkedlist 链表结构 增删快,查询慢 没有连续下标
ArrayList 如何进行性能调优?
public class ListDemo2 {
public static void main(String[] args) {
ArrayList al=new ArrayList<>(50);
for(int i=1;i<=88;i++) {
al.add(i);
System.out.print(i+",");
getLen(al);
}
}
public static void getLen(ArrayList al) {
try {
Field f=al.getClass().getDeclaredField("elementData");
f.setAccessible(true);
Object obj=f.get(al);
Object [] elementData=(Object[]) obj;
System.out.println("当前al容器的底层数组的长度"+elementData.length);
} catch (NoSuchFieldException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3,LinkedList
堆栈结构(先进后出):
/**
* 通过LinkedList集合制作一个堆栈结构类容
* 获取制作一个队列结构容器
*
*/
public class LinkedListDemo {
public static void main(String[] args) {
DuiLie dz=new DuiLie();
dz.push("a");
dz.push("b");
dz.push("c");
dz.push("d");
dz.push("e");
dz.bianLi();
}
}
/**
* 堆栈结构的容器
*
*/
class DuiZhan{
private LinkedList ll=new LinkedList<>();
/**
* 往堆栈结构容器中添加元素
* @param obj
*/
public void push(Object obj) {
ll.addFirst(obj);
}
public Object pop() {
return ll.remove();
}
public void bianLi() {
Iterator it=ll.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
class DuiLie{
private LinkedList ll=new LinkedList<>();
/**
* 往堆栈结构容器中添加元素
* @param obj
*/
public void push(Object obj) {
ll.addLast(obj);
}
public Object pop() {
return ll.remove();
}
public void bianLi() {
Iterator it=ll.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
结果:
5、ArrayList中的重复元素去重及其底层原理
自定义去重复
/**
* 对ArrayList中的元素去重1,元素是字符串2,元素是自定义对象
*需求:
* 判断两个人是同一个的依据
* 名字相同年龄相同
*
* 集合collection的contains在调用的时候底层调用容器对象equals方法
* 之前元素对象
*/
public class ArrayListDemo {
public static void main(String[] args) {
ArrayList al=new ArrayList<>();
al.add(new Person("laohu",18));
al.add(new Person("laoqiu",21));
al.add(new Person("laochen",26));
al.add(new Person("laoliu",19));
al.add(new Person("laohu",28));
ArrayList newAl= repeat(al);
System.out.println(newAl.size());
}
/**
*ArrayList al这个容器中是有重复元素的?
*1.建立一个新容器
*2.将老的容器遍历取出来其中的元素
*3.如果说这个元素存在于新容器中,那么不在新容器加入。如果不存在,就加
* @param al
* @return
*/
public static ArrayList repeat(ArrayList al) {
ArrayList newAl=new ArrayList<>();
for (Object obj : al) {
if(!newAl.contains(obj)) {
newAl.add(obj);
}
}
return newAl;
}
}
class Person{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Person() {
super();
}
@Override
public boolean equals(Object obj) {
Person p=(Person) obj;
System.out.println(p.name+"----equals----"+this.name);
return p.name.equals(this.name) /*&& p.age==this.age*/;
}
}
字符串去重复
public class ArrayListDemo{
public static void main(String[] args) {
ArrayList al = new ArrayList<>();
al.add("laohu");
al.add("laoqiu");
al.add("laochen");
al.add("laoliu");
al.add("laohu");
System.out.println(al.size())
}
public static ArrayList repeat(ArrayList al) {
ArrayList newal = new ArrayList<>();
for (Object obj : al) {//循环
if(!newal.contains(obj)) {
newal.add(obj);
}
}
return newal;
}
}