Java知识点积累——ArrayList

Java知识点积累——ArrayList

一、定义

ArrayList就是传说中的动态数组,用MSDN中的说法,就是Array的复杂版本,它提供了如下一些好处:
(1)动态的增加和减少元素
(2)实现了ICollection和IList接口
(3)灵活的设置数组的大小

二、方法

构造方法:
(1)ArrayList()
构造一个空的数组,默认容量是 10

(2)ArrrayList(int initialCapacity)
构造一个指定容量的空数组,当增加数据导致容量不足时,容量默认增加上次容量大小的一半

(3)ArrayList(Collection<? extends E> c)
这一点不是很好理解,它其实表示生成一个与括号内数组一样的另一个数组,括号内的数组能够继承生成后的数组。

1、ArrayList的创建

ArrayList <变量名> = new ArrayList(); //()中也可传参

2、ArrayList的赋值

变量名.add();

例子:

public class test {
    public static void main(String[] args) {
        ArrayList lis = new ArrayList();//ArrayList的创建

        lis.add("tony");
        lis.add("tom");
        lis.add("jack");
        lis.add("mary");
        lis.add("even");

        for(int i=0;i<lis.size();i++){
            String result = (String)lis.get(i);
            System.out.println(result);
        }

结果:输出结果如下。

tony
tom
jack
mary
even

3、ArrayList中元素的删除

变量名.remove(int Index);
//通过.remove(int Index)方法来进行删除。
//这里直接传入要删除元素的下标即可。
//当然也可以直接传入值进行删除。
//如果你觉得用下边的方式比较麻烦那可以直接输入要删除项的值。

4、ArrayList中元素的修改

变量.set(index, element);

5、ArrayList中元素的查找

.contains("要查找的元素");
//判断查找的元素是否包含在列表中。

例子:

import java.util.ArrayList;

public class tes {
    public static void main(String[] args) {
        ArrayList lis = new ArrayList();
        lis.add("tony");
        lis.add("tom");
        lis.add("jack");
        lis.add("mary");
        lis.add("even");

        if(lis.contains("vivian")){
            System.out.println("包含此元素!");
        }
        else{
            System.out.println("该元素不存在!");
        }
    }

}

总结:ArrayList 中常用的方法

add(E e): 在数组末尾添加元素

size(): 数组中实际元素个数,并不是数组容量

add(int index, E e): 在数组指定位置添加元素

clear(): 将数组中元素清空

contains(E e): 判断数组中是否含有某个元素

get(int index): 返回数组指定位置的元素

indexOf(E e): 返回数组指定元素第一次出现的位置

set(int index, E e): 替换数组指定位置的值

remove(int index): 移除数组指定位置的元素

remove(E e): 移除数组中第一次出现的指定元素

addAll(Collection<? extends E> c): 在数组末尾添加另一个数组

addAll(int index, collection< extends E> c): 在数组指定位置添加另一个数组

removeAll(Collection<?>c): 将数组中属于数组 c 中的元素全部删除

例子:

import java.util.ArrayList;  
import java.util.Arrays;  


public class ArrayListTest {  
    class Human{  
    }  

    class Male extends Human{     
    }  

    public static void main(String[] args) {  
        ArrayList<Integer> list1=new ArrayList<Integer>();  
        list1.add(1); // Appends the specified element to the end of this list  
        list1.add(2);  
        list1.add(3);  
        System.out.println(list1.size());  
        System.out.println(list1);  

        list1.add(2,4); // Inserts the specified element at the specified position in this list  
        System.out.println(list1);  

        list1.clear(); // Removes all of the elements from this list  
        System.out.println(list1);  

        list1.add(5);  
        ArrayList<Integer> list2=new ArrayList<Integer>();  
        list2.add(1);  
        list2.add(2);  
        list2.add(3);  
        list2.addAll(list1); // Appends all of the elements in the specified collection to the end of this list  
        System.out.println(list2);  

        System.out.println(list2.contains(5)); // Judge if this list contains the specified element  
        System.out.println(list2.get(2)); // Returns the element at the specified position in this list  
        System.out.println(list2.indexOf(5)); // Returns the index of the first occurrence of the specified element, or -1 if this list doesn't contain  

        list2.set(2, 10); // Replaces the element at the specified position in this list with the specified element  
        System.out.println(list2);  

        list2.remove(2); // Removes the element at the specified position in this list  
        System.out.println(list2);   
        list2.remove(Integer.valueOf(1)); // Removes the first occurrence of the specified element from this list, if it is present  
        System.out.println(list2);  
        list2.removeAll(Arrays.asList(5)); // Removes from this list all of its elements that are contained in the specified collection  
    }  
}  

输出结果:
3
[1, 2, 3]
[1, 2, 4, 3]
[]
[1, 2, 3, 5]
true
3
3
[1, 2, 10, 5]
[1, 2, 5]
[2, 5]

猜你喜欢

转载自blog.csdn.net/yangxingpa/article/details/80531847