新手Java
怪我咯
怪我咯 2017-04-17 12:05:27
[Java讨论组]
package practice;

public class ArrrayParser {
    public static void main(String args []){
        String s="1,2;3,4,5;6,7,8";
        double[][]d;
        String[] sFirst=s.split(";");
        d=new double[sFirst.length][];
        for(int i=0;i<=sFirst.length;i++){
            String[] sSecond=sFirst[i].split(",");
            d[i]=new double[sSecond.length];
            for(int j=0;j<=sSecond.length;j++){
                d[i][j]=Double.parseDouble(sSecond[j]);
            }
        }
        for(int i=0;i<=d.length;i++){
            for(int j=0;j<=d[i].length;j++){
                System.out.print(d[i][j]+" ");
            }
            System.out.println();
        }
    }

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
    at practice.ArrrayParser.main(ArrrayParser.java:13)

这是为何?如何解决?

怪我咯
怪我咯

走同样的路,发现不同的人生

全部回复(2)
PHP中文网

你确定最后没有少一个大括号?


package practice;

public class ArrrayParser {
    public static void main(String args[]) {
        String s = "1,2;3,4,5;6,7,8";
        double[][] d;
        String[] sFirst = s.split(";");
        d = new double[sFirst.length][];
        for (int i = 0; i < sFirst.length; i++) {
            String[] sSecond = sFirst[i].split(",");
            d[i] = new double[sSecond.length];
            for (int j = 0; j < sSecond.length; j++) {
                d[i][j] = Double.parseDouble(sSecond[j]);
            }
        }
        for (int i = 0; i < d.length; i++) {
            for (int j = 0; j < d[i].length; j++) {
                System.out.print(d[i][j] + " ");
            }
            System.out.println();
        }
    }
}

注意是小于,不是小于等于

PHP中文网

d=new double[sFirst.length][];
你确定这样通过了编译?

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号