Java-Array Basic applications

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.