灌装Java中的数组bidimensionnal [英] filling an bidimensionnal array in java

查看:199
本文介绍了灌装Java中的数组bidimensionnal的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有填充bidimensionnal双阵列出多维对象数组的一些问题。
我的问题是空值都造成了显示java.lang.NullPointerException
    在IOControl.ReadCSV $ 1.compare(ReadCSV.java:328)
    在IOControl.ReadCSV $ 1.compare(ReadCSV.java:1)
    在java.util.TimSort.countRunAndMakeAscending(来源不明)
    在java.util.TimSort.sort(来源不明)
    在java.util.TimSort.sort(来源不明)
    在java.util.Arrays.sort(来源不明)
    在IOControl.ReadCSV.run(ReadCSV.java:324)
    在en.window.Main.main(Main.java:46)
,我真的不明白为什么。

我在做什么?
我读一个CSV - 文件,我存储通过线路输入行,在我的对象数组数据名为内容
那么我灌了双击二维阵列名为 sortedOutput 中,我想用来存储 LeftSpeed​​ NormAvgPowOutput 这是存储在双值我数据阵列。

我想什么:
有我的 sortedOutput bidimensionnal数组从最小的到尺寸0的最大值进行排序:变量 LeftSpeed​​

那么,怎样才能避免我的空值?因为当我试图发现他们却使我的第二个数组中,compilator说我比不上双到值。

对不起,长的帖子,希望你们能帮助我了:)
在这里,我的code:

 公共无效的run(字符串路径,INT长度)
{
/ *
* ReadCSV类的主要功能。
 *打开/读取/关闭文件。
 *填充的对象。
 * /  BR的BufferedReader = NULL;
  字符串输入=;
  串cvsSplitBy =,;
  INT [] = POS INT新[200];  数据[] =内容新的数据[长度]
  双[] [] = sortedOutput新双[长度] [4];
  INT I = 0;
  INT J = 0;
  INT K = 0;  尝试
  {
    BR =新的BufferedReader(新的FileReader(路径));
    而((输入= br.readLine())!= NULL)
     {
       //使用逗号作为分隔符
       的String [] =行input.split(cvsSplitBy);
       内容[i] =新的Data();
       如果(我== 0)
       {
         //不相关这里
       }
       其他
       {
        J = 0;
        内容[I] .setTime(TIME,的getTime(POS [J ++],行));
        内容[I] .setData(DATA的getContent(POS [J ++],行));
        //等等
       }
       //摆脱非相干或无用的值,例如负电源,...
       如果(内容[I] .lhWdStdev大于0&放大器;&放大器;内容[I] .rhWdStdev大于0)
       {
        normalizeData(内容由[i]); // 不相关
        内容[k]的.setLeftSpeed​​();
        sortedOutput [K] [0] =内容[k]的.getLeftSpeed​​();
        sortedOutput [k]的[2] =内容[k]的.getNormAvgPowOutput();
        Arrays.sort(sortedOutput,新的java.util.Comparator<双[]>()
        {
           公众诠释比较(双人间[]一,双[] B)
           {
             返回Double.compare(一个[0],B [0]);
            }
         });
   }
   如果(sortedOutput [K] [0] == NULL)
   {
     的System.out.println(失败);
   }
   的System.out.println(输出=+ sortedOutput [K] [0]);
   我++;
   ķ++;
 }
}
赶上(FileNotFoundException异常E)
  {
    e.printStackTrace();
  }
赶上(IOException异常E)
  {
    e.printStackTrace();
  }
最后
  {
  //同样是不相关
}

编辑:所以最后我做了2个巨大的misstakes。
首先,我想在循环中的数组进行排序,这意味着之前被填满(感谢@Ted霍普)。
其次我没有正确处理值。我应该检查是否( A!= NULL B!= NULL A [ 0]!= NULL b [0]!= NULL ),然后返回新秩序。 (感谢@jboi)。


解决方案

我从stacktace看到的是,该命令 Double.compare(A [0],B [0]); 是导致NullPointerException异常。这意味着,一个二是。你可以作为一个快速的解决办法是改变用命令:

<?pre> Double.compare((A [0] == NULL)Double.NaN:A [0],(B [0] == NULL)Double.NaN:2 B [0]);

这是你需要理解为什么是空,是什么在CSV文件中,这使得它们空,你如何​​处理这些数据只能是快速和肮脏的。

I'm having some issues filling a bidimensionnal double-array out of a multi-dimensional object-array. My problem is that null value are causing a java.lang.NullPointerException at IOControl.ReadCSV$1.compare(ReadCSV.java:328) at IOControl.ReadCSV$1.compare(ReadCSV.java:1) at java.util.TimSort.countRunAndMakeAscending(Unknown Source) at java.util.TimSort.sort(Unknown Source) at java.util.TimSort.sort(Unknown Source) at java.util.Arrays.sort(Unknown Source) at IOControl.ReadCSV.run(ReadCSV.java:324) at en.window.Main.main(Main.java:46) , and i really don't understand why.

What am i doing ? I'm reading a CSV - file, i'm storing the input line by line, in my array of object Data called content. Then i'm filling a double bidimensional array called sortedOutput in which i would like to store LeftSpeed and NormAvgPowOutput which are double value stored in my Data array.

What i want : To have my sortedOutput bidimensionnal array sorted from the smallest to the biggest value on dimension 0 : for variable LeftSpeed.

So how can i avoid the null values? Because when i try to spot them while filling my second array, the compilator says i can't compare a double to a null value.

Sorry for the long post, hope you guys can help me out :) Here my code :

public void run(String path, int length) 
{
/* 
* Main function of ReadCSV class.
 * Open / reads / closes the file.
 * Fills the object.
 */

  BufferedReader br = null;
  String input = "";
  String cvsSplitBy = ",";
  int[] pos = new int[200];

  Data[] content = new Data[length];
  Double[][] sortedOutput = new Double[length][4];
  int i = 0;
  int j = 0;
  int k = 0;

  try 
  {
    br = new BufferedReader(new FileReader(path));
    while ((input = br.readLine()) != null) 
     {
       // use comma as separator
       String[] lines = input.split(cvsSplitBy);
       content[i] = new Data(); 
       if (i == 0)
       {
         //not relevant here                
       }
       else
       {
        j = 0;
        content[i].setTime("TIME", getTime(pos[j++], lines));
        content[i].setData("DATA", getContent(pos[j++], lines));
        //etc
       }
       // gets rid of non coherent or useless values, e.g negative power, ...
       if (content[i].lhWdStdev > 0 && content[i].rhWdStdev > 0)
       {
        normalizeData(content[i]); // not relevant
        content[k].setLeftSpeed();
        sortedOutput[k][0] = content[k].getLeftSpeed();
        sortedOutput[k][2] = content[k].getNormAvgPowOutput();
        Arrays.sort(sortedOutput, new java.util.Comparator<Double[]>()
        {
           public int compare(Double[]a, Double[]b)
           {
             return Double.compare(a[0], b[0]);
            }
         });
   }
   if (sortedOutput[k][0] == null)
   {
     System.out.println("FAIL");
   }
   System.out.println("Output = " +sortedOutput[k][0]);
   i++;
   k++;
 }
} 
catch (FileNotFoundException e) 
  {
    e.printStackTrace();
  } 
catch (IOException e) 
  {
    e.printStackTrace();
  } 
finally 
  {
  // Again it's not relevant
}

EDIT : So finally i made 2 huge misstakes. First i tried to sort the array in the loop, that means BEFORE it was fully filled (thanks @Ted Hopp). Secondly i didn't handle the null values correctly. I should have checked if (a != null, b != null, a[0] != null, b[0] != null ) and then return the new order. (thanks @jboi ).

解决方案

What I see from the stacktace is, that the command Double.compare(a[0], b[0]); is causing the NullPointerException. That means, that one the two is null. What you can to as a quick solution is to change the command with:

Double.compare((a[0]==null)? Double.NaN:a[0], (b[0]==null)? Double.NaN:b[0]);

This can only be quick and dirty as you need to understand WHY there're null, what is in the CSV File, that makes them null and how you handle such data.

这篇关于灌装Java中的数组bidimensionnal的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆