1

I need to add a number of matrces to an arraylist or some sort of collection in order to recall them at a later stage

I have tried the arraylist and arraycopy

List<Double> al = new ArrayList<>();
double [][] k = new double [d.length][d[0].length];

System.arraycopy (d,0,k,0,d.length);

for (int i1 =0; i1 < d.length; i1++)
    k[i1] = k.add(D[i1]);
al.add(k[i1]);
for (Integer x : k)
    System.out.print(x + " ");
print2D(k);

I need an array of say 4x4 matrices If I do say al.add(d); I get error: cannot find suitable method to add double even when al.add(Matrix)

0

2 Answers 2

1

You forgot the array as type for your generics. What you want is List<double[][]> and not List<Double>. Then you will be able to add your matrices to the list.

The addition is simple, just

list.add(matrix);
// or in your case:
al.add(k);

no need to copy anything around or access the individual entries in the matrices.

You can retrieve matrices via get for example:

double[][] firstMatrix = list.get(0);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Butiri & Zabuza I have corrected the List<Double [][]> but still get this error: no suitable method found for add(double[][]) al.add(k); and also this error: incompatible types: Double[][] cannot be converted to double[][], please explain double[][] firstMatrix = al.get(0);
As I said, it is double[][] and not Double[][].
0

The list must be a list of matrices, and you are declaring a list of Doubles. Try something like this:

List<double[][]> list = new ArrayList<>();
    double[][] matrix = {{1D,1D},{2D,2D}};
    list.add(matrix);

Hope that helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.