如何找到的元素的在Java中的数组的索引? [英] How to find the index of an element in an array in Java?
本文介绍了如何找到的元素的在Java中的数组的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望找到一个给定的元素的索引,了解其内容,在Java中。
I am looking to find the index of a given element, knowing its contents, in Java.
我试过下面的例子中,它不工作:
I tried the following example, which does not work:
class masi {
public static void main( String[] args ) {
char[] list = {'m', 'e', 'y'};
// should print 1
System.out.println(list[] == "e");
}
}
任何人都可以请解释一下什么是不对的,我需要做什么来解决这个问题呢?
Can anyone please explain what is wrong with this and what I need to do to fix it?
推荐答案
在这种情况下,您可以创建从你的字符数组ë新的字符串,然后做一个indeoxOf(E),该字符串:
In this case, you could create e new String from your array of chars and then do an indeoxOf("e") on that String:
System.out.println(new String(list).indexOf("e"));
但在原始数据类型的其他情况下,你必须遍历它。
But in other cases of primitive data types, you'll have to iterate over it.
这篇关于如何找到的元素的在Java中的数组的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文