java - 获取数组中元素的索引[错误]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4287417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java - get index of element in array [error]
提问by Jake
When i am trying to receive the index of the element i get an error when compiling (cannot find symbol on the code that i have marked with comment). I have the following code:
当我尝试接收元素的索引时,编译时出现错误(在我用注释标记的代码上找不到符号)。我有以下代码:
for (int i = 0; i < turtles.size(); i++) {
if (turtles.get(i).getX() == 450) {
results.add(turtles.get(i).indexOf(i)); //this line error
}
}
The thing is that i want to add the index of the retrieved turtle in the loop into a new array called results. How do i do that?
问题是我想将循环中检索到的海龟的索引添加到一个名为结果的新数组中。我怎么做?
The error is saying:
错误是说:
cannot find symbol
找不到标志
symbol: method indexOf(int)
符号:方法 indexOf(int)
location: class se.lth.cs.pt.turtle.visible.Turtle
位置:类 se.lth.cs.pt.turtle.visible.Turtle
results.add(turtles.get(i).indexOf(i));
>
采纳答案by Jake
I solved the problem by defining the arraylist to type of integer like this:
我通过将 arraylist 定义为整数类型来解决这个问题,如下所示:
ArrayList<Integer> results = new ArrayList<Integer>();
回答by Joel
Without the full error & all the code I'm not entirely sure what you're trying to do, but don't you just want to do this?
如果没有完整的错误和所有代码,我不完全确定您要做什么,但您不想这样做吗?
results.add(i);
or maybe this:
或者这个:
results.add(turtles.get(i));
(...the index of the retrieved turtle isi)
(...检索到的海龟的索引是i)
depending on whether you expect results to contain the index of the turtle, or the turtle itself.
取决于您希望结果包含海龟的索引还是海龟本身。
回答by Manmohan Soni
Hi I am providing a method u can use it
嗨,我提供了一种您可以使用它的方法
/**
* Method to get the index of the given item from the list
* @param stringArray
* @param name
* @return index of the item if item exists else return -1
*/
public static int getIndexOfItemInArray(String[] stringArray, String name) {
if (stringArray != null && stringArray.length > 0) {
ArrayList<String> list = new ArrayList<String>(Arrays.asList(stringArray));
int index = list.indexOf(name);
list.clear();
return index;
}
return -1;
}
回答by darioo
You must initialize that array, if you haven't done so, before your loop. So, your code becomes:
您必须在循环之前初始化该数组,如果您还没有这样做的话。所以,你的代码变成:
List results = new ArrayList();
for (int i = 0; i < turtles.size(); i++) {
if (turtles.get(i).getX() == 450) {
results.add(turtles.get(i).indexOf(i));
}
}
By the way, I hope you know what you're doing when you use turtles.get(i).indexOf(i)
, because it isn't very obvious from your code.
顺便说一句,我希望您在使用 时知道自己在做什么turtles.get(i).indexOf(i)
,因为从您的代码中这不是很明显。
Edit: after seeing your edited question, I can only assume that something isn't right with your se.lth.cs.pt.turtle.visible.Turtle
class. Like, it doesn't have a method named indexOf
that takes an int
as its parameter.
编辑:在看到您编辑的问题后,我只能假设您的se.lth.cs.pt.turtle.visible.Turtle
班级有问题。就像,它没有一个indexOf
以 anint
作为参数的命名方法。
回答by B3tturTh3nU
I believe your problem is that indexOf takes an object as a parameter, and returns the index. If you want to find the object at index i, you would use the 'Vector.elementAt(int index)' method instead.
我相信你的问题是 indexOf 将一个对象作为参数,并返回索引。如果您想在索引i处找到对象,您可以改用 'Vector.elementAt(int index)' 方法。
回答by Satwik
You ought to reconsider Joel's answer above (I hope it is still above!) once more. You say "I am getting a certain turtle...and want to put the index of the turle in a new array...", and it seems that the index is actually in the variable "i" already. There is no extra work needed to "fetch" it.
您应该再次重新考虑乔尔在上面的回答(我希望它仍然在上面!)。你说“我得到了一只乌龟……并且想把乌龟的索引放在一个新的数组中……”,看起来索引实际上已经在变量“i”中了。“获取”它不需要额外的工作。
"results.add(i)" seems to be what you want to do.
“results.add(i)”似乎是你想要做的。
回答by Nayan Wadekar
indexOf(Object) returns the index of the first occurrence of that object.
indexOf(Object) 返回该对象第一次出现的索引。
for(Turtle _turtle : turtles){
//-- Getting index of the turtle & adding it to results
results.add(turtles.indexOf(_turtle));
}
Retrieving objects based on index (i.e using i etc) may sometimes result in index out of bound exceptions & also difficult to manage during manipulations.
基于索引检索对象(即使用 i 等)有时可能会导致索引越界异常,并且在操作期间也难以管理。