在java中使用toString()列出字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19419915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
List to String using toString() in java
提问by dimancrown
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.
我有 ArrayList 包含字符串。我想检查字符是否存在于数组列表中。我正在使用以下代码。
if(list.toString.contains(char))
{
// enter code here
}
Can i use this toString() method. What is the drawback?
我可以使用这个 toString() 方法吗?缺点是什么?
采纳答案by Suresh Atta
if(list.toString.contains(char))
String
's contains()
method won't take char as param
, instead check with indexOf
String
的contains()
方法不会将 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 arrayand add your char in to that arrayand then use contains method.
作为一种解决方法,创建一个字符数组并将您的字符添加到该数组中,然后使用 contains 方法。
回答by Dark Knight
No you cannot go ahead with arraylist.toString(), as it will not provide string representation of contents in String.
不,您不能继续使用 arraylist.toString(),因为它不会提供字符串中内容的字符串表示。
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
}
}
回答by René Link
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;
}
回答by KyleM
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.
您可以调用toString()
任何 Java 对象。List
是一个包含(你猜对了)其他对象列表的对象。因此,您还可以调用toString()
包含在List
. 您应该阅读 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.
在您的特定情况下,您有一个字符串列表。您真正想要做的是检查列表中的每个字符串以查看该字符串是否包含特定字符。您可能想要阅读的主题包括迭代、for 循环和 for each 循环。
回答by Nishan
It would be a really bad idea to use List.toString() and search that. Your code should probably look something like this :
使用 List.toString() 并搜索它是一个非常糟糕的主意。你的代码应该是这样的:
Iterator it = list.getIterator();
char searchChar = 'S';
while (it.hasNext())
{
String s = (String) it.next();
if ( s.contains(searchChar) )
{
//Found the char!
}
}
回答by newuser
Try this,
尝试这个,
Arrays.toString(inputList.toArray()).contains(searchValue);
回答by Vidya
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.
关于list.toString
,这只是将对象的表示返回为字符串;它与内容无关。把它想象成一盒东西上的标签,上面写着“垃圾”。盒子上贴着垃圾标签,但你不知道里面装的是什么。
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.
几乎可以肯定的是,这toString
将为内存中的对象返回一个无意义的标签。因此,要了解里面的内容,您需要遍历内容,如上所示。