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

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

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.