day09 【ArrayList类】

64 阅读3分钟
遍历ArrayList集合兑现list的快捷键 集合对象.fori 
ArrayList<E>集合类是一个引用类型
类名 对象名 = new 类名(实际参数...);
ArrayList<String> list = new ArrayList<String>();
System.out.println(list.toString())
list.add("AAA");//把字符串AA添加到list集合对象内部的数组中

    
    System.out.println(list);//[AAA, BBB, CCC
ArrayList集合常用方法: 增(C)删(D)改(U)查(R)的方法 CRUD的操作
public boolean add(E e) :将指定的元素添加到此集合的尾部。
     list.add("AAA");//把字符串AA添加到list集合对象内部的数组中
public void add(int index,E e) :在此集合中的指定位置index处插入指定的元素e。
      list.add(1, "BBB");索引加
public boolean remove(Object o) :删除指定的元素,返回删除是否成功   注意: 只删除符合条件的第一个元素
public E remove(int index) :移除此集合中指定位置上的元素。返回被删除的元素。
public E set(int index,E element) :修改指定索引处的元素,返回被修改的元素。
     String whoUpdate = list.set(0, "A");
public E get(int index) :返回此集合中指定位置上的元素。返回获取的元素。
       String s = list.get(0);索引获取
public int size() :返回此集合中的元素数。遍历集合时,可以控制索引范围,防止越界。
     //int size() :返回此集合中的元素数。遍历集合时,可以控制索引范围,防止越界。
    int size = list.size();
    s = list.get(list.size() - 1);//CCC
public boolean isEmpty(): 判断集合是否为空
       //通过索引删除CCC
    String whoRemoved = list.remove(1);
    list.isEmpty()
public void clear(): 清空集合元素
         list.clear();
         

ArrayList集合存储字符串并遍历

 //以上for循环中数字4写死了,可以使用集合长度代替
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
}
 ArrayList集合容器中只能存储引用类型数据,不能存储基本类型数据
    如果非得向ArrayList集合中存储基本类型数据的话,
    在创建ArrayList集合对象时<>需要写基本类型对应的引用类型
    简单来讲: 每个基本类型都对应了一个类(包装类)
    
        基本类型                包装类
        byte                    Byte
        short                   Short
        int                     Integer
        long                    Long
        float                   Float
        double                  Double
        char                    Character
        boolean                 Boolean
 public class Demo04ArrayListBase {
    public static void main(String[] args) { 
        //错误的:<>中不能写基本类型int 
        ArrayList<Integer> list = new ArrayList<>();

        //add方法添加数据
        list.add(new Integer(100));
        list.add(200);
        list.add(300);

        //遍历
        for (int i = 0; i < list.size(); i++) {
            int num = list.get(i);
            System.out.println(num);
        }
    }
}
if ("test".equals(str)) {
                /*
                    注意:
                        因为执行完毕remove方法,立刻执行i++,
                        remove方法删除当前元素后,后面的元素会前移,
                        如果有多个连续的元素需要被删除的话,可能会删不完整(有遗漏)
                    解决方案:
                        在执行i++之前,先执行i--
                 */
                list.remove(i--);
            }
        }
    //定义方法,筛选ArrayList集合对象中的所有年龄小于18的Student对象
    public static ArrayList<Student> filterArrayList(ArrayList<Student> list) {
        //1.创建新的ArrayList集合对象newList,存储数据类型Student
        ArrayList<Student> newList = new ArrayList<>();
        //2.使用for循环遍历方法参数集合对象list
        for (int i = 0; i < list.size(); i++) {
            //2.1 获取当前Student对象stu
            Student stu = list.get(i);
            //2.2 判断 如果 当前Student对象stu的年龄<18 说明是我们想要的
            if (stu.getAge() < 18) {
                //2.3 把当前Student对象stu添加到新的集合对象newList中
                newList.add(stu);
            }
        }
        //3. for循环结束后,返回新的集合对象newList
        return newList;
    }
}