简体   繁体   English

在Java中使用toString()列出字符串

[英]List to String using toString() in java

I'm having ArrayList Contains of String. 我有ArrayList包含字符串。 I would like to check whether the character is present in the arraylist. 我想检查字符是否存在于arraylist中。 I'm using the following code. 我正在使用以下代码。

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

Can i use this toString() method. 我可以使用此toString()方法吗? What is the drawback? 缺点是什么?

It would be a really bad idea to use List.toString() and search that. 使用List.toString()进行搜索将是一个非常糟糕的主意。 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! 
} 
}

No you cannot go ahead with arraylist.toString(), as it will not provide string representation of contents in String. 不,您不能继续使用arraylist.toString(),因为它不会提供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
 }

}    

list.toString() gives you a string representation of a list and thus it contains more characters then just the concatenated list elements list.toString()为您提供列表的字符串表示形式,因此它包含更多的字符,而不仅仅是连接的列表元素

[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 它可能不适用于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;
}

尝试这个,

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

You can call toString() on any Java Object. 您可以在任何Java对象上调用toString() List is an Object which contains (you guessed it) a list of other Objects. List是一个包含(您猜对了)其他对象列表的对象。 Therefore, you can also call toString() on each Object contained within the List . 因此,您还可以对List包含的每个对象调用toString() You should read about inheritance in Java. 您应该阅读有关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. 您可能需要阅读的主题包括迭代,循环和每个循环。

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; list.toString ,它只是简单地以字符串形式返回对象的表示形式。 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. 几乎可以肯定的是, toString将为内存中的对象返回一个废话标签。 So to get at what's inside, you need to loop through the contents as shown above. 因此,要了解其中的内容,您需要遍历上面显示的内容。

if(list.toString.contains(char))

String 's contains() method won't take char as param , instead check with indexOf Stringcontains()方法不会将char作为param ,而是使用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. 解决方法是,制作一个char数组并将您的char添加到该数组中 ,然后使用contains方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM