Java学习笔记之ArrayList集合

关于ArrayList

ArrayList常用方法:

    public boolean remove(Object o):删除指定元素,返回删除是否成功
    public E remove(int index):删除指定索引处的元素,返回被删除的元素
    public E set(int index,E element):修改指定索引处的元素,返回被修改的元素
    public E get(int index):返回指定索引处的元素
    public int size():返回集合中元素的个数

ArrayList构造方法:

    public ArrayList():创建一个空的集合对象

ArrayList添加方法:

    public boolean add(E e):将指定的元素追加到此集合的末尾
    public void add(int index,E element):在此集合中的指定位置插入指定的元素
package com.itheima_01;


/*
    ArrayList构造方法:
        public ArrayList():创建一个空的集合对象
    ArrayList添加方法:
        public boolean add(E e):将指定的元素追加到此集合的末尾
        public void add(int index,E element):在此集合中的指定位置插入指定的元素
 */

import java.util.ArrayList;

public class ArrayListDemo01 {
    public static void main(String[] args) {

        //public ArrayList():创建一个空的集合对象
        ArrayList<String> array = new ArrayList<>();

        //public boolean add(E e):将指定的元素追加到此集合的末尾
        array.add("hello");
        array.add("world");
        array.add("Java");

        //public void add(int index,E element):在此集合中的指定位置插入指定的元素
        array.add(1,"JavaSE");

        //索引越界异常:IndexOutOfBoundsException
        array.add(5,"JavaEE");

        System.out.println("array:" + array);

    }
}



版权声明:本文为qq_45454190原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。