1

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;
}
2
  • @jhamon i have already check it that fine.... Commented Oct 6, 2015 at 12:17
  • Your collectionData is a String[] which does not have the nice pretty printing as collections. collectionData.toString() gives you something as [Ljava.lang.String;@58ceff1 which is not met by th search names. Use a collection for the collection data and the results will differ. Commented Oct 6, 2015 at 12:33

5 Answers 5

6

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?

Sign up to request clarification or add additional context in comments.

2 Comments

You shouldn't use asList inside the loop. It will create a new LIst everytime, so will be slower when the packageName array becomes large.
@AnkitDeshpande I understand which is why I asked OP why not use a list instead of an array.
1

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)

2 Comments

i try collectionData.indexOf(searchName.get(i)) this but it show error :Cannot invoke indexOf(String) on the array type String[]
Shooting too fast heh =)
1

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;
    }

Comments

0

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;
   }

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.