面向对象-List

ArrayList去除自定义对象元素重复3

class Student {
    String name;
    int age;

    Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

/*  @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + "]";
    }*/


    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Student other = (Student) obj;
        if (age != other.age)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }
    

}



public class CollectionDemo {
    
    static ArrayList getSingleEle(ArrayList list) {
        ArrayList newList = new ArrayList();
        ListIterator it  = list.listIterator();
        while(it.hasNext()) {
            Object obj = it.next();
            if(!newList.contains(obj)) { // contains 有没有相同的元素(equals) --->Object-->地址
                newList.add(obj);
            }
        }
        return newList;
    }
    
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static void main(String[] args) {

        ArrayList list = new ArrayList();
        list.add(new Student("zs", 18));
        list.add(new Student("zs", 18));
        list.add(new Student("ls", 19));
        System.out.println(list);

        ArrayList newList = getSingleEle(list);
        System.out.println(newList);

    }
}

LinkedList数据结构分析
链表:多个节点组成的
节点:是包含自己和前节点地址,后节点地址
链表插入和删除是比较快的
链表缺点:查找和修改是比较慢的


LinkedList特有方式

public class LinkedListDemo1 {

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static void main(String[] args) {

        LinkedList list = new LinkedList();
        list.add("a");
        list.add("b");
        list.add("c");

        System.out.println(list);//[a, b, c]
        /*      ListIterator it = list.listIterator();
                while(it.hasNext()) {
                    System.out.println(it.next());
                }*/

        特有方法
        往第一个位置添加元素
        list.addFirst("wzb");
        list.addFirst("1");
        System.out.println(list);//[1, wzb, a, b, c]

        在集合的最后添加一个元素
        list.addLast("uio");
        System.out.println(list);//[1, wzb, a, b, c, uio]

        移除第一个元素
        list.removeFirst();
        System.out.println(list);//[wzb, a, b, c, uio]

        移除最后一个元素
        list.removeLast();
        System.out.println(list);//[wzb, a, b, c]
        
        LinkedList查找是比较慢的
        System.out.println(list.get(0));
    }
    
}


LinkedList实现栈结构内存

class Stack{
    
    LinkedList linkedList;
    Stack(){
        
         linkedList = new LinkedList();
        
    }
    //1.入栈:在集合最后添加一个元素
    void push (Object obj) {
        linkedList.addLast(obj);
    }
    //1.出栈:在集合最后一个元素移除
    void pop () {
        linkedList.removeLast();
    }
    @Override
    public String toString() {
        return linkedList.toString();
    }
    
}

public class LinkedListDemo2 {
    
    public static void main(String[] args) {
        Stack s = new Stack();
        s.push("a");
        s.push("b");
        s.push("c");
        System.out.println(s);//[a, b, c]
        
        s.pop();
        System.out.println(s);//[a, b]

    }

}

Vector特有方法

public class VectorDemo {
    
    private static Enumeration Enumeration;

    public static void main(String[] args) {
        
        //使用的很少,都使用ArrayList  从1.2才并到List
        Vector vc = new Vector();
        vc.add("a");
        vc.add("b"); //synchronized synchronized方法都会加锁  更安全
        System.out.println(vc);
        
        //特有的一些方法  在1.2之前添加元素
        vc.addElement("c");
        System.out.println(vc);
        
        //获取所有元素
        Enumeration e =vc.elements();
        while(e.hasMoreElements()) {
            System.out.println(e.nextElement());//a b c
        }
        
        
    }

}

List总结

image

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在一个方法内部定义的变量都存储在栈中,当这个函数运行结束后,其对应的栈就会被回收,此时,在其方法体中定义的变量将不...
    Y了个J阅读 9,864评论 1 14
  • 今日任务 1、List接口介绍(掌握常用List特有方法)2、练习3、ArrayList介绍(必须清楚集合的特征、...
    Villain丶Cc阅读 4,711评论 1 1
  • 今日小确幸 给一位陌生的小姐姐打电话,诉说了我心里的烦闷,她很理解并开导了我,感谢生命中遇到的这些美好的人儿。 今...
    mg依莲阅读 1,235评论 0 0
  • 疲倦 却不烦闷焦躁 因为我心甘情愿地 为爱 为你到不知极限
    小城蜉蝣阅读 1,365评论 0 0
  • 体验时间:2017年12月8日 体验者:周勇 影城名称:内江万达影城 职位:服务员 导语:肯德基隶属于百胜中...
    内江万达影城阅读 4,540评论 0 0