Generates an array of 1 length 10 elements that store an int type, randomly assigning a value to an array element and its value must not be duplicated to print the value and index of the largest array element in it.
Main file
ImportJava.util.Random; Public classMain { Public Static voidMain (string[] args) {intarray[]; Array=New int[10]; P: for(inti = 0; I < 10;) {//expression 3 is empty intValue = Make_random ();//Generate random numbers for(intm = 0; M < i; m++) {//Judging if there are duplicates if(value = = Array[m]) {//If there are heavy values ContinueP//The next cycle of the P-block}} Array[i]= value;//Assigning a value to an arrayi++;//array Subscript value + 1} arraymessage (array); //output a randomly generated arrayMax_and_min (array);//output maximum value and its index } /*** Production of random numbers *@return */ Public Static intmake_random () {Random Rnd=NewRandom ();//instantiate an object intTMP = Rnd.nextint (10);//generate a random number within 100 returntmp//return random number } /*** Output Array information *@paramArray*/ Public Static voidArraymessage (intarray[]) {System.out.print ("The generated random array is: \n["); for(inti = 0; I < 9; i++) {System.out.print (Array[i]+ ","); } System.out.println (array[9] + "]"); } /*** Maximum value of output array and its index *@paramArray*/ Public Static voidMax_and_min (intarray[]) { intmax = array[9];//defines the initial value of the maximum to be any element of the array, which reduces the number of comparisons intMark = 9;//The index that defines the maximum value is the index of the element; for(inti = 0; I < 10; i++) {//iterates through the array of all if(Array[i] >= max) {//If an element is greater than or equal to the maximum valuemax = Array[i];//set the element to the maximum valueMark = i;//assigns the index of the element to mark}} System.out.print ("Where the maximum value of an array element is:" + Max + ", the index is:" +mark); }}
Java-Array Basic applications