List.remove()的两个重载方法

本文介绍Java编程中使用注解处理器移除特定ID的方法。通过示例代码讲解如何遍历类的所有方法并移除带有特定注解的元素。探讨了List对象中两种remove方法的区别及其正确使用方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java编程思想20章的一个例子,20.2编写注解处理器,在list<Interger> useCases中移除注解中出现了的id(int):

    public static void trackUseCases(List<Integer> useCases, Class<?> cl) {
        for(Method method : cl.getDeclaredMethods() ) {
            UseCase uc = method.getAnnotation(UseCase.class);
            if( uc!=null ) {
                System.out.println("Found Use Case: " + uc.id() + " " + uc.description());
//  //              useCases.remove(uc.id());     //IndexOutOfBoundsException
                useCases.remove(new Integer(uc.id()));
//或者           useCases.remove(useCases.indexOf(uc.id()));
            }
        }

        for( int i : useCases ) {
            System.out.println("Warning: Missing use case-" + i);
        }
    }

开始就写了第一种方式,结果就抛异常了,看看源码:

List对象重载了两个remove方法,1、remove(int index),    2、 remove(Object o)。

1.

/**
     * Removes the element at the specified position in this list (optional
     * operation).  Shifts any subsequent elements to the left (subtracts one
     * from their indices).  Returns the element that was removed from the
     * list.
     *
     * @param index the index of the element to be removed
     * @return the element previously at the specified position
     * @throws UnsupportedOperationException if the <tt>remove</tt> operation
     *         is not supported by this list
     * @throws IndexOutOfBoundsException if the index is out of range
     *         (<tt>index < 0 || index >= size()</tt>)
     */
    E remove(int index);
2.
/**
     * Removes the first occurrence of the specified element from this list,
     * if it is present (optional operation).  If this list does not contain
     * the element, it is unchanged.  More formally, removes the element with
     * the lowest index <tt>i</tt> such that
     * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>
     * (if such an element exists).  Returns <tt>true</tt> if this list
     * contained the specified element (or equivalently, if this list changed
     * as a result of the call).
     *
     * @param o element to be removed from this list, if present
     * @return <tt>true</tt> if this list contained the specified element
     * @throws ClassCastException if the type of the specified element
     *         is incompatible with this list
     * (<a href="Collection.html#optional-restrictions">optional</a>)
     * @throws NullPointerException if the specified element is null and this
     *         list does not permit null elements
     * (<a href="Collection.html#optional-restrictions">optional</a>)
     * @throws UnsupportedOperationException if the <tt>remove</tt> operation
     *         is not supported by this list
     */
    boolean remove(Object o);

任务描述 本关任务:完成对集合中数据的增删改查。 相关知识 为了完成本关任务,你需要学习: 1.怎么获取集合中的数据; 2.怎么修改集合中的数据; 3.怎么删除集合中的数据。 获取集合中的数据 使用get(index)即可获取集合中的数据,和数组相同,index为角标,从0开始。 例如: ArrayList list = new ArrayList(); list.add("张无忌"); list.add(18); String name = (String)list.get(0); int age = (int)list.get(1); System.out.println("姓名:" + name + " 年龄:" + age); 输出: 姓名:张无忌 年龄:18 看到这里你可能会有疑问: String name = (String)list.get(0); 为什么使用集合的get()方法获取到数据之后还需要(String)强转数据类型呢? 这是因为我们在定义集合的时候并没有给定一个数据类型,所以集合中的元素,可以是任意类型的对象(Object),如果把某个对象放入集合,则会忽略它的类型,而把它当做Object类处理。 这样我们在从集合中获取数据的时候,默认也会将它看做是Object类型,所以我们在使用的时候需要强转成对应类型。 修改集合中的数据 使用set(index, element)方法即可修改集合中的数据,第一个参数代表索引,即修改集合第几个位置的元素,index从0开始,第二个参数element是要修改的数据。 例如: ArrayList list = new ArrayList(); list.add("张无忌"); list.add(18); String name = (String)list.get(0); int age = (int)list.get(1); System.out.println("姓名:" + name + " 年龄:" + age); list.set(0,"张三丰"); //修改集合的第一个元素 name = (String)list.get(0); System.out.println("姓名:" + name + " 年龄:" + age); 输出: 姓名:张无忌 年龄:18 姓名:张三丰 年龄:18 删除集合中的数据 使用remove(index)或remove(element)方法即可删除元素,remove方法是一个重载方法,第一种可以根据索引位置删除元素,第二种可以根据元素的引用来删除集合中对应的元素。 如下: ArrayList list = new ArrayList(); list.add("张无忌"); list.add(18); //List集合的toString可以输出数组中所有的数据 System.out.println(list.toString()); Object obj1 = list.get(0); list.remove(obj1); //根据元素的引用删除元素 System.out.println(list.toString()); list.remove(0); //根据索引删除元素 System.out.println(list.toString()); 输出: [张无忌, 18] [18] [] 请你思考一个问题: ArrayList list = new ArrayList(); list.add("张无忌"); list.add(18); 对于上述代码,要删除所有元素,应该使用remove(0),remove(0)还是remove(0),remove(1)? 编程要求 请仔细阅读右侧代码,根据方法内的提示,在Begin - End区域内进行代码补充,具体任务如下: 删除集合的第一个和最后一个元素; 添加两个字符串:hello,educoder至集合中; 修改集合的第三个元素,改为:list; 输出集合中所有的元素。 测试说明 补充完代码后,点击测评,平台会对你编写的代码进行测试,当你的结果与预期输出一致时,即为通过。 测试输入: 4 张三 李四 王五 赵六 预期输出: 李四 王五 list educoder 提示,获取数组的长度: ArrayList list = new ArrayList(); list.add(1); list.add(3); System.out.println(list.size()); 输出:2package step2; import java.util.ArrayList; import java.util.Scanner; public class HelloWorld { @SuppressWarnings("unchecked") public static void main(String[] args) { //获取输入的数据并添加至集合 Scanner sc = new Scanner(System.in); ArrayList list = new ArrayList<>(); int length = sc.nextInt(); for(int i =0 ; i< length; i++){ list.add(sc.next()); } /********** Begin *********/ /********** End **********/ } }
03-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值