创建字符列表

tgabmvqs  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(412)

我想做一个字符列表的数组。现在我有这个密码:

Scanner scannerMcScannersonTM = new Scanner(System.in); //This is trademarked.
    String[] inputs = new String[5];
    List<List<char>> list = new ArrayList<List<char>>();  
    for(int i =0; i < inputs.length; i++){
        System.out.println("Enter pair number " + (i+1) + " separated by a space");
        inputs[i] = scannerMcScannersonTM.nextLine();
        for(int j = 0; j < inputs[i].length(); i++){
            list[i] = inputs[i].toChar(); //it is clear that I don't know what I am doing lol

        }
    }

谢谢你的帮助!:d

egdjgwm8

egdjgwm81#

Scanner scannerMcScannersonTM = new Scanner(System.in); //This is trademarked.

//you do not need this if you know the size and not using inputs afterwards.
String[] inputs = new String[5]; 

// java collections can only contain non-primitive types, hence, use Character instead of char
List<List<Character>> list = new ArrayList<>();

for(int i=0; i < inputs.length; i++) {
    System.out.println("Enter pair number " + (i+1) + " separated by a space");

    String inputs[i] = scannerMcScannersonTM.nextLine();

    // .toCharArray() method on string does exactly what it says.
    // and Arrays.asList(.. an array ..) will convert the array to a List!
    list.add(Arrays.asList(inputs[i].toCharArray())); 
}

相关问题