Java 集合 ArrayList remove(int index)方法

方法remove(int index)用于从列表中删除指定索引的元素。它删除一个元素并返回相同的元素。如果指定的索引小于零或大于列表的大小(ArrayList的索引大小),则抛出IndexOutOfBoundsException

public Object remove(int index)

package beginnersbook.com;
import java.util.ArrayList;
public class RemoveExample {
   public static void main(String args[]) {
       //String ArrayList
       ArrayList<String> al = new ArrayList<String>();
       al.add("AB");
       al.add("CD");
       al.add("EF");
       al.add("GH");
       al.add("AB");
       al.add("YZ");
       System.out.println("ArrayList before remove:");
       for(String var: al){
            System.out.println(var);
       }
       //Removing 1st element
       al.remove(0);
       //Removing 3rd element from the remaining list
       al.remove(2);
       //Removing 4th element from the remaining list
       al.remove(2);
       System.out.println("ArrayList After remove:");
       for(String var2: al){
             System.out.println(var2);
       }
    }
}

输出:

ArrayList before remove:
AB
CD
EF
GH
AB
YZ
ArrayList After remove:
CD
EF
YZ

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程