No, that solution is absolutely correct and very minimal.
Note however, that this is a very unusual situation: Because String is handled specially in Java, even "foo" is actually a String. So the need for splitting a String into individual chars and join them back is not required in normal code.
Compare this to C/C++ where "foo" you have a bundle of chars terminated by a zero byte on one side and string on the other side and many conversions between them due do legacy methods.
How to concat two char arrays in java? - Stack Overflow
Convert Character array to string in Java - Stack Overflow
String.join method not working for array/stack despite me having updated JDK?
A quick and easy way to join array elements with a separator (the opposite of split) in Java - Stack Overflow
What is a char array in Java?
What are the performance implications when converting a char array to a string?
How can I check if a string is empty or null after converting a char array to a string?
No, that solution is absolutely correct and very minimal.
Note however, that this is a very unusual situation: Because String is handled specially in Java, even "foo" is actually a String. So the need for splitting a String into individual chars and join them back is not required in normal code.
Compare this to C/C++ where "foo" you have a bundle of chars terminated by a zero byte on one side and string on the other side and many conversions between them due do legacy methods.
String text = String.copyValueOf(data);
or
String text = String.valueOf(data);
is arguably better (encapsulates the new String call).
It depends I guess. The simpler approach would be just to convert the char arrays to a String and concaternate the Strings.
A better approach would be to use StringBuilder
char info[] = new char[10];
char data[] = new char[10];
// Assuming you've filled the char arrays...
StringBuilder sb = new StringBuilder(64);
sb.append(info);
sb.append(data);
char result[] = sb.toString().toCharArray();
try this
char result[] = new char[info.length + data.length];
System.arraycopy(info, 0, result, 0, info.length);
System.arraycopy(data, 0, result, info.length, data.length);
Character[] a = ...
new String(ArrayUtils.toPrimitive(a));
ArrayUtils is part of Apache Commons Lang.
The most efficient way to do it is most likely this:
Character[] chars = ...
StringBuilder sb = new StringBuilder(chars.length);
for (Character c : chars)
sb.append(c.charValue());
String str = sb.toString();
Notes:
- Using a StringBuilder avoids creating multiple intermediate strings.
- Providing the initial size avoids reallocations.
- Using
charValue()avoids callingCharacter.toString()...
However, I'd probably go with @Torious's elegant answer unless performance was a significant issue.
Incidentally, the JLS says that the compiler is permitted to optimize String concatenation expressions using equivalent StringBuilder code ... but it does not sanction that optimization across multiple statements. Therefore something like this:
String s = ""
for (Character c : chars) {
s += c;
}
is likely to do lots of separate concatenations, creating (and discarding) lots of intermediate strings.
I'm trying to convert all the character elements in my stack into an element of type String. For example - if my stack has [a,b,c] then I want to use the .join method to create a string "abc".
This is how I'm calling the method:
Stack mystack = new Stack<>();
finalstring = String.join("/", mystack);However, eclipse gives me the following error message: The method join(CharSequence, CharSequence...) in the type String is not applicable for the arguments (String, Object[])
IntelliJ gives me a similar error message. Searching this message up, the common agreement is that the reason this is happening is that I'm using an earlier version of Java. However, I am using Java 8. When I write in my command prompt java --version it says I'm using JDK 17. In Eclipse and IntelliJ, it also tells me the JDK I'm using is version 17.
I don't know why I'm still getting this error message. Any ideas would be greatly appreciated.
Using Java 8 you can do this in a very clean way:
String.join(delimiter, elements);
This works in three ways:
1) directly specifying the elements
String joined1 = String.join(",", "a", "b", "c");
2) using arrays
String[] array = new String[] { "a", "b", "c" };
String joined2 = String.join(",", array);
3) using iterables
List<String> list = Arrays.asList(array);
String joined3 = String.join(",", list);
If you're on Android you can TextUtils.join(delimiter, tokens)