Anik_Datta
Anik_Datta

Reputation: 1

Check a string is present in the ArrayList?

I have a string. Now how to check that the string is present in my array list?

Suppose:

str="nokia"
mobiles[0]="samsung"
mobiles[1]="nokia"
mobiles[2]="blackberry"

I have tried

Boolean match=false;    
for(int j = 0 ; j <= 15 ; j++) {
   match =mobiles[j].compare(str);
   if(match == true) {
     break;
   }
}

But .compare(str) is showing error.

Upvotes: 0

Views: 804

Answers (5)

PakkuDon
PakkuDon

Reputation: 1615

I'm guessing that compare(str) is throwing an error because there is no function named compare() for the String class.

You could use equals() or equalsIgnoreCase() for case-insensitive matching. There's also compareTo() and compareToIgnoreCase() which are closer to what you typed. Note that the compareTo methods return an int value.

You can read up on the method descriptions here.

Here's an example using equalsIgnoreCase().

boolean match = false;    
for (int j = 0; j < mobiles.length; j++)
{
    match = mobiles[j].equalsIgnoreCase(str);
    if (match)
    {
         break;
    }
} 

I've changed the terminating condition on the loop to use the length of the mobiles array. That way you won't need to change a hard-coded value in the event that you change the length of the array. It may also make the intent of the loop a little clearer.

Also as pointed out by ZouZou, writing match == true isn't necessary since match is itself a boolean value. This case doesn't seem to require the use of a wrapper class either so you could just declare match as the primitive type boolean.

Upvotes: 2

The Guy with The Hat
The Guy with The Hat

Reputation: 11132

I would use Arrays's binarySearch(Object[] a, Object key) method in conjunction with its sort(Object[] a) method.

import java.util.Arrays; //put this at the top of your code

//your code here

String[] tempMobiles = mobiles; //temporary array that will be modified
Arrays.sort(tempMobiles); //sorts tempMobiles to prepare it for the next line
if(tempMobiles[Arrays.binarySearch(tempMobiles, str)].equals(str)) //if the string at the location returned by the binarySearch equals str...
{
    match = true; //sets match to true
}

Upvotes: 0

Ofer Lando
Ofer Lando

Reputation: 824

In the question you mentioned an ArrayList but your example shows an Array. If you really have an ArrayList, just use something like:

if(mobiles.contains(str))
{
    //code here
}

If you have an Array, you can use the same "contains" after converting to ArrayList, using something like:

if(Arrays.asList(mobiles).contains(str))
{
    //code here
}

Upvotes: 2

Magnus
Magnus

Reputation: 1640

I suggest not using an operation for comparing the strings, but to do it directly:

boolean match = false;
for (j=0; j<=mobiles.length; j++) {
    if (mobiles[j].equals(str)) {
        match=true; break;
    }
}

If you don't need the match boolean later, you can delete that.

Upvotes: 0

Sitansu
Sitansu

Reputation: 3329

Try it:

 match =mobiles[j].equalsIgnoreCase(str);

equalsIgnoreCase compares this String to another String, ignoring case considerations. Two strings are considered equal ignoring case if they are of the same length, and corresponding characters in the two strings are equal ignoring case.

Upvotes: 0

Related Questions