当前位置: 动力学知识库 > 问答 > 编程问答 >

java: How to check StringBuilder charcters to see if it contains same characters as new string request from array?

问题描述:

I want to check if a Java StringBuilder object contains a character.

For example I have a list of foods in an array {apple, pear, orange, peach, cherry} and I want to add a random number of these food types to the StringBuilder.

How would I check if StringBuilder contains a character?

网友答案:

A different option is to create a List<String> of all of the options, and use Collections.shuffle to randomly permute the list. You can then join together the first n elements.

Since both Collections.shuffle and joining together the first n elements takes linear time, the entire process should take linear time.

网友答案:

It is of course possible to check the string by iterating through it (e.g. use indexOf()), but I would use a Set in this particular instance to add the fruits (if it already exists it will not be added again), once you're done adding fruits convert the Set into a String, e.g.

Set<String> fruits = new HashSet<String>();
for (String fruit: fruitSource) {
    fruits.add(fruit);
}
StringBuilder sb = new StringBuilder();
for (String fruit: fruits) {
    sb.append(fruit);
    sb.append(", ");
}
return sb.toString();
网友答案:

You shouldn't be "checking" anything to do that, you should do it so that duplicates never get added in the first place. To do that:

  1. start with a List<String> of the options
    • copy the list
    • add a random element to the result
    • remove that element.
    • Go back to 3 until you've added as many elements as you want or there aren't anymore left
网友答案:
import java.util.Random;

public class Test {
    public static void main(String args[]) {
        String[] fruits = { "apple", "pear", "orange", "peach", "cherry" };

        StringBuilder sb = new StringBuilder("Some fruits: apple");

        Random r = new Random();
        int N = 3;
        for (int i = 0; i < N; i++) {
            String toAdd;
            do {
                toAdd = fruits[r.nextInt(fruits.length)];
            } while (sb.indexOf(toAdd) != -1);

            sb.append(", " + toAdd);
        }

        System.out.println(sb);
    }    
}

Output:

Some fruits: apple, pear, orange, peach

But I agree with Michael Borgwardt on that it's a bad thing to check using index-of over and over again.

网友答案:

A simple way to do it is similar to Marc van Kempen's solution but smaller.

Set<String> fruits = new HashSet<String>();
StringBuilder sb = new StringBuilder();
for (String fruit: fruits) {
    if(fruits.add(fruit)){
        sb.append(fruit);
        sb.append(", ");
    }
}
return sb.toString();
网友答案:

The StringBuilder class does not have a contains() method, but String does. So you can use StringBuilder to build a string by using toString(). Then call contains() on it, to check if it contains that character or string.

Example:

stringBuilder.toString().contains(characterYouWantToCheck)
分享给朋友:
您可能感兴趣的文章:
随机阅读: