Garg
Garg

Reputation: 2731

How to find index of ArrayList item in String Array

I want to find ArrayList<String> item's index in String Array but every time indexOf() give -1 index .

I cant understand where is wrong? Please check my code and guide me.

public static void arrangeUiComponent() {

    ArrayList<String> packageName = new ArrayList<String>();
    packageName.add("com.example.dummy");
    packageName.add("edu.app.reading");
    ArrayList<Integer> index = getIndex(packageName);

}

// searching method

private static ArrayList<Integer> getIndex(ArrayList<String> searchName) {

    ArrayList<Integer> indexList = new ArrayList<Integer>();
    String[] collectionData = new String[] { "com.example.app",
            "com.example.appdemo", "com.example.dummy", "edu.app.reading",
            "edu.app.knowledge" };

    /*
     * for iterating each and every item of list
     */
    for (int i = 0; i < searchName.size(); i++) {
        Log.i("MISSION", "value will be: " + searchName.get(i).toString());
        /*
         * for searching listItem in package name array
         */
        for (int j = 0; j < collectionData.length; j++) {
            indexList.add(collectionData.toString().indexOf(searchName.get(i).toString()));
            break;
        }
    }
    return indexList;
}

Upvotes: 1

Views: 1886

Answers (5)

Ankit Deshpande
Ankit Deshpande

Reputation: 3604

I'm assuming that in the indexLIst you want corresponding packageName's index values.

I don't think this will work in the way you want it. The indexList being an Arraylist(data is not stored in the input order) might not have corresponding index values.
Ex:
packageName list:
"com.example.dummy", "edu.app.reading"
so the indexList should have values:
2 , 3

but it might contain:
3, 2 as well because data is not stored in the order in which it is entered.

You should probably use a linkedList if you want to preserve the order. use a Hashmap<String, integer>.


You can do something like this using a hashmap:

public static void arrangeUiComponent() {

        ArrayList<String> packageName = new ArrayList<String>();
        packageName.add("com.example.dummy");
        packageName.add("edu.app.reading");
        HashMap<String, Integer> indexMap = getIndex(packageName);
        for (String s : packageName) {
            int index = indexMap.get(s);
        }

    }

    private static HashMap<String, Integer> getIndex(ArrayList<String> searchName) {

        HashMap<String, Integer> indexMap = new HashMap<String, Integer>();
        String[] collectionData = new String[] { "com.example.app", "com.example.appdemo", "com.example.dummy",
                "edu.app.reading", "edu.app.knowledge" };

        for (String search : searchName) {
            for (int i = 0; i < collectionData.length; i++) {
                if (search.equals(collectionData[i])) {
                    indexMap.put(search, i);
                    break;
                }
            }
        }
        return indexMap;
    }

Upvotes: 1

jhamon
jhamon

Reputation: 3691

Use a debugger and look the value of collectionData.toString(). It returns something that is not your list of strings. That the object representation. Quote from javadoc:

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object.

EDIT: whoops, collectionDatais an array, not a List, you should use

java.util.Arrays.asList(collectionData).indexOf(searchName.get(i))

It will search for the searchName.get(i)string inside the collectionDataList and not inside the collectionData representation which is a String (that's why indexOf is valid).

As searchName is a list of String, you don't need to add the toString() on searchName.get(i)

Upvotes: 1

Shyamkumar
Shyamkumar

Reputation: 41

toString() method of an array returns something similar to [Ljava.lang.String;@2a139a55. This is the reason you were getting index value as -1.

Other than the solution sam2090 provided, you can try 2 more options.

Replace

indexList.add(collectionData.toString().indexOf(searchName.get(i).toString()))

with

indexList.add(java.util.Arrays.binarySearch(collectionData, searchName.get(i)))

or

Replace collectionData.toString() with java.util.Arrays.toString(values)

Upvotes: 0

ashiquzzaman33
ashiquzzaman33

Reputation: 5741

You use collectionData.toString() which return [Ljava.lang.String;@15db9742. So collectionData.toString().indexof() always find nothing and return -1

To solve this you can use:

Declare arraylist as

ArrayList<String>cd = new ArrayList<String>(Arrays.asList(collectionData));

which convert String[] to ArrayList then ArrayList gives us facility of finding element with indexof().

Then in your inner for loop

for (int j = 0; j < collectionData.length; j++) {
       indexList.add(cd.indexOf(searchName.get(i).toString()));
       break;
   }

Upvotes: 0

sam
sam

Reputation: 2033

Replace

    for (int j = 0; j < collectionData.length; j++) {
        indexList.add(collectionData.toString().indexOf(searchName.get(i).toString()));
        break;
    }

with

      indexList.add(Arrays.asList(collectionData).indexOf(searchName.get(i)));

Here is the working demo of your code. Arrays.asList converts your string array to a list. Why don't you use a list instead of string collectionData array?

Upvotes: 6

Related Questions