标签: indexof

在对象数组中查找对象的索引

我有这个

arr = [{name: 'pippo',lastname: 'p'},{name: 'mickey',lastname: 'mouse'}]
x ={name: 'mickey', lastname: 'mouse'}
arr.indexOf(x)
Run Code Online (Sandbox Code Playgroud)

为什么 arr.indexOf(x) 返回 -1?是否有另一种解决方案可以在对象数组中找到对象的索引?

javascript arrays object indexof reactjs

0
推荐指数
1
解决办法
99
查看次数

Java indexOf 不调用等于

我想了解为什么 indexOf 函数不调用覆盖方法 equals 。我有这门课:

class Test {
    public Integer _test;

    public Test(Integer test) {
        _test= test;
    }

    @Override
    public int hashCode() {
        return Objects.hash(_test);
    }

    @Override
    public boolean equals(Object obj) {
        System.out.println("equals function called");
        if (!(obj instanceof Integer)) return super.equals(obj);
        Integer test = (Integer) obj;
        return _test == test;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的主要代码:


List<Test> listTest = new Arraylist<Test>();

listTest.add(new Test(1));

System.out.println(listTest.indexOf(new Integer(1)));

Run Code Online (Sandbox Code Playgroud)

输出:-1


请注意,如果我这样做,则调用 equals 函数:

List<Test> listTest = new Arraylist<Test>();

listTest.add(new Test(1));

System.out.println(listTest.indexOf(new Test(1)));
Run Code Online (Sandbox Code Playgroud)

输出:-1 等于调用的函数

java overriding equals indexof

0
推荐指数
1
解决办法
62
查看次数

为什么 indexOf() 和 contains() 不能像我在 Java 中预期的那样工作?

// Objects for the bookstore I created

BookStore bookStore = new BookStore("Subu's Book Store","Perambur");
bookStore.addItem(new Item("Animal Farm",20));
bookStore.addItem(new Item("Animal Farm",20));
Run Code Online (Sandbox Code Playgroud)

如果我使用原始工作方法,则输出:

Animal Farm has been bought
Animal Farm is already bought
Run Code Online (Sandbox Code Playgroud)

使用 contains() 不起作用的方法的输出

Animal Farm has been bought
Animal Farm has been bought
Run Code Online (Sandbox Code Playgroud)

使用 indexof() 不起作用的方法的输出

-1
Animal Farm has been bought
-1
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 1
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:372)
at java.base/java.util.ArrayList.get(ArrayList.java:459)
at com.company.subusproject.BookStore.searchItem(BookStore.java:41)
at …
Run Code Online (Sandbox Code Playgroud)

java arrays contains object indexof

0
推荐指数
1
解决办法
143
查看次数

如何获得不是第一个的indexOf元素

假定

ArrayList<Integer> list =  Arrays.asList(new Integer[] {1,2,3,4,5,6,1,8,9});
Run Code Online (Sandbox Code Playgroud)

查找第二次出现

我想获取(多个)包含元素“ 1” 的第二次发现的索引,但将始终返回(因为这是第一次发现)。list.indexOf(1)0

性能

我想做到这一点而不使用像for或那样的循环while。由于我在游戏中需要它,因此使用循环根本无效。

编辑:有没有办法获得“ indexOf”一些没有迭代器的元素?

java arraylist indexof

-1
推荐指数
1
解决办法
74
查看次数

String.IndexOf()返回字符串的意外索引

String.IndexOf()方法不符合我的预期。

我希望它不会找到匹配项,因为所输入的确切单词不在str

string str = "I am your Friend";
int index = str.IndexOf("you",0,StringComparison.OrdinalIgnoreCase);
Console.WriteLine(index);
Run Code Online (Sandbox Code Playgroud)

输出5

我的预期结果是-1,因为字符串不包含

.net c# indexof

-1
推荐指数
1
解决办法
110
查看次数

IndexOf(char) 与 Contains(string) 用于检查字符串中字符是否存在的性能

我想知道是否

string.IndexOf(char) 
Run Code Online (Sandbox Code Playgroud)

更快的

string.Contains(string) 
Run Code Online (Sandbox Code Playgroud)

目的是检查单个字符是否存在于string. 我知道根据要求我应该使用,string.Contains(string)但这不是这个问题的重点。我确实尝试反汇编 mscorlib.dll以比较它们的实现,但我找不到

 string.IndexOf(char)
Run Code Online (Sandbox Code Playgroud)

因为它是在CLR 本身内实现的。实施

 string.Contains(string)
Run Code Online (Sandbox Code Playgroud)

不过看起来很重

c# string performance contains indexof

-2
推荐指数
1
解决办法
3466
查看次数

对于每个string.IndexOf(char)错误?

var str = "GB29NWBK60161331926819"

foreach (var item in str.ToCharArray())
{
    Debug.WriteLine(str.IndexOf(item));
}
Run Code Online (Sandbox Code Playgroud)

给出输出

0 1 2 3 4 5 1 7 8 9 10 8 10 13 13 10 3 2 8 19 10 3

我在期待

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

是这个还是一个bug还是我的假设不正确?

.net c# string indexof .net-4.5

-3
推荐指数
1
解决办法
97
查看次数