java - 如何在运行时输入时创建预定义大小的字符数组?

标签 java arrays

我需要将 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/

相关文章:

java.lang.IllegalStateException : Failed to load ApplicationContext

java - Spring Controller RequestMapping PathVariable 中 URL 的零长度部分破坏了解析

arrays - 对成员 '+' 的引用不明确

java - 初级Java : Storing 7 unique random integers in a size 7 array

javascript - Jquery比较并返回字符串数组

java - 是否有与 C# linq let 等效的 java lambda 表达式?

java - 获取使用 ProcessBuilder 启动的进程的内存使用情况和执行时间

java - 编写调用成员变量的方法

ios - 如何使用 Segue 将 PHAsset 数组传递到另一个 ViewController

javascript - 数组排序: Alphabetical and Numerically