6

I am trying to convert String builder to String array in java.I have used to String method to convert the Stringbuilder to String and then use split method to convert it to String array.

I am facing some issue in array size. The output of String builder is as follows: 0,,1,,,,,,,,,,2,,,. Actually it contains 15 elements, but it's showing the size of stringbuilder as 18. And after converting to String array, it's showing size as 13.

Am I doing wrong method? Is there is other method to convert String builder to String Array?

StringBuilder output = new StringBuilder();

for (String str : listToSearch) {
    int index = findIndex(headerArrayList, str);
    if (index > -1) {
        output.append(index);
    } else {
        output.append(str);
    }
    output.append(",");
}

String out = output.toString();
String[] destIndexArray = out.split(",");

The findIndex method:

private static int findIndex(List<String> headerList, String element) {
    for (int i = 0; i < headerList.size(); i++) {
        if (headerList.get(i).equals(element)) {
            return i;
        }
    }
    return -1;
}
5
  • 1
    How about using a list instead of the stringbuilder?? Commented Dec 26, 2016 at 6:13
  • you should use a list, instead of String builder. At last just convert it into Array Commented Dec 26, 2016 at 6:13
  • 1
    Also check this: docs.oracle.com/javase/7/docs/api/java/util/… Commented Dec 26, 2016 at 6:16
  • But it contains both integer values and String values. Commented Dec 26, 2016 at 6:18
  • see finally, you are getting an array of String. So in list just save integer as String. Commented Dec 26, 2016 at 6:22

3 Answers 3

5

Am I doing wrong method?

There are many places your code need improvements. I'm suggesting some:

  1. Instead of re-inventing wheel, look for existing API: https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#indexOf(java.lang.Object)

  1. You can add only Strings instead of index.

for (String str :listToSearch) {
    int index=headerArrayList.indexOf(str);
    if (index < 0) {
        output.append(str);
        output.append(",");
    }
    // if(!headerArrayList.contains(str))
    // output.append(str);
    // output.append(",");
}

  1. Use List<String> instead of StringBuilder.

Is there is other method to convert String builder to String Array?

Yes, but if you follow above points, you won't need it. but since you asked. See this: How can a StringBuilder best be converted to a String[]?

Sign up to request clarification or add additional context in comments.

Comments

2

See using stringbuilder and again manipulating it and converting it into array is not a good idea. Instead insert everything into list of string, and to check if string is integer or not use this :

str.matches("^-?\\d+$");

And if you want to convert it into array you can do that

String[] str = list.toArray(new String[list.size()])

Comments

1

Have you simply tried in it with one line code:

stringBuilder.toString().split("delimiter"); ?

Which is in your case, might look like:

destIndexArray = output.toString().split(",");

If that doesnt work, then instead of converting into a StringBuilder, you can do this:

List<String> output = new List<String>();

for (String str : listToSearch) {
int index = findIndex(headerArrayList, str);
if (index > -1) {
    output.add(index);
} else {
    output.add(str);
}
}

I think in this case you can do whatever with your list, no need of delimiter. Hope it helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.