Víctor Martín
Víctor Martín

Reputation: 3450

Using Arrays.asList and compare

I have an array like the next:

this.ntpServers[0][0] = "Global";
this.ntpServers[0][1] = "pool.ntp.org";
this.ntpServers[1][0] = "Africa";
this.ntpServers[1][1] = "africa.pool.ntp.org";
this.ntpServers[2][0] = "Asia";
this.ntpServers[2][1] = "asia.pool.ntp.org";
this.ntpServers[3][0] = "Europe";
this.ntpServers[3][1] = "europe.pool.ntp.org";
this.ntpServers[4][0] = "North America";
...
this.ntpServers[85][0] = ...
this.ntpServers[85][1] = ...

I have in another String a country, and I am trying to use the next code to compare if exist in the list, but Iit doesn't returns me a true, when it must be true. If I check for "Asia" it would be true. But something is wrong.

gettedCountry is a String

public int existNTP(String[][] list) {

    if(Arrays.asList(list).contains(gettedCountry)){

        Log.i("Found", "Found");

        }

        return position;
    }

Thank for your help.

Upvotes: 0

Views: 1169

Answers (2)

Vincent Mimoun-Prat
Vincent Mimoun-Prat

Reputation: 28541

Either make proper objects to turn your two-dimentional array into a list (recommended)

OR

iterate over your array:

int position = 0;
for (String[] entry : ntpServers) {
  if (entry[0].equals(country)) return position;
  ++position;
}

return -1; // Not found is an invalid position like -1

Upvotes: 1

Mohamed_AbdAllah
Mohamed_AbdAllah

Reputation: 5322

Arrays.asList(list) will return an ArrayList of String[] not String. If you need to check for the first item:

ArrayList<String[]> arr=Arrays.asList(list);

for(String[] arry : arr){
  if(arry[0].equals(gettedCountry)) /* Do your stuff */;
}

Upvotes: 0

Related Questions