Java-Vector类和linkedList类

Vector类

  • Vector 类可以实现可增长的对象数组。与数组一样,它包含可以使用整数索引进行访问的组件。但是,Vector 的大小可以根据需要增大或缩小,以适应创建 Vector 后进行添加或移除项的操作。

  • Vector类中有多新的方法,例如addElement添加组件,elementAt()返回指定索引,elements()返回此向量组件的枚举,insertElementAt()将指定的对象在指定的index vector的成员等等,可以根据API进行查看使用。

  • Vector中有一个特有的遍历方式

 Vector<String> ve = new Vector<>();
        ve.addElement("a");
        ve.addElement("b");
        ve.addElement("c");
        ve.addElement("d");
        ve.addElement("e");
        Enumeration<String> elements = ve.elements();
        while(elements.hasMoreElements()){
            String s = elements.nextElement();
            System.out.print(s+" ");//a b c d e 
        }
    }

LinkedList类

  • List 接口的链接列表实现。实现所有可选的列表操作,并且允许所有元素(包括 null)。除了实现 List 接口外,LinkedList 类还为在列表的开头及结尾 get、remove 和 insert 元素提供了统一的命名方法。这些操作允许将链接列表用作堆栈、队列或双端队列。

  • LinkedList类中也有特有的功能,如下

LinkedList<Integer> link = new LinkedList<>();
        link.add(1);
        link.add(2);
        link.add(3);
        link.add(4);
        System.out.println(link);//[1, 2, 3, 4]

//          void addFirst(E e)
//          在此列表的开始处插入指定的元素。
//          void addLast(E e)
//          将指定的元素列表的结束。

        link.addFirst(666);
        link.addLast(999);
        System.out.println(link);//[666, 1, 2, 3, 4, 999]

//        getFirst()
//        返回此列表中的第一个元素。
//        E getLast()
//        返回此列表中的最后一个元素。

        System.out.println(link.getFirst());//666
        System.out.println(link.getLast());//999


//        boolean offer(E e)
//        将指定的元素添加到列表的尾部(最后一个元素)。
//        boolean offerFirst(E e)
//        在列表的前面插入指定的元素。
//        boolean offerLast(E e)
//        在列表的结尾插入指定的元素。

        link.offer(123);
        System.out.println(link);//[666, 1, 2, 3, 4, 999, 123]
        link.offerFirst(555);
        link.offerLast(456);
        System.out.println(link);//[555, 666, 1, 2, 3, 4, 999, 123, 456]

//      void push(E e)将一个元素推到由该列表所表示的堆栈上。
        link.push(444);
        System.out.println(link);//[444, 555, 666, 1, 2, 3, 4, 999, 123, 456]

    }

还有许多的方法可以通过API进行查看。

  • 如何去除ArrayList集合中字符串的重复值
ArrayList<String> people = new ArrayList<>();
        people.add("周杰伦");
        people.add("彭于晏");
        people.add("胡歌");
        people.add("刘浩然");
        people.add("周杰伦");
        people.add("彭于晏");
        people.add("胡歌");
        people.add("刘浩然");

        ArrayList<String> name = new ArrayList<>();
        for(int i=0;i<people.size();i++){
            String s = people.get(i);
            if(!name.contains(s)){
                name.add(s);
            }
        }
        System.out.println(name);

    }

以上是一种情况,但是还有以下这种情况

//有一个People的类

 ArrayList<People> people = new ArrayList<>();
        people.add(new People("胡歌",35));
        people.add(new People("彭于晏",34));
        people.add(new People("周杰伦",30));
        people.add(new People("刘浩然",20));
        people.add(new People("周杰伦",30));
        people.add(new People("刘浩然",20));

        ArrayList<People> name = new ArrayList<>();
        for(int i=0;i<people.size();i++){
            People p = people.get(i);
            if(!name.contains(p)){
                name.add(p);
            }
        }
        System.out.println(name);

    }

//以下为结果,会发现并没有去除重复的内容
//[People{name='胡歌', age=35}, People{name='彭于晏', age=34}, People{name='周杰伦', age=30}, People{name='刘浩然', age=20}, People{name='周杰伦', age=30}, People{name='刘浩然', age=20}]

//这是因为contains方法调用的equals方法,但是equals并没有重写,虽然我们认为对象的成员变量都是一样的,可是计算机并不这样认为,从而比较的就是地址值,每创造一个对象,对象的地址值都会不一样,所以我们需要在People类中重写equals方法

 @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        People people = (People) o;
        return age == people.age &&
                Objects.equals(name, people.name);
    }


//在运行一次的结果
//[People{name='胡歌', age=35}, People{name='彭于晏', age=34}, People{name='周杰伦', age=30}, People{name='刘浩然', age=20}]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值