0

I want to read a text file and insert it into a 2d array. Here is my code.

        List<String> list2 = new ArrayList<String>();

        String thisLine = null;
        while ((thisLine = input1.readLine()) != null) {
        list2.add(thisLine);
        }

        double[][] X;
        X = new double[list2.size()][];

        String[] temp;
        String delims = ",";

        for (int k1 = 0; k1 <= (X.length - 1); k1++) {
            String line = input.readLine();
            temp = line.split(delims);
            for (int i = 0; i < temp.length; i++) {
                X[k1][i] = Double.parseDouble(temp[i]);
            }
        }

The problem is that it returns nothing and as soon as I specify the number of columns in 2d array declaration line, everything is fine. Based on my java knowledge, the number of columns in 2d array declaration is not necessary and in my project (Since we apply different datasets), the number of columns is not specified.

MTT
  • 5,113
  • 7
  • 35
  • 61

2 Answers2

2

A 2d array is basically an array of arrays. Let's call it an outer array consisting of inner arrays.

When initializing a 2d array, it's true that you only have to declare the length of the outer array. I.e. the number of rows. By doing this, only the outer array will be initialized. The inner arrays are yet to be initialized.

In fact, these two arrays will contain the exact same elements:

double[][] array1 = new double[3][];
double[][] array2 = {null, null, null};

To initialize an inner array, you could do one of these:

array1[0] = new double[4];
array1[0] = new double[] {1.0, 3.14, 42.0, 2.718};

In your case, you could simply put this in your code to initialize the inner arrays:

temp = line.split(delims);
X[k1] = new double[temp.length]; //<- Add this line
Lone nebula
  • 4,768
  • 2
  • 16
  • 16
1
X = new double[list2.size()][];

You have now created a jagged array, which is an array of arrays, bound by list2.size().

X[k1][i] = Double.parseDouble(temp[i]);

You have attempted to dereference a spot in the jagged array which does not exist. This is bad.

If the number of columns in your spec isn't specified, then it's provided either by the length of the arguments you parse, or implicitly by the element(s) you attempt to place into that location.

This would work, since it defines an array, which is what the jagged array needs...

X[k1][i] = new double[] {Double.parseDouble(temp[i])};

...but it would only give you a two-dimensional array effective as new Double[list2.size()][1].

Considering that you make use of String[] temp, perhaps instead, you want to use that as the condition on your jagged array? Here's a quick and dirty way to do it that involves more variables than I'd like, but would work:

String[] temp;
double[] tempDouble;

for (int k1 = 0; k1 <= (X.length - 1); k1++) {
    temp = input.readline().split(delims);
    tempDouble = new double[temp.length];
    for(int i = 0; i < temp.length; i++) {
        tempDouble[i] = Double.parseDouble(temp[i]);
    }
    // a bit further down, you can use the array as part of your jagged array.
    X[k1] = tempDouble;
}
Community
  • 1
  • 1
Makoto
  • 104,088
  • 27
  • 192
  • 230