java 如果字符串不存在,则将字符串添加到字符串数组中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7776725/
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
Add a String to an array of Strings if it is not already present
提问by blackStar
I am trying to compare a string to an array of strings and add the string to the array if it is already not in the array.
我试图将一个字符串与一个字符串数组进行比较,如果它已经不在数组中,则将该字符串添加到该数组中。
I tried
我试过
String [] array =new String [100];
for (int i=0; i<counter; i++){
if(!str.equals(arry[i])){
array[i]=str;
counter++;
}
}
It doesn't seem to work.
So basicaly if array = (mike, john, tom, bob);
and the new string is tony
, it is supposed to compare tony
to the array and add it to the array. But if the next string is mike
, not add it to the array as it is already in the list.
它似乎不起作用。所以基本上如果array = (mike, john, tom, bob);
新字符串是tony
,它应该tony
与数组进行比较并将其添加到数组中。但是如果下一个字符串是mike
,则不要将其添加到数组中,因为它已经在列表中。
回答by phihag
回答by JB Nizet
An array has a fixed size, so adding an element to an array is not something easy. The behavior that you want is exactly the behavior of a java.util.Set
. Learn how to use the standard collections : they're much more powerful than arrays. If you want to preserve the order of the elements, then use a LinkedHashSet. See http://download.oracle.com/javase/tutorial/collections/.
数组有固定的大小,因此向数组添加元素并不容易。您想要的行为正是 a 的行为java.util.Set
。了解如何使用标准集合:它们比数组强大得多。如果要保留元素的顺序,请使用 LinkedHashSet。请参阅http://download.oracle.com/javase/tutorial/collections/。
Now, why does your code fail?
现在,为什么您的代码会失败?
You're iterating through the array, and as soon as you find one element that is not equal to the string, you replace it with the string. And you also iterate without taking the length of the array in consideration. Here's the code you might want :
您正在遍历数组,一旦找到一个不等于字符串的元素,就将其替换为字符串。而且您还进行了迭代而不考虑数组的长度。这是您可能想要的代码:
boolean found = false;
for (String element : array) {
if (str.equals(element)) {
found = true;
break;
}
}
if (!found) {
// add str to array, but where? Use a Set instead.
}
回答by Philipp Reichart
Avoid arrays if you can, Java's Collections Frameworkis so much less trouble:
如果可以,请避免使用数组,Java 的Collections Framework 的麻烦要少得多:
Keep all your names in a HashSet<String>
(use a LinkedHashSet
if order is important, as JB Nizet mentioned in a comment) and just add()
every"next name" you come across -- the set semantics will keep all elements unique and it even grows as needed, no need to create a new array and copy things around yourself.
将您所有的名字保存在一个HashSet<String>
(使用LinkedHashSet
if 顺序很重要,正如 JB Nizet 在评论中提到的那样)和您遇到的add()
每个“下一个名字”——集合语义将保持所有元素的唯一性,它甚至可以根据需要增长,没有需要创建一个新数组并复制自己周围的东西。
Set<String> names = new HashSet<String>();
names.add("mike");
names.add("john");
names.add("tom");
names.add("bob");
assert names.size() == 4;
names.add("bob");
assert names.size() == 4; // still, because "bob" was already in the set
names.add("tony");
assert names.size() == 5; // "tony" is a new unique value, so the set grows
回答by Sahil Muthoo
You should be using a Setto solve this problem. A Set is a data structure which cannot contain duplicate elements and is aptly suited to your problem.
您应该使用Set来解决此问题。Set 是一种不能包含重复元素的数据结构,非常适合您的问题。
Set<String> names = new LinkedHashSet<String>();
Collections.addAll(names, "Mike", "John", "Tom", "Bob");
names.add("Tony");
System.out.println(names); // Tony gets added to the end of the Set
names.add("Mike");
System.out.println(names); // Set already contains Mike, don't add another
Output
输出
[Mike, John, Tom, Bob, Tony]
[Mike, John, Tom, Bob, Tony]