0

I'm having ArrayList Contains of String. I would like to check whether the character is present in the arraylist. I'm using the following code.

if(list.toString.contains(char))
{
   // enter code here
}

Can i use this toString() method. What is the drawback?

4
  • 1
    Wait, what? You're searching a character in a list of strings? Can you give an example of what your list actually contains? If you meant that you're trying to see if your list of string contains a particular string, then you should iterate through the list, or use the indexOf method on the list. Commented Oct 17, 2013 at 6:45
  • 1
    which char are you checking ??? only that can make a difference Commented Oct 17, 2013 at 6:46
  • Hi,my list contains String values. for Example my list is [L2, L1, 0, R1, R2, L4, L3, 0, R3, R4, L6, L5, 0, R5, R6, L8] and i wanna check whether the 'U' is present in this String. if (list.toString().contains("U")) { // } Commented Oct 17, 2013 at 6:49
  • 2
    So, your list has strings like "ABC", "DEF", "GHI". And do you want to know if it contains the String "DEF"? Or do you want to know if it contains any String that has the character 'B'. If you want to see if it contains "DEF", then you can do if(list.indexOf("DEF") > -1){...}. If you want to see if the Strings contain a particular character, you should do for (String s : list) { if (s.indexOf('B') > -1) { ... } } Commented Oct 17, 2013 at 6:56

7 Answers 7

3

It would be a really bad idea to use List.toString() and search that. Your code should probably look something like this :

Iterator it = list.getIterator(); 
char searchChar = 'S';

while (it.hasNext())  
{ 
String s = (String) it.next(); 
if ( s.contains(searchChar) ) 
{   
//Found the char! 
} 
}
Sign up to request clarification or add additional context in comments.

Comments

3

list.toString() gives you a string representation of a list and thus it contains more characters then just the concatenated list elements

[stringElement1, stringElement2, ... ]

Therefore your approach will not work if the character you are looking for is , , , [ or ]. And keep in mind that this string representation is implementation specific. It might not work for other list implementations than ArrayList

I would recommend to write a method linke this:

private boolean listElementContains(List<String> list, String subString){
    for(String element : list){
        if(element.contains(subString)){
            return true;
        }
    }
    return false;
}

1 Comment

Indeed, consider an implementation of List whose toString method returns "UnindexedFooList of [L1, R1]" that would contain U every time!
2

No you cannot go ahead with arraylist.toString(), as it will not provide string representation of contents in String.

Better approach is to iterate over list and check, as below.

for(String detail:listString){

 if(detail.contains('X')) //replace 'X' with your character
 {
   // do somethng
 }

}    

Comments

2

Try this,

Arrays.toString(inputList.toArray()).contains(searchValue);

Comments

1

You can call toString() on any Java Object. List is an Object which contains (you guessed it) a list of other Objects. Therefore, you can also call toString() on each Object contained within the List. You should read about inheritance in Java.

In your particular case, you have a List of Strings. What you actually want to do is check each String in the List to see if the String contains a particular character. Topics you may want to read about include iteration, for loops, and for each loops.

1 Comment

By the way, unlike many posters here, I intentionally didn't give you the code because this is an obvious homework question that you didn't seem to put much effort into.
1

If I understand this correctly, your code would look like this:

List<String> strings = new ArrayList<>();
//add strings to list
for (String string : strings) {
    //Look for some character c
    if (string.indexOf(c) >= 0) {
        return true;
    }
}

return false;

On the matter of list.toString, that simply returns a representation of the object as a string; it has nothing to do with the contents. Think of it like a label on a box of stuff that says "Junk." The box is labeled Junk, but you have no idea what's in it.

What's nearly certain is that toString will return a nonsense label for the object in memory. So to get at what's inside, you need to loop through the contents as shown above.

Comments

0
if(list.toString.contains(char))

String's contains() method won't take char as param, instead check with indexOf

Your code works, with little modifications.

A small example here:

     List<String> list= new ArrayList<>();
     list.add("test");
     list.add("test2");
     if (list.toString().indexOf('t') > -1)      // True
     {
         System.out.println("yes there");
     }

Note:

As a workaround, Make an char array and add your char in to that array and then use contains method.

3 Comments

contains method is nothing but implementation of indexOf. In java by default it's calling the indexOf method while calling the contains() right?
Yes, Exactly. Your understanding is correct. But you cannot pass a char to contains method directly. :)
@dimancrown If you see the API of string class, There are no method called contain which allows char as a argument. There is a method contain which allows a char[], that is String. So what you can do is, Take a character array.put your char in that and pass the array. Simple.

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.