0

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.

Novarg
  • 7,390
  • 3
  • 38
  • 74

5 Answers5

2

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.

PakkuDon
  • 1,627
  • 4
  • 22
  • 21
2

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
}
Ofer Lando
  • 814
  • 7
  • 12
  • 2
    `Arrays.toString()` doesn't convert an array to an ArrayList. It does just return a String representation of the content of your array. You're probably looking for `Arrays.asList`. – Alexis C. Dec 29 '13 at 13:27
  • Thanks, ZouZou - you are obviously right - and I corrected my answer accordingly! – Ofer Lando Dec 29 '13 at 13:41
0

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.

Magnus
  • 1,550
  • 4
  • 14
  • 33
0

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.

Sitansu
  • 3,225
  • 8
  • 34
  • 61
0

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
}
The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75