Quiero dividir la matriz en una matriz múltiple con un tamaño más pequeño. A continuación se muestra el código que estoy usando para obtener los valores de la matriz.
itemtbldata = itemtbldata.substring(1, itemtbldata.length()-1);
System.out.println("itemtbldata "+itemtbldata);
String[] itemcell = itemtbldata.split(",");
System.out.println(itemcell.length);
Por ejemplo, itemcell tiene valores inferiores
itemcell = [renu,1252,ed,dev,kalam,8562,ed,dev]
Ahora quiero obtener lo siguiente. Por favor alguien ayuda
arr1 = [renu,1252,ed,dev]
arr2 = [kalam,8562,ed,dev]
-2
kavi
23 mar. 2017 a las 12:26
2 respuestas
La mejor respuesta
Puede usar System.arraycopy
de esta manera (vea la demostración de trabajo http://rextester.com/RZA81274)
int finalRowSize = 4; //Set here the chunks size
String[] itemcell = {"renu","1252","ed","dev","kalam","8562","ed","dev"};
String[][] result = monoToBidi(itemcell,itemcell.length / finalRowSize, finalRowSize);
public static String[][] monoToBidi(final String[] array, final int rows, final int cols){
String[][] bidi = new String[rows][cols];
for ( int i = 0; i < rows; i++ )
System.arraycopy(array, (i*cols), bidi[i], 0, cols);
return bidi;
}
1
Theodore K.
23 mar. 2017 a las 13:30
Aquí hay un enfoque Java escrito manualmente:
public static void main(String[] args)
{
int max = 4;
String[] data = { "renu", "1252", "ed", "dev", "kalam", "8562", "ed", "dev", "hi", };
// use ceiling so 9 / 4 = 3. (round shouldn't be necessary, but just for safety, here it is)
int numOfArrays = (int)Math.round(Math.ceil(data.length / (float)max));
String[][] data2 = new String[numOfArrays][];
// loop over all instances except the last one (as that one might be smaller than the max
for (int i = 0; i < numOfArrays-1; i++)
{
data2[i] = new String[max];
System.arraycopy(data, i*max, data2[i], 0, max);
}
if (numOfArrays > 0)
{
int index = numOfArrays-1;
int length = data.length % max;
data2[index] = new String[length];
System.arraycopy(data, index*max, data2[index], 0, length);
}
System.out.println(Arrays.deepToString(data2));
}
0
Wietlol
23 mar. 2017 a las 09:49
Preguntas relacionadas
Nuevas preguntas
java
Java es un lenguaje de programación de alto nivel. Utilice esta etiqueta cuando tenga problemas para usar o comprender el idioma en sí. Esta etiqueta rara vez se usa sola y se usa con mayor frecuencia junto con [spring], [spring-boot], [jakarta-ee], [android], [javafx], [hadoop], [gradle] y [maven].