Java 如何在运行时输入时创建预定义大小的字符数组?
我需要在给定大小的字符数组中存储input1 input2 input3Java 如何在运行时输入时创建预定义大小的字符数组?,java,arrays,Java,Arrays,我需要在给定大小的字符数组中存储input1 input2 input3 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();
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.tocharray input2.tocharray input3.tocharray,则会根据输入提供不同大小的数组。您可以使用方法
您可以使用此示例代码
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
}
你可以使用这个方法
您可以使用此示例代码
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
}
使用Arrays.copyOfT[],int
复制指定的数组,如果需要,则截断或填充为null
必要时,副本具有指定的长度
使用Arrays.copyOfT[],int
复制指定的数组,如果需要,则截断或填充为null
必要时,副本具有指定的长度
在单个字符数组中输入1、输入2和输入3?或者3个大小相同的单独字符数组?这些是单独的数组。请输入任何三个不超过大小的字符串。这意味着什么?如果用户输入的字符串超过大小字符,该怎么办?@NamanGala此大小是字符数组的大小,因为我们必须将其放入长度大小的字符数组中。input1,input2,在单个字符数组中输入3?或者3个大小相同的单独字符数组?这些是单独的数组。请输入任何三个不超过大小的字符串。这意味着什么?如果用户输入的字符串超过大小字符,该怎么办?@NamanGala此大小是字符数组的大小,因为我们必须将其放入长度大小的字符数组中。