企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# Java `ArrayList remove(int index)`方法 > 原文: [https://beginnersbook.com/2013/12/java-arraylist-remove-method-example/](https://beginnersbook.com/2013/12/java-arraylist-remove-method-example/) 方法`remove(int index)`用于从列表中删除指定索引的元素。它删除一个元素并返回相同的元素。如果指定的索引小于零或大于列表的大小(`ArrayList`的索引大小),则抛出`IndexOutOfBoundsException`。 `public Object remove(int index)` ### 例 ```java 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); } } } ``` 输出: ```java ArrayList before remove: AB CD EF GH AB YZ ArrayList After remove: CD EF YZ ```