如何将String类型的二维数组转化为float类型的二维数组

fengmochen 2014-09-18 12:30:18
如题:
下面是我的代码
public float[][] Tofloat(String[][] str){

float[][] array1=new float[str.length][];
for(int i=0; i<str.length; i++){
for(int j=0; j<str[i].length; j++){

array1[i][j]=Float.parseFloat(str[i][j]);
}
}
}
但是出错!
错误如下:
Exception in thread "main" java.lang.NullPointerException
如何破?
...全文
397 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
S117 2014-09-18
  • 打赏
  • 举报
回复

    String[][] str = {{"1","2","3",},{"4","5","6"},{"7","8","9"}};
    float[][] array = new float[str.length][];
    for(int i = 0 ; i< str.length;i++){
      array[i] = new float[str[i].length];
      for(int j = 0; j < str[i].length;j++){
        array[i][j] = Float.valueOf(str[i][j]);
      }
    }
白开水MD5 2014-09-18
  • 打赏
  • 举报
回复
循环之前加判断 1、判断str是否为null 2、判断str的长度是否为0 你直接取str[0][0]有可能str为null或者不为null但它的没有00这个值

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧