Java array list删除元素
时间: 2023-10-08 16:13:44 浏览: 82
Java ArrayList删除特定元素的方法
你可以使用 ArrayList 的 remove() 方法来删除指定索引位置上的元素,或者使用 remove(Object obj) 方法来删除指定元素。下面是示例代码:
```
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<String>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
System.out.println("Fruits before removing: " + fruits);
// removing element at index 1
fruits.remove(1);
System.out.println("Fruits after removing element at index 1: " + fruits);
// removing element "Orange"
fruits.remove("Orange");
System.out.println("Fruits after removing element \"Orange\": " + fruits);
}
}
```
输出结果为:
```
Fruits before removing: [Apple, Banana, Orange]
Fruits after removing element at index 1: [Apple, Orange]
Fruits after removing element "Orange": [Apple]
```
阅读全文