Java 获取数组列表中项目的索引;
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16393709/
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
Getting Index of an item in an arraylist;
提问by user2351151
I have a Class called AuctionItem
. The AuctionItem
Class has a method called getName()
that returns a String
. If I have an ArrayList
of type AuctionItem
, what is the best way to return the index of an item in the ArrayList
that has a specific name?
我有一个名为AuctionItem
. 该AuctionItem
班有一个名为方法getName()
返回一个String
。如果我有一个ArrayList
of type AuctionItem
,返回ArrayList
具有特定名称的项目的索引的最佳方法是什么?
I know that there is an .indexOf()
function. The parameter for this function is an object. To find the item that has a name, should I just use a for loop, and when the item is found, return the element position in the ArrayList
?
我知道有一个.indexOf()
功能。这个函数的参数是一个对象。要查找具有名称的项目,我是否应该只使用 for 循环,当找到该项目时,返回ArrayList
? 中的元素位置?
Is there a better way?
有没有更好的办法?
回答by T.J. Crowder
To find the item that has a name, should I just use a for loop, and when the item is found, return the element position in the ArrayList?
要查找具有名称的项目,我是否应该只使用 for 循环,当找到该项目时,返回 ArrayList 中的元素位置?
Yes to the loop (either using indexes or an Iterator
). On the return value, either return its index, or the item iteself, depending on your needs. ArrayList
doesn't have an indexOf
(Object target, Comparatorcompare)` or similar. Now that Java is getting lambda expressions (in Java 8, ~March 2014), I expect we'll see APIs get methods that accept lambdas for things like this.
是循环(使用索引或Iterator
)。在返回值上,根据您的需要返回其索引或项目本身。ArrayList
没有indexOf
(Object target, Comparatorcompare)` 或类似的。现在 Java 正在获取 lambda 表达式(在 Java 8 中,~2014 年 3 月),我希望我们会看到 API 获取接受 lambda 的方法,用于此类事情。
回答by Suresh Atta
Yes.you have to loop it
是的,你必须循环它
public int getIndex(String itemName)
{
for (int i = 0; i < arraylist.size(); i++)
{
AuctionItem auction = arraylist.get(i);
if (itemName.equals(auction.getname()))
{
return i;
}
}
return -1;
}
回答by harsh
Basically you need to look up ArrayList
element based on name getName
. Two approaches to this problem:
基本上你需要ArrayList
根据 name查找元素getName
。解决这个问题的两种方法:
1- Don't use ArrayList
, Use HashMap<String,AutionItem>
where String
would be name
1,不要使用ArrayList
,使用HashMap<String,AutionItem>
其中String
将名字
2- Use getName
to generate index and use index based addition into array list list.add(int index, E element)
. One way to generate index from name would be to use its hashCode and modulo by ArrayList
current size (something similar what is used inside HashMap
)
2-getName
用于生成索引并使用基于索引的添加到数组列表中list.add(int index, E element)
。从名称生成索引的一种方法是使用其 hashCode 和ArrayList
当前大小的模数(类似于内部使用的内容HashMap
)
回答by Deepak
for (int i = 0; i < list.length; i++) {
if (list.get(i) .getName().equalsIgnoreCase("myName")) {
System.out.println(i);
break;
}
}
回答by Kooki
I think a for-loop should be a valid solution :
我认为 for 循环应该是一个有效的解决方案:
public int getIndexByname(String pName)
{
for(AuctionItem _item : *yourArray*)
{
if(_item.getName().equals(pName))
return *yourarray*.indexOf(_item)
}
return -1;
}
回答by Kai
You could implement hashCode
/equals
of your AuctionItem
so that two of them are equal if they have the same name. When you do this you can use the methods indexOf
and contains
of the ArrayList
like this: arrayList.indexOf(new AuctionItem("The name"))
. Or when you assume in the equals method that a String is passed: arrayList.indexOf("The name")
. But that's not the best design.
您可以实现hashCode
/ equals
of yourAuctionItem
以便它们中的两个具有相同的名称。当你这样做,你可以使用的方法indexOf
和contains
的ArrayList
是这样的:arrayList.indexOf(new AuctionItem("The name"))
。或者当你在equals方法假定字符串传递:arrayList.indexOf("The name")
。但这不是最好的设计。
But I would also prefer using a HashMap
to map the name to the item.
但我也更喜欢使用 aHashMap
将名称映射到项目。
回答by Moosee
Rather than a brute force loop through the list (eg 1 to 10000), rather use an iterative search approach : The List needs to be sorted by the element to be tested.
而不是通过列表的蛮力循环(例如 1 到 10000),而是使用迭代搜索方法:列表需要按要测试的元素进行排序。
Start search at the middle element size()/2 eg 5000 if search item greater than element at 5000, then test the element at the midpoint between the upper(10000) and midpoint(5000) - 7500
在中间元素 size()/2 处开始搜索,例如 5000 如果搜索项大于 5000 处的元素,然后在 upper(10000) 和 midpoint(5000) 之间的中点测试元素 - 7500
keep doing this until you reach the match (or use a brute force loop through once you get down to a smaller range (eg 20 items)
继续这样做直到你到达比赛(或者一旦你进入一个较小的范围(例如20个项目),就使用蛮力循环)
You can search a list of 10000 in around 13 to 14 tests, rather than potentially 9999 tests.
您可以在大约 13 到 14 个测试中搜索 10000 个列表,而不是潜在的 9999 个测试。
回答by Ritwik Gupta
.indexOf() works well. If you want an example here is one:
.indexOf() 效果很好。如果你想要一个例子,这里是一个:
ArrayList<String> example = new ArrayList<String>();
example.add("AB");
example.add("CD");
example.add("EF");
example.add("GH");
example.add("IJ");
example.add("KL");
example.add("MN");
System.out.println("Index of 'AB': "+example.indexOf("AB"));
System.out.println("Index of 'KL': "+example.indexOf("KL"));
System.out.println("Index of 'AA': "+example.indexOf("AA"));
System.out.println("Index of 'EF': "+example.indexOf("EF"));
will give you an output of
会给你一个输出
Index of 'AB': 0
Index of 'KL': 5
Index of 'AA': -1
Index of 'EF': 2
Note: This method returns -1 if the specified element is not present in the list.
注意:如果指定的元素不在列表中,则此方法返回 -1。