Reputation: 5
I'm trying to make a Java application that takes a number as input and creates various matrices.
What's the best way to make this?
I've made this, and then I've tried to make it by array of arrays.
public class main {
public static void main(String[] args) {
int max = 0;
Scanner scan = new Scanner(System.in);
System.out.println("Number of matrix?");
max = scan.nextInt();
int[] matrius = new int[max];
int[][] matriu = new int[2][2];
matrius[0] = matriu[2][2];
matrius[1] = matriu[2][2];
for(int i = 0; i < matrius.length; i++){
matrius[i] = i;
}
for(int i = 0; i < matrius.length; i++){
System.out.println(matrius[i]);
}
}
}
Thank you!
Upvotes: 0
Views: 88
Reputation: 900
Make a list to hold all your 2D matrices of 2 x 2. List has initial capacity entered by user i.e max.
Now loop and create 2D arrays and add them to the list.
public static void main(String[] args) {
int max = 0;
Scanner scan = new Scanner(System.in);
System.out.println("Number of matrix?");
max = scan.nextInt();
List<int[][]> allMatrices = new ArrayList<int[][]>(max);
for(int i = 0; i < max; i++){
int x[][]=new int [2][2];
allMatrices.add(x);
}
// To acces the 2 D arrays
foreach(int [][] x : allMatrices){
for(int i=0;i<x.length;i++)
{
for(int j=0;j<x[i].length ;j++)
{
// do some operation on x[i][j]
}
}
}
Upvotes: 1
Reputation: 10051
I think the best way is using OpenMapRealMatrix
of commons-math project.
This object can store a matrix with any number of rows and columns and it offers a lot of useful methods: copy, create new matrix, multiply, etc...
Take a look at this page:
Upvotes: 0