-3

I would need to create an ArrayList of 6 elements vectors (vectors are double types) but cannot find the way to properly do it. I don't seem to find the right type to declare for my ArrayList.

Would anyone be able to help me out?

A draft of my code would be:

public static ArrayList<> readFile(File file) throws IOException{

    // Array containing all spacecraft states
    ArrayList<> manPlan = new ArrayList<>();

    // File reader
    BufferedReader reader = Files.newBufferedReader(file.toPath(), charset);

    String line;
    while ((line = reader.readLine()) != null) {
        if (validLine.matcher(line).find()){

            // Build AbsoluteDate
            String[] columns = line.split("\\s+");

            // Read orbital elements
            double var1 = Double.parseDouble(columns[4]);
            double var2 = Double.parseDouble(columns[5]);
            double var3 = FastMath.toRadians(Double.parseDouble(columns[6]));
            double var4 = FastMath.toRadians(Double.parseDouble(columns[7]));
            double var5 = FastMath.toRadians(Double.parseDouble(columns[8]));
            double var6 = FastMath.toRadians(Double.parseDouble(columns[9]));

            double variables[] = {var1, var2, var3, var4, var5, var6};

            try {
                manPlan.add(variables);
            } catch (OrekitException e1) {
                logger.error("Orekit error, failed to build SpacecraftState", e);
            }
        }

    }

    reader.close();

    return manPlan;
}

}

Thank you very much!

oz380

oz380
  • 57
  • 3
  • 9

3 Answers3

2

Suppose this is vector:

class MyVector {
    Double aDouble;
    Double bDouble;
}

You can create a list as this:

ArrayList<MyVector> myVectors = new ArrayList<>();
xingbin
  • 27,410
  • 9
  • 53
  • 103
1

Based on your code, you are creating an array with 6 values (that you called vector) and you want to insert those arrays in a list. You can create a list of double array.

ArrayList<double[]> manPlan = new ArrayList<>();

A small explanation as why List<double[]> is valid but List<double> is not.

When you declare a variable that use generic type (like List<>), you need to provide a type. That type can't be a primitive (double, int, boolean, ...).

It explains why List<double> is not accepted you will find out that each primitive type have a class wrapper to fixed this problem, here you would use `List (note the upper case).

Now, since you want an array, using double[] as a type is valid since the type would an array, which is not a primitives type.

Thanks to zapl that corrects me

AxelH
  • 14,325
  • 2
  • 25
  • 55
  • primitive arrays are objects, so `List` is totally a thing. – zapl May 16 '18 at 11:26
  • Indeed @zapl, I didn't thought about this because I first thought about a `List` which isn't correct because of the primitive type, but the array solve that problem! Thanks for that. – AxelH May 16 '18 at 11:27
0

Ok I think you can use Java collections:

Vector -this solution will be threat safe link to doc:

The reason why we should use java collections: stackoverflow link

    Set<String> files = new HashSet<String>();
    files.add("1|2|3|4|5|6");
    files.add("7|8|9|10|11|12");

    Vector<Vector<Double>> vectors = new Vector<Vector<Double>>();
    for(String line : files) {
        Vector<Double> doubles = new Vector<Double>();
        String[] items = line.split("\\|");
        for(int index = 0; index < items.length; index++) {
            doubles.add(new Double(items[index]));
        }
        vectors.add(doubles);
    }
    System.out.println(vectors);

Output: [[7.0, 8.0, 9.0, 10.0, 11.0, 12.0], [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]]

Adrian Klimczak
  • 133
  • 1
  • 10