Java learning from scratch (array), java learning array from scratch

Source: Internet
Author: User

Java learning from scratch (array), java learning array from scratch
1. What is an array? It is a collection of related data. An array is actually a series of variables. Arrays can be divided into one-dimensional arrays, two-dimensional arrays, and multi-dimensional arrays. The Data Types of variables in the same array are the same. 2. Advantages of Arrays: 100 integer variables are not defined using Arrays: int i1; int i2 ;... Int i100 is defined by array: int I [100] multiple variables of the same type can be defined by array. 3. array declaration one-dimensional array can store tens of millions of data, and the data types are identical. To use an array of Java, two steps must be taken: (1) declaring the array and (2) Allocating the memory to the array. The syntax for these two steps is as follows:Declaration Form 1:Declare a one-dimensional array: data type array name[];Int num [];Allocate memory to the array: array name = new data type [length];Int num [] = new int [10];Statement Form 2:Declare a one-dimensional array: Data Type[]Array name; int [] num; int [] num = new int [10]; In the Declaration format of the array, "Data Type" is the data type that declares the array element, common types include integer, floating point, and complex. "Array name" is used to unify the names of elements of the same data type. Its naming rules are the same as those of variables. We recommend that you use meaningful names to name arrays.After the array is declared, the name of the array is actually saved 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, the number of elements to be stored in the declared array,"New" means that the command compiler opens up space based on the length in the brackets.4. Declare the array and assign values

Package com. pb. demo1; import java. util. extends; public class demo4 {public static void main (String [] args) {extends input = new evaluate (System. in); // declare a String array String [] cities = new String [5]; // assign a value to System. out. println ("enter the names of the five cities:"); for (int I = 0; I <cities. length; I ++) {cities [I] = input. next ();} // output System. out. println ("the input city name is:"); for (String city: cities) {System. out. println (city );}}}
V. Compare the size and sorting
Package com. pb. demo1; import java. util. average;/** enter the score of five students and obtain the highest score, lowest score, and average score */public class Demo5 {public static void main (String [] args) {// declare a Scanner variable similar to internal input = new external (System. in); // declare the integer array int [] scores = new int [5]; System. out. println ("Enter the score of five students: The score is an integer"); // assign an array value for (int I = 0; I <scores. length; I ++) {scores [I] = input. nextInt () ;}// total score float sum = 0; // minimum score int min = 0; // maximum score int max = 0; // average score float avg = 0; // assign the first value to min and max to compare min = scores [0]; max = scores [0]; for (int I = 0; I <scores. length; I ++) {// sum = sum + scores [I]; // if (scores [I] <min) {min = scores [I];} // if (scores [I]> max) {max = scores [I] ;}} System. out. println ("total score:" + sum); System. out. println ("min:" + min); System. out. println ("Maximum low:" + max); System. out. println ("average score:" + sum/scores. length );}}

 

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.