1. Preparation of a simple procedure that requires the array length of 5, 10, 20 static assignment, the output value of the array in the console.
public class Blue { public static void main (String [] args) { // method TODO automatically generated stubs int [] = {ARR 10,20,30,40,50 }; for ( int I = 0; I <ARR .length; I ++ ) System.out.println (ARR [I]); } }
2. Write a simple procedure that requires the array length of 5, 10, 20 dynamically assigned, the output value of the array in the console.
public class Blue { public static void main (String [] args) { // method TODO automatically generated stubs int [] of arr1 = new new int [. 5 ]; int A =. 1 ; for ( int I = 0; I <of arr1. length; I ++ ) { of arr1 [I] = A * 10 ; System.out.println (of arr1 [I]); A ++ ; } } }
3. Write a simple procedure, the definition of an integer array, which elements are {23,45,22,33,56}, and the required array elements, the average value
public class Blue { public static void main (String [] args) { // method TODO automatically generated stubs int [] = {arr2 is 23,45,22,33,56 }; int SUM = 0, P = 0 ; for ( int I = 0; I <arr2.length; I ++ ) { SUM + = arr2 is [I]; P = SUM / arr2.length; } System.out.println ( "as the array element and" + sum + "" + " the average value of the array element is "+ P); } }
4. In a number of the largest array of eight integers (18,25,7,36,13,2,89,63) and find the index.
public class Blue { public static void main (String [] args) { // the TODO method of automatically generating stubs int [] = {A 18,25,7,36,13,2,89,63 }; int max = A [0 ]; for ( int I = 0; I <a.length; I ++ ) { IF (max < A [I]) max = A [I]; } System.out.println ( "maximum number of array" + max); for ( int J = 0; J <a.length; J ++ ) { IF (== max A [J]) System.out.println ( "array subscript is the maximum number of" + J); } } }
5. An array of storage elements in reverse order (knowledge point: traverse the array, the array element access)
public class Blue { public static void main (String [] args) { // method TODO automatically generated stubs int [] = {A 18,25,7,36,13,2,89,63 }; for ( int I . 1-a.length =; I> = 0; i-- ) System.out.println (A [I]); } }
6, there is already a good array row sequence. Now enter a number, required by the laws of the original insert it into the array. (Extra credit)
Import Classes in java.util *. ; public class Blue { public static void main (String [] args) { // method TODO automatically generated stubs int [] = {A 18,25,7,36,13,2,89, 63 is }; int [] B = new new int [a.length +. 1 ]; Scanner iNPUT = new new Scanner (the System.in); System.out.println ( "enter a number C" ); int C = input.nextInt ( ); Arrays.sort (A); for ( int I = 0; I <a.length; I ++ ) { if (a[i] > c) { b[i] = c; for (i = i + 1; i < b.length; i++) { b[i] = a[i - 1]; } } else { b[i] = a[i]; } } for (int i = 0; i<a.length+1; i++) { System.out.print(+b[i]); System.out.print(" "); } } }