集合之List

List集合

1.List的功能

有序的collection(也称为序列)。此接口的用户可以对列表的每个元素的插入位置进行精确地控制。用户可以根据元素的整数索引(在列表中的位置)访问元素,并搜索列表中的元素。
特点:
**有序
**可重复
**可为空
**有下标
案例一:List集合存储学生对象并进行遍历
注意:ArryList<>底层结构是数组,所以集合的元素是有角标的
集合可以会存储 多泛型但是大多数情况下,集合会存储为泛型结构,这样做有两个好处。
1.限定集合只能存储一种类型的元素
2.避免了向下转型
代码:
public class Test {
    public static void main( String[] args) {
        List<String>list=new ArrayList<String>();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");
        list.add(0,"e");
        System.out.println(list.toString());
        //按角标移除
        list.remove(0);
        System.out.println(list.toString());
        //按角标获取元素
        System.out.println(list.get(0));//[a, b, c, d]
        list.set(list.size()-1,"o");//a
        System.out.println(list.toString());//[a, b, c, o]

//注意:记住每次编程前先选择编码格式,大多数用utf-8,迭代器是遍历集合的一种方法
    }
}
结果:
[e, a, b, c, d]
[a, b, c, d]
a
[a, b, c, o]

2.List集合的特有功能概述

void add(int index,E element)//在指定所引处添加元素 ,如果这个索引处的元素本来就存在那么这个新的值将会覆盖这个旧值。
E remove(int index )//移除指定角标处的元素,返回的是移除的元素
E get(int index)//获取指定索引处的元素
E set(int index,E element)//更改指定索引处的元素,返回的是被替换的元素
3.遍历List集合的三种方法,以下就是具体的实现方法,其结果都是一样
代码:
public class Test2 {
    public static void main(String[] args) {
        //遍历list
        List<String> list=new ArrayList<String>();
        String s1="a";
        String s2="b";
        String s3="c";
        String s4="d";
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);
        //方法一;用方法自己特有的
        ListIterator<String> str = list.listIterator();
        while (str.hasNext()){
            System.out.println(str.next());
        }
        //方法二:用公用的方法
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
        //方法三:用for循环
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));

        }

    }
}
结果:
a
b
c
d
a
b
c
d
a
b
c
d
总结:遍历List集合有三种方式
*调用iterator
*用ListIterator,他继承自iterator,可以使用它中间的方法。

3.反向输出集合的数据元素

先看一个图


方法:hashprvious():判断上一个元素是否存在
       previous():返回列表中的上一个元素,它是Listlterator中的特有功能
      这两个方法的使用和hashnext()和next()方法相同,只是遍历的方向不同。
代码:
public class Test3 {

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        String string = "我";
        String string1 = "爱";
        String string2 = "你";
        list.add(string);
        list.add(string1);
        list.add(string2);
        //反向遍历,先把”指针“指向最后一个元素反向遍历,然后再去
        ListIterator<String> stringListIterator = list.listIterator();
        while (stringListIterator.hasNext()) {

            System.out.print(stringListIterator.next());
        }
        while (stringListIterator.hasNext()) {
            stringListIterator.next();
        }
        System.out.println(" ");
        System.out.println("反向输出为");
        while (stringListIterator.hasPrevious()) {

            System.out.print(stringListIterator.previous());

        }
    }
}
结果:
我爱你 
反向输出为
你爱我

4.案例2:一个集合中有没有world.有的话就给集合添加一个元素"java"

代码:
public class Test4 {
    public static void main(String[] args) {
        String s1 = "love";
        String s2 = "world";
        String s3 = "life";
        List<String> list = new ArrayList<String>();
        list.add(s1);
        list.add(s2);
        list.add(s3);
//        //方法一,用公用的方法itrator()方法
//        Iterator<String> iterator = list.iterator();
//        while (iterator.hasNext()) {
//            if (iterator.next().equals("world")) {
//                list.add("java");//Exception in thread "main" java.util.ConcurrentModificationException
//            }
//        }
        //  方法二:用迭代器自己的方法,此时删除和添加不会出现异常
//        ListIterator<String> stringListIterator = list.listIterator();
//        while (stringListIterator.hasNext()) {
//            if (stringListIterator.next().equals("world")) {
//                stringListIterator.add("java");
//            }
//        }
        //[love, world, java, life]
        System.out.println(list.toString());
        //方法三:用for()循环可以解决异常
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).equals("world")) {
                list.add("java");
            }

        }
        System.out.println(list.toString());
        //[love, world, life, java]
    }
}




结果:注意当我们用iterator去遍历时,就会发现有异常,但是方法三和方法二就可以。原因是因为,用迭代器iterator来遍历时,采用hashnext()和next()方法时,会出现ConcurrentModificationException,迭代依赖于集合,当我们定义好一个集合时,此时用迭代器去遍历时,则集合的大小就已经确定,被迭代器所知,此时调用集合的add()和remove(0)方法时,迭代器就不允许,所以会出现异常。
解决方案:
*迭代器调用自己 的方法
*用for()循环
两种方法结果一样,见代码中的法二法三。只是有个小小的区别,当调用迭代器自己的方法时,是直接加在world之后,而用for()循环时,就加在集合的最后边。

5.list三个子类的特点

**Arraylist:
    底层数据结构是数组,查询快,增删慢
    线程不安全,效率高
**Vector
    底层数据结构是数组,查询快,增删慢
    线程安全,效率低
**LinkedList
    底层数据结构是链表,查询慢,增删快
    线程不安全,效率高

6.数组和链表的区别

  *数组:查询快,增删慢
 *链表:查询慢,增删快

7.数据结构之栈队列

*数据结构其实就是存储数据的格式
分类:栈,队列,数组,链表,树,哈希表
*栈:先进后出
*队列:先进先出



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值