【Java acm】特殊输入

input:
[[1,2,3 ],
[4, 5,6],
[7,8]]

output:
[[1, 2, 3], [4, 5, 6], [7, 8]]

思路

按行读入, 然后进行字符串处理, 将其他字符替换为空字符.在split(,)
repalceAll(“\s+”,“”), 将所有空白字符替换成空字符(包括空格, 制表, 换行等)

代码实现

import java.util.*;

public class test {
    public static void myFunc(ArrayList> arr){
        //测试展示
        System.out.println(arr);
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        ArrayList> arr = new ArrayList<>();
        while (sc.hasNextLine()) {
            ArrayList row = new ArrayList<>();

            String s = sc.nextLine();
            /*
            [[1,2,3] ,
            [3,4,5]]
             */
            String[] list = s.trim().replace("],","")
                    .replace("[", "")
                    .replace("]", "")
                    .replace(" ","")
                    .split(",");
            for (int i = 0; i < list.length; i++) {
                int tmp = Integer.parseInt(list[i]);
                row.add(tmp);
            }
            arr.add(row);
        }

        myFunc(arr);
    }
}

你可能感兴趣的:(IO,java)