Package WZS. test2; import Java. util. arrays; // generate the specified array class public class test {public static void main (string [] ARGs) {int [] a1 = rang (10 ); int [] a2 = rang (2, 10); int [] a3 = rang (2, 10, 2); system. out. println ("A1:" + arrays. tostring (A1); system. out. println ("A2:" + arrays. tostring (A2); system. out. println ("A3:" + arrays. tostring (A3);}/*** 0 -- N * @ Param N * @ return */public static int [] rang (int n) {int [] result = new int [N]; for (INT I = 0; I <n; I ++) {result [I] = I;} return result ;} /*** start -- end * @ Param start * @ Param end * @ return */public static int [] rang (INT start, int end) {int size = end-start; int [] result = new int [size]; for (INT I = 0; I <size; I ++) {result [I] = start + I;} return result ;} /*** start -- end step: Step * @ Param start * @ Param end * @ Param step * @ return */public static int [] rang (INT start, int end, int step) {int size = (end-Start)/step; int [] result = new int [size]; for (INT I = 0; I <size; I ++) {result [I] = start + I * step;} return result ;}}
Output result:
a1:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]a2:[2, 3, 4, 5, 6, 7, 8, 9]a3:[2, 4, 6, 8]