在Java中,给字符数组赋值可以通过以下几种方法:
声明和初始化
char[] myarray = {'a', 'b', 'c'};
逐个元素赋值
char[] array = new char;
array = 'a';
array = 'b';
array = 'c';
// ...以此类推
使用`Arrays.fill()`方法
char[] array = new char;
Arrays.fill(array, 'a');
使用`String.getChars()`方法
String str = "abc";
char[] array = new char[str.length()];
str.getChars(0, str.length(), array, 0);
使用`System.arraycopy()`方法
String str = "abc";
char[] array = new char[str.length()];
System.arraycopy(str.toCharArray(), 0, array, 0, str.length());
使用`String.getBytes()`方法
String str = "abc";
byte[] bytes = str.getBytes();
char[] array = new char[bytes.length / 2];
for (int i = 0; i < array.length; i++) {
array[i] = (char) bytes[2 * i];
}
以上方法都可以用来给字符数组赋值。选择哪一种方法取决于你的具体需求