How to represent a 2D matrix in Java? -
i have create in java 2d matrix (consisting of double values) 1d vector. should possible access individual rows , columns individual elements. moreover, should thread-safe (threads writing @ same time). perhaps later need matrix operations too.
which data structure best this? 2d array or treemap? or there amazing external library?
you should use vector 2d array. threadsafe.
vector<vector<double>> matrix= new vector<vector<double>>(); for(int i=0;i<2;i++){ vector<double> r=new vector<>(); for(int j=0;j<2;j++){ r.add(math.random()); } matrix.add(r); } for(int i=0;i<2;i++){ vector<double> r=matrix.get(i); for(int j=0;j<2;j++){ system.out.print(r.get(j)); } system.out.println(); }
if matrix indexes
00 01
10 11
you can specifix index value this
double r2c1=matrix.get(1).get(0); //2nd row 1st column
have @ vector
Comments
Post a Comment