dimancrown dimancrown - 2 years ago 148
Java Question

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.

// enter code here

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

Answer Source

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<>();
     if (list.toString().indexOf('t') > -1)      // True
         System.out.println("yes there");


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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download