java集合

来源:互联网 发布:阿里云隐藏域名信息 编辑:程序博客网 时间:2024/06/05 03:24

Java 集合
会自动扩容
ArrayList
优点:底层使用数组储存,连续 遍历元素和随机访问效率高,
缺点:添加和删除效率低

建一个学生类
package com.sxt.array;

public class Student {
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return “名字:”+name+” 年龄:”+age;
}
@Override
public boolean equals(Object obj) {
if(obj==null)return false;
if(obj instanceof Student){
Student s = (Student)obj;
return this.name.equals(s.name)&&this.age==s.age;
//这里也可以换成 return this.toString().equals(obj.toString());
因为已经从写了toString方法
}
return super.equals(obj);

}写测试类package com.sxt.array;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Array {

public static void main(String[] args) {   List list =new ArrayList();   list.add(new Student("张三",18));//使用add方法添加人   list.add(new Student("李四",20));   list.add(new Student("刘伟",22));   list.add(new Student("张强",19));   //增强for循环   for(Object s:list){       System.out.println(s);   }   System.out.println("============="); //使用删除方法,删除第一个人   list.remove(0);    //迭代器   Iterator iterator = list.iterator();   while(iterator.hasNext()){       System.out.println(iterator.next());      }   System.out.println("=========");  //把第一位学生换成赵帅   list.set(0, new Student("赵帅",20));   //查找学生刘伟是否在班级中   注意,这里要从写equals方法、否则结果不对   boolean a=list.contains(new Student("刘伟",22));   System.out.println(a);   print(list);   //删除所有学生   list.clear();   print(list);   System.out.println("hello");}private static void print(List list) {    for(int i=0;i<list.size();i++){        Student s = (Student) list.get(i);        System.out.println(s);    }}}名字:张三 年龄:18

名字:李四 年龄:20
名字:刘伟 年龄:22

名字:张强 年龄:19

名字:李四 年龄:20
名字:刘伟 年龄:22

名字:张强 年龄:19

true
名字:赵帅 年龄:20
名字:刘伟 年龄:22
名字:张强 年龄:19
hello

LinkedList
底层用链表储存,不连续,
优点:插入和删除效率高,
缺点:便利和随机访问元素效率低下
新建学生类
package com.sxt.list;

public class Student {
private String name ;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {

    return "名字:"+name+" 年龄:"+age;}@Overridepublic boolean equals(Object obj) {   if(obj==null)return false;   if(obj instanceof Student){      Student s=(Student)obj;      return this.toString().equals(s.toString());   }    return false;}

}
写测试类
package com.sxt.demo1;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import com.sxt.list.Student;

public class Demo {

public static void main(String[] args) {    List list=new LinkedList<>();    list.add(new Student("张三",19));    list.add(new Student("刘强",20));    list.add(new Student("赵帅",23));    list.add(new Student("宋刚",18));    list.add(new Student("刘凯",22));    List temp=new ArrayList<>();    temp.add(new Student("刘强",20));    temp.add(new Student("赵帅",23));    temp.add(new Student("宋刚",18));    temp.add(new Student("李伟",19));    List temp1 = new ArrayList<>();    temp1.add(new Student("赵帅",23));    temp1.add(new Student("宋刚",18));    //是否包含指定元素    System.out.println(list.contains(new Student("abc",20)));    //包含所有元素    System.out.println(list.containsAll(temp1));    //指定位置添加  黄琪    list.set(2, new Student("黄琪",20));    Iterator iterator = list.iterator();//迭代器    while(iterator.hasNext()){        System.out.println(iterator.next());    }    System.out.println("=========");    //删除一样的部分、、也必须从写equals方法,否则结果有误    list.removeAll(temp);    print(list);}private static void print(List list) {    for(int i=0;i<list.size();i++){        System.out.println(list.get(i));    }}

}