解读ArrayList中的indexOf(Object o)方法 2022-03-28 23:12 120阅读 0赞 源码如下: /** * Returns the index of the first occurrence of the specified element * in this list, or -1 if this list does not contain the element. * More formally, returns the lowest index <tt>i</tt> such that * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, * or -1 if there is no such index. */ public int indexOf(Object o) { if (o == null) { for (int i = 0; i < size; i++) if (elementData[i]==null) return i; } else { for (int i = 0; i < size; i++) if (o.equals(elementData[i])) return i; } return -1; } 可以看出,方法中首先判断了传入的对象是否为空,如果为空的话,会去遍历寻找数组中为空的对象,使用==的方式。 因为空对象不能使用equals方法,而且判断对象相等又不能使用==方法,因此找空对象时需要另外使用==的方式遍历寻找。 结果就是分别判断分别查找。 找到对应的对象以后,返回**对象当前的下标**。 找不到对象的情况,返回**-1**. 文章版权声明:注明蒲公英云原创文章,转载或复制请以超链接形式并注明出处。
相关 谨用ArrayList中的subList方法 0、subList方法api: Parameters: fromIndex low endpoint (inclusive) of the subList 柔情只为你懂/ 2021年05月12日 19:49/ 0 赞/ 301 阅读
相关 Java中ArrayList的常用方法 java中ArrayList是Java集合中非常重要的 下面列举出ArrayList的常用方法 import java.util.; pu 本是古典 何须时尚/ 2021年07月25日 10:05/ 0 赞/ 233 阅读
相关 ArrayList的使用方法 数组列表是一个存储同一类对象或具有共同超类的对象的数据结构,在程序运行时,列表可以根据需要调整大小,创建数组列表最简单的方法是调用其不带参数的构造函数 Arr 左手的ㄟ右手/ 2022年01月08日 10:25/ 0 赞/ 95 阅读
相关 java——ArrayList中contains()方法中的疑问 问题引子: ist students=newArrayList(); students.add(newStudent("20160800612")); System.ou 浅浅的花香味﹌/ 2022年01月20日 15:25/ 0 赞/ 97 阅读
相关 解读ArrayList中的grow(int minCapacity)方法 查看源码: / Increases the capacity to ensure that it can hold at least the 谁借莪1个温暖的怀抱¢/ 2022年03月28日 22:12/ 0 赞/ 171 阅读
相关 解读ArrayList中的indexOf(Object o)方法 源码如下: / Returns the index of the first occurrence of the specified elemen 小鱼儿/ 2022年03月28日 23:12/ 0 赞/ 121 阅读
相关 ArrayList中remove方法的注意事项 少罗嗦,先看代码 public class ListRemove { public static void main(String[] args) 分手后的思念是犯贱/ 2022年05月22日 06:10/ 0 赞/ 65 阅读
相关 ArrayList的contains方法 今天在用ArrayList类的caontains方法是遇到了问题,我写了一个存放User类的ArrayList 但在调用list.contains(user)时总是返回fal 迷南。/ 2022年08月01日 08:27/ 0 赞/ 34 阅读
相关 ArrayList中toArray()方法的使用注意 import java.util.; public class ArrayListToArray { public static void m 你的名字/ 2022年08月25日 18:55/ 0 赞/ 44 阅读
相关 解读I/O多路复用技术 前言 当我们要编写一个echo服务器程序的时候,需要对用户从标准输入键入的交互命令做出响应。在这种情况下,服务器必须响应两个相互独立的I/O事件:1)网络客户端发起网络连 怼烎@/ 2023年01月02日 13:25/ 0 赞/ 23 阅读
还没有评论,来说两句吧...