1

I am trying to get v8 from the third array inside following arraylist

String[][] test = {
                    {"v1","v2","v3","v4","v5","v6","v7"},
                    {"v1","v2","v3","v4","v5","v6","v7"},
                    {"v1","v2","v3","v4","v5","v6","v7", "v8"}
                  };

ArrayList<String[]> test2= new ArrayList<String[]>(Arrays.asList(test));
Log.e("v1: ", "" + test2.get(0));

for (int j = 0; j <= test2.size(); j++) {
    for (String[] arrays: test2) {
        for (String string2 : arrays) {
            if (string2.equalsIgnoreCase("v8")) {
                Log.e("LOOOOOOOOOG", "" + test2.indexOf("v8")); // 3
            }else {
                Log.e("LOOOOOOOOOG", "Cant find it!!");
            }
        }
    }
}

How would i do this?

I currently just get either -1 or Cant find it!!

I am trying to solve the above problem to solve the following HashMap problem.

public static void updateJSONdata() {
    mEmailList = new ArrayList<HashMap<String, String>>();
    JSONParser jParser = new JSONParser();
    JSONObject json = jParser.getJSONFromUrl(READ_EMAILS_URL);

    try {
        mEmails = json.getJSONArray("info");


        // looping through all emails according to the json object returned
        for (int i = 0; i < mEmails.length(); i++) {
            JSONObject c = mEmails.getJSONObject(i);

            // gets the content of each tag
            String email = c.getString(TAG_EMAIL);
            String firstName = c.getString(TAG_FNAME);
            String lastName = c.getString(TAG_LNAME);
            String address = c.getString(TAG_ADDRESS);
            String phoneNumber = c.getString(TAG_PHONE);
            String city = c.getString(TAG_CITY);
            String state = c.getString(TAG_STATE);
            String zip = c.getString(TAG_ZIP);

            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            map.put(TAG_EMAIL, email);
            map.put(TAG_FNAME, firstName);
            map.put(TAG_LNAME, lastName);
            map.put(TAG_ADDRESS, address);
            map.put(TAG_PHONE, phoneNumber);
            map.put(TAG_CITY, city);
            map.put(TAG_STATE, state);
            map.put(TAG_ZIP, zip);

            // adding HashList to ArrayList
            mEmailList.add(map);

            for (HashMap<String, String> maps : mEmailList){
                 for (Entry<String, String> mapEntry : maps.entrySet()){
                    String key = mapEntry.getKey();
                    String value = mapEntry.getValue();

                    if (value.equals(passedEmail)) {
                        Log.e("Is this email in the database?", value + " Is in the database!!!");                          
                        int index = map.get(key).indexOf(value);
                        Log.e("mEmailList: ", "" + mEmailList);

//                          String[] test = mEmailList.indexOf(value);

                        fullName = mEmailList.get(index).get(TAG_FNAME) + 
                                          " " + 
                                          mEmailList.get(index).get(TAG_LNAME);

                        mPhoneNumber = mEmailList.get(index).get(TAG_PHONE);

                        mAddress = mEmailList.get(index).get(TAG_ADDRESS) + " " + 
                                          mEmailList.get(index).get(TAG_CITY) + " " + 
                                          mEmailList.get(index).get(TAG_STATE) + " " + 
                                          mEmailList.get(index).get(TAG_ZIP);
                        }
                    }

                 }
            }
        } catch (JSONException e) {
        e.printStackTrace();
        }
    }
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
  • What HashMap are you referring to in the title? – Eran Sep 07 '14 at 21:32
  • This is just an example, if i can solve this i can solve the hashmap problem. But ill add te hashmap i guess. –  Sep 07 '14 at 21:33
  • Does `test2.indexOf("v8")` even compile? `test2` contains arrays, not Strings. You need `test2.get(2).indexOf("v8")` to find the index of "v8" within the 3rd array in the list. – Eran Sep 07 '14 at 21:34
  • the name is strings but is referring to arrays. –  Sep 07 '14 at 21:37
  • why the android tag? – royB Sep 08 '14 at 18:43

2 Answers2

0

You are getting -1 because of

test2.indexOf("v8")

The ArrayList test2 contains arrays String[], it doesn't containg "v8". test2, for example, contains { "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8" }, but not "v8".

Note: The methods String#indexOf() and ArrayList#indexOf() are different, so you should read their specification before using them.

Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
  • So how should i go about it to get what i want? –  Sep 07 '14 at 21:42
  • These are different problems. Try to minimize your code. Delete non-relevant parts. Read this [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Christian Tapia Sep 07 '14 at 21:45
0

It looks like you want the index within the list that contains a map that has a specific email address as one of the values. For that purpose you need to call indexOf on the list, and pass to it a Map.

Something like : mEmailList.indexOf(map).

What you are doing is searching for the index of the first occurrence of a sub-string within another String. That won't give you an index within the list.

In addition, it looks like you are mixing the code that creates the list of maps with the code that searches the list for a specific email.

Eran
  • 387,369
  • 54
  • 702
  • 768