どうも、ちょげ(@chogetarou)です。
リスト(List)の特定の要素を削除する方法を紹介します。
方法

リスト(List)の特定の要素を削除するには、remove()を使います。
まず、リストからremove()を呼び出します。
そして、remove()の引数に削除する要素を指定します。
//list=対象のリスト, item=削除する要素
list.remove(item);
上記のremove()は、呼び出したリスト(List)から引数の要素を削除します。
使用例
import java.util.ArrayList;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws Exception {
ArrayList<String> nums = new ArrayList<String>(Arrays.asList("one", "two", "three", "four", "five"));
System.out.println("変更前:" + nums);
nums.remove("two");
System.out.println("変更後:" + nums);
}
}
出力:
変更前:[one, two, three, four, five]
変更後:[one, three, four, five]
コメント