Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - There is an array in java, and its elements are divided into several arrays one by one in order. The number of arrays is a variable parameter and is written as a method call.
There is an array in java, and its elements are divided into several arrays one by one in order. The number of arrays is a variable parameter and is written as a method call.
If you use a plastic array of java, there may be a problem, that is, the number passed in cannot be divisible by the length of the array, while a two-dimensional array requires that each element in the array has the same length, so you can only discard the redundant elements.

According to the following example, the number of 1 1 is transmitted for the first time, then three arrays are printed, and then four arrays can be printed, and the number is 12, which is expected to be adopted.

Packaging test;

Import java.util.arrays;

Public class splitArr {

public static int[][]split(int[]arr,int n){

int m = arr.length/n; //Number of arrays returned

int[][]result = new int[m][n];

If (array length ==0||n==0){

Return the result;

} Otherwise {

for(int I = 0; I & ltm;; i++){

for(int j = 0; j & ltn; j++){

result[I][j]= arr[n * I+j];

}

}

}

Return the result;

}

Public static void main(String [] args){

int[] arr = new int []{ 1,2,3,4,5,6,7,8,9, 10, 1 1 };

int n = 3;

int[][]result = split arr . split(arr,n);

for(int I = 0; I < Result. Length; i++){

system . out . println(arrays . tostring(result[I]));

}

}

}

verify the result