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
Import Java.util.random;public class Main {public static void main (string[] args) {int array[];array = new INT[10];p: for (i NT i = 0; I < 10;) {//Expression 3 is null int value = Make_random ();//Generate random number for (int m = 0; m < i; m++) {//To determine if there is duplicate if (value = = Array[m]) {// If there is a value continue p;//the next cycle of P block}}array[i] = value;//to the array assignment i++;//array subscript value + 1}arraymessage (array); Output randomly generated array max_and_min (array);//output maximum and its index}/** * production random number * @return */public static int make_random () {Random rnd = new Rando m ();//Instantiate an object int tmp = Rnd.nextint (10);//generates a random number within 100 return tmp;//returns a random number}/** * Output array information * @param array */public static void Arraymessage (int array[]) {System.out.print ("generated random array: \n["); for (int i = 0; I < 9; i++) {System.out.print (Array[i] + ",");} System.out.println (Array[9] + "]");} /** * The maximum value of the output array and its index * @param array */public static void max_and_min (int array[]) {int Max = array[9];//defines the initial value of the maximum to be any element of the array, so You can reduce the number of comparisons int mark = 9;//defines the index of the maximum value as the index of the element, for (int i = 0; i <; i++) {//Iterate over all if (Array[i] >= max) {//If a metaThe element is greater than or equal to the maximum max = array[i];//Sets the maximum value of the elements Mark = i;//assigns the element's index to Mark}}system.out.print ("where the maximum value of the array element is:" + Max + ", indexed as:" + mark);} }
Console output:
Java-Array Basic applications