Java Learning Lesson 04

Source: Internet
Author: User

Array

* Definition: A data structure used to save multiple elements of the same type at once

One-dimensional arrays, multidimensional arrays.


* One-dimensional array:


Statement

Type [] Array variable name

int [] arr;

Or

int arr[];

Both forms are possible, and there is no difference, but it is recommended that we take the first approach.



* Initialize:


Dynamic initialization:

Define an array and assign values to the arrays to perform them separately.

int [] arr = new int[2];

Arr[0] = 1;

ARR[1] = 1;


* Static initialization:

We are initializing an array while we are defining an array.

int []arr = new int[]{1,2,3};

* NEW:

Used to initialize an object.


* Inside the array Note:

Once the array length is specified, it can never be changed.

Once specified, the array holds an element type that cannot be changed and can only hold elements of that type


* Length:

Arr.length; Indicates the length of the array arr[i].


* Access to array elements

Arr[index]: Index represents the indexes of the array elements, noting that the index is from 0 to length-1.

That is: arr[0] access to the first element


Index: Can be a constant, or it can be an expression.


The first one-dimensional array that holds 10 shaping elements, where the values are 1 to 10, each of the 10 numbers and the average of 10 numbers.

And the difference between the two neighbors, the final and.

Public class arraytestdemo{     public static void main ( String[] args) {        int []arr=new int[10];// Define an array of length 10         int sum=0;         int num=0;        double average=0;         //dynamic initialization of an array         for (int  i=0;i<arr.length;i++) {            arr[i]= (i+1);             //summation sum              sum+=arr[i];                  }          Average= (SUM*1.0/10);   &nbsP;      system.out.println ("Sum of 1-10 10 numbers:" +sum);          system.out.println ("The average of 1-10 10 numbers is:" +average);          sum=0;         for (int i=0;i<arr.length-1;i++ ) {             num= (arr[i+1]-arr[i]);              sum+=num;          }        system.out.println ("The difference of 22 adjacent numbers is:" +num);         system.out.println ("22 The difference of the number of neighbors and for:" +sum);      }}

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M00/A6/E8/wKioL1ndoxfROsv8AAAcS1T-4AM603.png-wh_500x0-wm_ 3-wmp_4-s_207111648.png "title=" U8GXL4RL) ~ (zw91%@e[3723.png "alt=" Wkiol1ndoxfrosv8aaacs1t-4am603.png-wh_50 "/>


* Two-dimensional array:


Statement

Type [] [] array name;

int [] arr;


* Initialize:

Dynamic initialization

int [][]arr = new int[3][];

* Static Initialization

int [][]arr = {{8,6},{3,7,4,8}};

Public class array1testdemo{    public static void main (String[]  args) {        int [][]arr=new int[3][3];         int num=1;        for (int  i=0;i<arr.length;i++) {           for (int  j=0;j<arr[i].length;j++) {                arr[i][j]=num;                num++;                System.out.print (arr[i][j]+ "  ");            }            system.out.println (" ");        &nBsp; }         system.out.println (" ");          //two-dimensional array transpose            for (int i=0;i<arr.length;i++) {            for (int j=0;j<arr[i].length;j++) {                system.out.print (arr[j][i]+ "  ");            }           system.out.println ( " ");          }}}

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M02/A6/EF/wKioL1nd36PThwlpAAAXwctG3Js140.png-wh_500x0-wm_ 3-wmp_4-s_2480017868.png "title=" l8s_n}rt]c[n87]5 (zod9uk.png "alt=" wkiol1nd36pthwlpaaaxwctg3js140.png-wh_50 "/ >

Java Learning Lesson 04

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.