Introduction to Java first season _1.6_ array

Source: Internet
Author: User

Since the choice of distance, then only the trials and hardships.

Array

The array is one of the important data structures for each programming language, of course, the implementation and processing of the different language arrays are not the same.
The array provided in the Java language is used to store the same type elements of a fixed size.

publicclass Demo1 {    publicstaticvoidmain(String[] args) {        // 定义一个数组,保存五名学生的成绩        int7893978463 };        // 输出数组中的第二个成绩        System.out.println("数组中的第2个成绩为:" + scores[1]);    }}
Basic use of arrays

1. Declaring arrays

Syntax: data type [] array name;

Or a data type array name [];

Where the array name can be any valid variable name, such as:

2. Allocate space

In short, it is the maximum number of elements that can be stored in the specified array

Syntax: array name = new data type [array length];

Where the array length is the number of elements that can be stored in an array, such as:

In other words, we can also combine the above two steps to allocate space for it while declaring an array, such as:

3. Assign Value

After allocating space, the data can be placed into the array, and the elements in the array are accessed by subscripts, such as storing student scores in the scores array

4. Working with data in arrays

We can manipulate and manipulate the array after the assignment, such as getting and outputting the values of the elements in the array

Another way to create an array directly in Java is to combine declaring an array, allocating space, and assigning values, such as

It is equivalent to:

Using loops to manipulate arrays in Java
 Public Static void Main(string[] args) {//define an array to save five students ' scores        int[] scores = { +, the, the, -, the};//for Cycle Printing         for(inti =0; i < scores.length; i++) {System.out.print (scores[i]+"  "); } System.out.println ();//foreach Printing        //foreach is a special simplified version of the For statement, which is simpler and easier to iterate through arrays and collections.         //for (element type variable: traversing object) {        //Execute the Code        //}         for(intI:scores) {System.out.print (i+"  "); }    }

Execution results
78 93 97) 84 63
78 93 97) 84 63

Programming exercises
 PackageCOM.ZHB; Public  class Demo1 {     Public Static void Main(string[] args) {int[] Nums =New int[] { A, at,4, About, -,148, -};intmax = nums[0];//Assume the maximum value is the first element in the array        intMin = nums[0];//Assumed minimum value is the first element in the array        Doublesum =0;//Cumulative value        DoubleAVG =0;//Average         for(inti =0; i < nums.length; i++) {//Iterate through the elements in the array            //If the current value is greater than Max, replace the value of Max            if(Nums[i]> Max)            {max = nums[i]; }//If the current value is less than min, replace min's value            if(nums[i]< min)            {min = nums[i]; }//Cumulative sumSum+=nums[i]; }//AveragingAVG = sum/nums.length; System.out.println ("The maximum value in the array:"+ max); System.out.println ("The minimum value in the array:"+ min); System.out.println ("The average in the array:"+ avg); }}

Output results
Maximum value in array: 148
Minimum value in array: 4
Average of the array: 49.0

Using the Arrays class to manipulate arrays in Java
The

Arrays class is a tool class provided in Java, in the Java.util package. The class contains methods for manipulating arrays directly, such as sorting, searching, and so on.

package  com.zhb;< Span class= "Hljs-keyword" >import  java.util.Arrays; public  class  demo1  { Span class= "Hljs-keyword" >public  static  void         main  (string[] args) {//define an array of strings  String[] Hobbys = { "sports" ,  "game" , " "Movie" };        //uses the sort () method of the arrays class to sort the array             Arrays.sort (Hobbys);        //Use the ToString () method of the arrays class to convert the array to a string and output the     System.out.println (arrays.tostring (Hobbys)); }}

Execution results
[Game, movie, sports]

Two-dimensional arrays in Java

The so-called two-dimensional array, which can be simply understood as a "special" one-dimensional array, each of its array space holds a one-dimensional array.

1. Declaring arrays and allocating space

Or

Such as:

2. Assign Value

The assignment of a two-dimensional array, similar to a one-dimensional array, can be assigned individually by subscript, noting that the index starts at 0

Such as:

3. Working with arrays

A two-dimensional array accesses and outputs the same array of dimensions, just one more subscript. In the loop output, you need to embed a loop inside, even if you are using a double loop to output each element in a two-dimensional array. Such as:

Operation Result:

What you need to know: When you define a two-dimensional array, you can specify only the number of rows, and then specify the number of columns for each row, respectively. If the number of columns per row is different, an irregular two-dimensional array is created, as follows:

The result of the operation is:

Bubble sort
 PackageCOM.ZHB; Public  class Demo1 {     Public Static void Main(string[] args) {//Bubble sort        int[] nums={1, -,2, About, at, $};inttmp for(inti =0; I < nums.length-1; i++) { for(intj =0; J < nums.length-i-1; J + +) {if(nums[j]>nums[j+1]) {tmp = Nums[j]; NUMS[J] = nums[j+1]; nums[j+1]= tmp; }            }        } for(intI:nums) {System.out.print (i+"  "); }    }}

Output results
1 2 13 23 46 56

Introduction to Java first season _1.6_ 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.