dimancrown
dimancrown

Reputation: 109

List to String using toString() in java

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?

Upvotes: 0

Views: 28403

Answers (7)

René Link
René Link

Reputation: 51343

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;
}

Upvotes: 3

Suresh Atta
Suresh Atta

Reputation: 121998

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.

Upvotes: 0

Vidya
Vidya

Reputation: 30310

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.

Upvotes: 1

newuser
newuser

Reputation: 8466

Try this,

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

Upvotes: 2

Nishan
Nishan

Reputation: 2871

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! 
} 
}

Upvotes: 3

Kyle
Kyle

Reputation: 4288

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.

Upvotes: 1

Dark Knight
Dark Knight

Reputation: 8347

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
 }

}    

Upvotes: 2

Related Questions