繁体   English   中英

将整数从文件传递到数组

[英]Passing integers from file to array

有人可以解释一下为什么这段代码不起作用吗? 我的数组什么都没装-.-

    Integer[] tab1 = new Integer[401];
    int[][] tab2 = new int[20][20];
    File fr;
    int i = 0, c = 0;
    fr = new File("problem11");
    Scanner sc;
    try {
        sc = new Scanner(fr);
        while (sc.hasNext()) {
            // System.out.printf("%d ", sc.nextInt());
            tab1[i] = sc.nextInt();

            i++;
            System.out.print(tab1[i]);
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

您正在显示尚未填充的下一个数组元素

i++;
System.out.print(tab1[i]);

应该

System.out.print(tab1[i++]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM