Basics 015-ArrayList, commonly used method

After defining the length of the array can no longer be changed, and the length may vary ArrayList.

For a right ArrayList <E> represents a lack of type (Type lack of means among a collection of elements are unified type, and lack only type is a reference type, not a basic type)

Precautions:

For ArrayList collection type, if direct printing, the result is not the address of the print, but the content, if the content is empty, the print is []

import java.util.ArrayList;

public class Demo01ArrayList {
    public static void main(String[] args) {
        ArrayList<String> ay = new ArrayList<>();
        //内容为空时打印
        System.out.println(ay);

        //添加内容后再打印
        ay.add("张三");
        ay.add("李四");
        ay.add("王五");
        System.out.println(ay);
    }
}

Output:

[]
[张三, 李四, 王五]

Common methods:

public boolean add (E e): added element, and the type of the parameter to the lack of consistency among a set type. The return value represents the success.

Note: For the ArrayList collection is, add add the action must succeed, so the return value can not be used, but for other collections it, add add actions are not necessarily successful.

public E get (int index): Get binding elements from among the reference parameter is an index (starting from 0), the return value is the element corresponding to the position.

public E remove (int index): binding to remove elements from among the parameter is an index label, the return value is removed element.

public int size (): Get the length dimension of the binding, the binding element contains the number of the return value.

import java.util.ArrayList;

public class Demo02ArrayList {
    public static void main(String[] args) {
        ArrayList<String> arry = new ArrayList<>();
        System.out.println("1===========================");
        System.out.println(arry);
        arry.add("如来");
        arry.add("观音");
        arry.add("唐三藏");
        arry.add("孙悟空");
        arry.add("猪八戒");
        arry.add("沙悟净");

        System.out.println("2===========================");
        System.out.println(arry);
        int len = arry.size();
        System.out.println("集合长度为:"+len);



        //获取某个指定元素
        String temp = arry.get(2);
        System.out.println("第二个元素为:"+temp);
        //删除某个指定元素---通过标号
        String remove = arry.remove(2);
        System.out.println("被删除元素为" + remove);
        temp = arry.get(2);
        System.out.println("第二个元素为:"+temp);

        System.out.println("3===========================");
        System.out.println(arry);
        len = arry.size();
        System.out.println("集合长度为:"+len);

        //删除某个元素通过集合内容
        System.out.println("4===========================");
        boolean result = arry.remove("沙悟净");
        System.out.println("删除 沙悟净 成功?"+result);
        System.out.println(arry);
        len = arry.size();
        System.out.println("集合长度为:"+len);

        //删除某个元素通过集合内容
        System.out.println("5===========================");
         result = arry.remove("斗战胜佛");
        System.out.println("删除 斗战胜佛 成功?"+result);
    }
}

Output:

1===========================
[]
2===========================
[如来, 观音, 唐三藏, 孙悟空, 猪八戒, 沙悟净]
集合长度为:6
第二个元素为:唐三藏
被删除元素为唐三藏
第二个元素为:孙悟空
3===========================
[如来, 观音, 孙悟空, 猪八戒, 沙悟净]
集合长度为:5
4===========================
删除 沙悟净 成功?true
[如来, 观音, 孙悟空, 猪八戒]
集合长度为:4
5===========================
删除 斗战胜佛 成功?false

 

Published 69 original articles · won praise 4 · Views 3907

Guess you like

Origin blog.csdn.net/l0510402015/article/details/104089135