ArrayList常用方法
阿新 • • 發佈:2021-07-09
package com.cheng.arraylist;
import java.util.ArrayList;
/*
ArrayList常用方法:
public boolean remove(Object o);刪除指定元素,返回刪除是否成功
public E remove(int index);刪除指定索引處的元素,返回被刪除的元素
public E remove(int index,E element);修改指定索引處的元素,返回被修改的元素
public E remove(int index);返回指定索引處的元素
public int size();返回集合中元素的個數
*/
public class Demo02 {
public static void main(String[] args) {
//建立集合
ArrayList array = new ArrayList();
//新增元素
array.add("hello");
array.add("world");
array.add("java");
//public boolean remove(Object o);刪除指定元素,返回刪除是否成功
// System.out.println(array.remove("world"));//true
// System.out.println(array.remove("niu"));//false 沒有這個元素刪除失敗
//public E remove(int index);刪除指定索引處的元素,返回被刪除的元素
//System.out.println(array.remove(1));//刪除world 返回world
//若是索引在集合沒有,則報錯索引越界
//public E remove(int index,E element);修改指定索引處的元素,返回被修改的元素
//System.out.println(array.set(2,"aaaaaa"));//輸出java,將2索引處的java替換為aaaaaa
// public E remove(int index);返回指定索引處的元素
System.out.println(array.get(1));//1索引處的元素world
//public int size();返回集合中元素的個數
System.out.println(array.size());//3
//輸出
System.out.println("array:"+array);
}
}