【问题标题】:Java - Convert user input to 2d integer arrayJava - 将用户输入转换为二维整数数组
【发布时间】:2021-06-22 20:39:04
【问题描述】:

如何转换用户输入(第一个输入是大小):

4
1 2
3
3
-1

相当于:

int graph[][] = {{1,2},{3},{3},{}};

我的尝试是:

        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();

        int graph2[][] = {{1,2},{3},{3},{}};
        int graph[][] = new int[n][n];

        for (int i = 0; i < n; i++) {
                int counter = 0;
                char[] arr = sc.nextLine().toCharArray();
                for (char c : arr){
                    graph[i][counter++] = (int)c-48;
                }

        }
        System.out.println(Arrays.deepToString(graph2));
        System.out.println(Arrays.deepToString(graph));

但是,我必须输入没有空格:

4
12
3
3

输出是:

Need it in : [[1, 2], [3], [3], []]
Crrent Output : [[0, 0, 0, 0], [1, 2, 0, 0], [3, 0, 0, 0], [3, 0, 0, 0]]

【问题讨论】:

  • -1 是否表示空值?
  • @Abhinav 是的,这意味着空
  • @maio290 这是我第一次添加问题,对不起。我会添加我的尝试

标签: java multidimensional-array data-structures graph tree


【解决方案1】:

一些简单的代码:

import java.util.Arrays;
import java.util.Scanner;

public class Array2D {
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int size = in.nextInt();
    //consume the extra line
    in.nextLine();  
    int[][] array = new int[size][];
    for(int i = 0 ;i <size; i++) {
        String line = in.nextLine();
        array[i] = toIntArray(line.split(" "));
    }
}

public static int[] toIntArray(String[] arr){
    int intArray[] = new int[arr.length];
    for(int i=0;i<arr.length;i++) {
        intArray[i] = Integer.parseInt(arr[i]);
    }
    return intArray;
}

}

【讨论】:

  • 嘿,我得到了您的代码的以下输出 [[1, 2], null, null, null] [[1, 2], [3], null, null] [[1, 2],[3],[3],空]
  • 非常感谢,这非常有用。它按要求工作。
【解决方案2】:

我有下面的代码为我工作,至少对于你提到的测试用例。我不确定您是否仅限于使用数组

public static void main(String[] args) throws Exception {
    //The part to take input from user
    BufferedReader  br = new BufferedReader (new InputStreamReader(System.in));
    
    //Take the first input and parse it to Integer
    int T = Integer.parseInt(br.readLine());
    
    //Create a new arraylist which can fit as per input
    ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
    
    //Will take inputs from user T times
    while(T-- >0) {
        //Handle the case for space separated inputs 
        String[] inp = br.readLine().split(" ");
        
        ArrayList<Integer> inner = new ArrayList<>();
        
        //If there are any elements in current line of input , convert it to Integer by parsing and add to inner array
        for(String s : inp) {
            if(Integer.parseInt(s) >= 0) {
            inner.add(Integer.parseInt(s));
            }
        }
        
        graph.add(inner);
    }
    
    System.out.println(" Graph is " + graph);
    // Graph is [[1, 2], [3], [3], []]
        
}

【讨论】:

    猜你喜欢
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    • 2018-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多