简体   繁体   English

在对象数组上使用Arrays.binarySearch并在对象内查找字符串

[英]Using Arrays.binarySearch on an array of Objects and looking for a String within the object

OK so I have the following Object class: OK,所以我有以下Object类:

public class IntoleranceFood implements Comparable<IntoleranceFood>{

private int scoreInt;
public String foodName;

public IntoleranceFood(String food, int score) {
    super();
    this.foodName = food;
    this.scoreInt = score;

}

//getters and setters
public String getFood() {
        return foodName;
}
public void setFood(String food) {
        this.foodName = food;
}
public int getScore() {
    return scoreInt;
}
public void setScore(int score) {
        this.scoreInt = score;
}


@Override
public String toString() {
    return "Intolerance Food [Name=" + foodName + ", Score=" + scoreInt + "]";
}

@Override
public int compareTo(IntoleranceFood arg0) {

    return toString().compareTo(arg0.toString());
}

}

And then in my Activity I have created an array for these objects to go into, and filled up the array with "IntoleranceFood" Objects: 然后在我的活动中,为这些对象创建了一个数组,并用“ IntoleranceFood”对象填充了该数组:

int numFoodItemTypes = db.intoleranceFoodItemTypesTotal();
    IntoleranceFood[] foodArray = new IntoleranceFood[numFoodItemTypes];

    Cursor cAllFoodTypes = db.intoleranceFoodTypesList();
    int foodItem = 0;
    do{
        foodArray[foodItem] = new IntoleranceFood(cAllFoodTypes.getString(0), 0);
        foodItem++;
    }while(cAllFoodTypes.moveToNext());

I managed to sort the array by implementing Comparable and the compareTo method in my Object class: 我通过在Object类中实现Comparable和compareTo方法来对数组进行排序:

Arrays.sort(foodArray);

But I want to then search the array using binary search, and look for the position in the array where a certain Object with a specific food name (String) resides. 但是我想使用二进制搜索来搜索数组,并在数组中查找具有特定食物名称(字符串)的特定对象所在的位置。 But I dont know how to get the following code working, and specifically in terms of: 但是我不知道如何使以下代码正常工作,特别是在以下方面:
-binarySearch(Object[] array, Object value) -binarySearch(Object []数组,对象值)
I don't know what to put in "Object value" so this: 我不知道要在“对象值”中添加什么,因此:

Arrays.binarySearch(foodArray, "Cereal");

Is clearly wrong! 显然是错误的! But I'm not sure how to search the Object array for an Object containing the String food name "Cereal". 但是我不确定如何在对象数组中搜索包含字符串食物名称“谷物”的对象。

Thanks. 谢谢。


Yes so after the very useful reply below, I realsied what I need to be doing is: 是的,因此在以下非常有用的回复之后,我意识到我需要做的是:

IntoleranceFood searchOb = new IntoleranceFood("Cereal",0);

    int searchIndex = Arrays.binarySearch(foodArray, searchOb);

And that works! 那行得通!

In my opinion your mistake is 我认为您的错误是

Arrays.binarySearch(foodArray, "Cereal");

because "Cereal" is not the Object you are looking for and your array doesnt contain this object. 因为“谷物”不是您要查找的对象,并且您的数组不包含该对象。 The second parameter should be an instance of the IntoleranceFood class and "Cereal" is just a property of that class. 第二个参数应该是IntoleranceFood类的实例,而“ Cereal”只是该类的一个属性。

For your problem i would use a HashMap or another Map who fits your problem best! 对于您的问题,我将使用最适合您问题的HashMap或其他Map!

Maybe this article will help you : How to sort a HashMap in Java 也许本文会为您提供帮助: 如何在Java中对HashMap进行排序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM