Java starts from zero Nine (array)

Source: Internet
Author: User

First, what is an array array is a set of related data, an array is actually a series of variables, arrays can be divided into one-dimensional arrays, two-dimensional arrays, multidimensional arrays. Variable data types in the same array two, the advantages of arrays do not use an array to define 100 integer variables: int i1;int i2;...int i100 using an array definition: int i[100] Multiple variables of the same type can use an array to complete the three,Array DeclarationA one-dimensional array can hold thousands of data, and the type of the data is exactly the same. To use an array of Java, you must go through two steps: (1) declare an array, (2) Allocate memory to the array. The syntax for these two steps is as follows: declaration Form one:Declaring a one-dimensional array: Data type array name []; int num []; Allocate memory to array: array name = new data type [length]; int num [] = new INT[10]; declaration Form two:Declaring one-dimensional arrays: data types []Array name; int [] num;int [] num = new int [10]; In the declaration format of an array, the data type is the data type that declares the array element, and the common types are integer, float, and character. "Array Name" is the name of the element used to unify this set of identical data types, with the same naming rules and variables, it is recommended to use a meaningful name for the array name after the array declaration actually holds the name of this array in the stack memory, the next step is to configure the memory required for the array in the heap memory, where "length" tells the compiler how many elements are to be stored in the declared array . "New", however, is the command compiler to open space based on the length in parentheses. Iv. declaring an array and assigning values
 PackageCom.pb.demo1;ImportJava.util.Scanner; Public classDemo4 { Public Static voidMain (string[] args) {Scanner input=NewScanner (system.in); //declaring an array of stringsString [] cities=NewString [5]; //Assign ValueSystem.out.println ("Please enter the name of 5 cities:");  for(inti = 0; i < cities.length; i++) {Cities[i]=Input.next (); }                //OutputSystem.out.println ("The name of the city entered is:");  for(String city:cities) {System.out.println (city); }            }}
V. Compare size and sort
 PackageCom.pb.demo1;ImportJava.util.Scanner;/** Enter 5 students ' scores and find the highest score, the lowest score, the average score*/ Public classDemo5 { Public Static voidMain (string[] args) {//declare a variable that is similar to a scannerScanner input =NewScanner (system.in); //declaring a shaped array        int[] scores=New int[5]; System.out.println ("Please input 5 students ' scores: The result is an integer"); //assigning values to arrays         for(inti = 0; i < scores.length; i++) {Scores[i]=Input.nextint (); }        // Total        floatSum=0; //Minimum score        intMin=0; //Highest score        intMax=0; //Average score        floatAvg=0; //assign the first value to Min and max for comparisonMin=scores[0]; Max=scores[0];  for(inti = 0; i < scores.length; i++) {          // Totalsum=sum+Scores[i]; //Minimum score            if(Scores[i] <min) {min=Scores[i]; }            //Highest score            if(scores[i]>max) {Max=Scores[i]; }} System.out.println ("Total:" +sum); System.out.println ("Lowest Score:" +min); System.out.println ("Highest Low:" +max); System.out.println ("Average score:" +sum/scores.length); }}

Java starts from zero Nine (array)

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.