Python - ¿Cómo representar una matriz 2D en Java?

 
Vista:
Imágen de perfil de genio

¿Cómo representar una matriz 2D en Java?

Publicado por genio (1 intervención) el 23/09/2022 07:53:42
Te daré un ejemplo:

int rowLen = 10, colLen = 20;
Integer[][] matrix = new Integer[rowLen][colLen];
for(int i = 0; i < rowLen; i++)
for(int j = 0; j < colLen; j++)
matrix[i][j] = 2*(i + j); // only an example of how to access it. you can do here whatever you want.
¿Está claro?


Debe usar Vector para matriz 2D. Es seguro para subprocesos .

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();
}
Si estos son sus índices de matriz

00 01

10 11

Puede obtener un valor de índice específico como este

Double r2c1=matrix.get(1).get(0); //2nd row 1st column
Echa un vistazo aVector


Si necesita un comportamiento seguro para subprocesos, use

Vector<Vector<Double>> matrix = new Vector<Vector<Double>>();
Si no necesita un comportamiento seguro para subprocesos, use

ArrayList<ArrayList<Double>> matrix = new ArrayList<ArrayList<Double>>();
Lee mas:http://www.yxjava.com/es/Java-2/1002048554.html
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Francisco Javier
Val: 249
Ha aumentado su posición en 29 puestos en Python (en relación al último mes)
Gráfica de Python

¿Cómo representar una matriz 2D en Java?

Publicado por Francisco Javier (313 intervenciones) el 23/09/2022 10:57:32
1
https://www.lawebdelprogramador.com/foros/Java/index1.html
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar