如何在Java中表示2D矩阵? [英] How to represent a 2D matrix in Java?
本文介绍了如何在Java中表示2D矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
哪种数据结构最适合?只是2D数组或TreeMap?还是有任何惊人的外部图书馆?
解决方案
您应该使用Vector为2D数组。 线程安全。
向量<向量&Double>> matrix = new Vector< Vector&Double>>(); (int i = 0; i <2; i ++)
{
矢量< Double> r = new Vector<>(); (int j = 0; j< 2; j ++)
{
r.add(Math.random());
}
matrix.add(r); (int i = 0; i< 2; i ++){
}
向量< Double> r = matrix.get(i); (int j = 0; j< 2; j ++){
System.out.print(r.get(j));
}
System.out.println();
}
如果这是您的矩阵索引
00 01
10 11
您可以获得这样的特定索引值
Double r2c1 = matrix.get(1).get(0); //第二行第一列
看看
矢量
I have to create in Java a 2D matrix (consisting of double values) as well as a 1D vector. It should be possible to access individual rows and columns as well as individual elements. Moreover, it should be thread-safe (threads writing at the same time). Perhaps later I need some matrix operations too.
Which data structure is best for this? Just a 2D array or a TreeMap? Or is there any amazing external library?
解决方案
You should use Vector for 2D array. It is 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 this is your matrix indexes
00 01
10 11
You can get specifix index value like this
Double r2c1=matrix.get(1).get(0); //2nd row 1st column
Have a look at Vector
这篇关于如何在Java中表示2D矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文