问题
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?
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
?
Is there a better way?
回答1:
I think a for-loop should be a valid solution :
public int getIndexByname(String pName)
{
for(AuctionItem _item : *yourArray*)
{
if(_item.getName().equals(pName))
return *yourarray*.indexOf(_item)
}
return -1;
}
回答2:
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;
}
回答3:
Basically you need to look up ArrayList
element based on name getName
. Two approaches to this problem:
1- Don't use ArrayList
, Use HashMap<String,AutionItem>
where String
would be name
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
)
回答4:
for (int i = 0; i < list.length; i++) {
if (list.get(i) .getName().equalsIgnoreCase("myName")) {
System.out.println(i);
break;
}
}
回答5:
.indexOf() works well. If you want an example here is one:
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.
回答6:
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?
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, Comparator compare)` 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.
回答7:
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.
But I would also prefer using a HashMap
to map the name to the item.
回答8:
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.
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
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)
You can search a list of 10000 in around 13 to 14 tests, rather than potentially 9999 tests.
来源:https://stackoverflow.com/questions/16393709/getting-index-of-an-item-in-an-arraylist