In Java, I have been trying to append a string to a Char Array. I am using the code:
list = (new String(list) + word).toCharArray();
With list being the char array, and word being a string.
What am I doing wrong?
In Java, I have been trying to append a string to a Char Array. I am using the code:
list = (new String(list) + word).toCharArray();
With list being the char array, and word being a string.
What am I doing wrong?
Make sure that:
char[] or byte[] (or String, StringBuilder, StringBuffer but that's a bit off-topic)list != nullExplanation:
String one-argument constructor only takes the types I've listed abovenew String(null) is an ambiguous call to the String one-argument constructorword can be any type, including a null object (in which case it will be represented as "null")You could show us the exact error what you get. By using the below code I can get some output without any error.
String word="hi";
char[] list=null;
if(list!=null)
list = (new String(list) + word).toCharArray();
else
list = word.toCharArray();
for(char ch: list)
System.out.println("List: "+ch);