我需要将 input1
input2
input3
存储在给定相同大小的 char 数组
中。
public static void evalCharArray(int size){
System.out.println("please enter any three string not exceed than size"+size);
Scanner sc=new Scanner(System.in);
String input1=sc.nextLine();
String input2=sc.nextLine();
String input3=sc.nextLine();
//what i need is-I want to store these input string in three separate charArray of same length(ie of int size)
}
如果我使用 input1.toCharArray()
input2.toCharArray()
input3.toCharArray()
这些给我不同的数组基于输入的大小。
最佳答案
您可以使用 System.arraycopy()
方法。
您可以使用此示例代码。
public static void main(String[] args) {
String s = "work";
char[] arr = new char[10];
System.arraycopy(s.toCharArray(), 0, arr, 0, s.toCharArray().length );
System.out.println(arr.length); // prints 10
System.out.println(arr); // prints work
}
关于java - 如何在运行时输入时创建预定义大小的字符数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30724944/