Java Array Chapter Summary

Source: Internet
Author: User
Tags type null

Array (array of Yu Shunji and Wang Wei)

1. Array: A container that stores multiple elements of the same data type.

2, Characteristics: Each element has a number, starting from 0, The maximum number is the length-1. Number of professional names: index

3. Define the format

3.1): data type [] array name;

3.2): data type array name [];

Recommendation is a way, B method forget it. But to be able to read

4. Initialization of arrays

4.1) Dynamic Initialization

Give only the length, the system gives the default value

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

4.2) Static Initialization

Given the value, the system determines the length

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

Simplified version: int[] arr = {£ º};

5. Memory allocation in Java

Stack store local Variables

Heap store all new out of

Method Area (object-oriented part explained in Detail)

Local Method Area (system-dependent)

Register (CPU Usage)

Memory Graph:

650) this.width=650; "src=" https://s2.51cto.com/wyfs02/M01/8E/80/wKiom1jCTF6g0y1MAAI-TP6V50c400.png-wh_500x0-wm_ 3-wmp_4-s_3855849351.png "title=" 1.png "alt=" wkiom1jctf6g0y1maai-tp6v50c400.png-wh_50 "/>

Attention:

A: A local variable is defined in the method definition or on a method Declaration.

B: The difference between stack memory and heap memory

Stack: when the data is used, it Disappears.

Heap: every new one comes with an address.

Each variable has a default value

Byte,short,int,long 0

float,double 0.0

Char ' \u0000 '

Boolean false

Reference type NULL

After the data is used, it is recycled when the garbage collector is Idle.

6. Array Memory Graph

An array

650) this.width=650; "src=" https://s3.51cto.com/wyfs02/M02/8E/81/wKiom1jCTKWDRg2oAAA2dmYKjfs949.png-wh_500x0-wm_ 3-wmp_4-s_4183753482.png "title=" 2.png "alt=" wkiom1jctkwdrg2oaaa2dmykjfs949.png-wh_50 "/>

Array of two

650) this.width=650; "src=" https://s1.51cto.com/wyfs02/M01/8E/7F/wKioL1jCTM7Dtp9cAAIsjARafsc638.png-wh_500x0-wm_ 3-wmp_4-s_691766234.png "title=" 3.png "alt=" wkiol1jctm7dtp9caaisjarafsc638.png-wh_50 "/>

C: three arrays (two stack variables point to the same heap Memory)

650) this.width=650; "src=" https://s4.51cto.com/wyfs02/M02/8E/81/wKiom1jCTQ7xsTpFAAKDx1Mn_g4137.png-wh_500x0-wm_ 3-wmp_4-s_146179413.png "title=" 4.png "alt=" wkiom1jctq7xstpfaakdx1mn_g4137.png-wh_50 "/>

7. Array Traversal case:

7.1) Traversal:

mode 1:public static void PrintArray (int[] arr) {for (int x=0; x<arr.length; x + +) {System.out.println (arr[x]);}} Way 2:public static void PrintArray (int[] Arr) {System.out.print ("["); for (int x=0; x<arr.length; x + +) {if (× = = Arr.length-1) {System.out.println (arr[x]+ "]");} else {System.out.println (arr[x]+ ",");}}}

7.2) Maximum Value

Maximum value: publicstatic int getmax (int[] arr) {int max = arr[0];for (int x=1; x<arr.length; x + +) {if (arr[x] > max) {max = arr[x];}} Return max;}  min: public static int getmin (int[] arr) {int min = arr[0];for (int x=1; x<arr.length; x + +) {if (arr[x] < Min) {min = arr[x];}} Return min;}

7.3) reverse Order

mode 1:public static void reverse (int[] arr) {for (int x=0; x<arr.length/2; x + +) {int temp = arr[x];arr[x] = Arr[arr.leng th-1-x];arr[arr.length-1-x] = temp;}} mode 2:public static void reverse (int[] arr) {for (int start=0,end=arr.length-1; start<=end; start++,end--) {int temp = a rr[start];arr[start] = arr[end];arr[end] = temp;}}

7.4) Basic Search

Way 1:public static int getindex (int[] arr,int value) {for (int x=0; x<arr.length; x + +) {if (arr[x] = = Value) {return x;} }return-1;} mode 2:public static int getindex (int[] arr,int value) {int index = -1;for (int x=0; x<arr.length; x + +) {if (arr[x] = = Val Ue) {index = x;break;}} Return index;}

7.5) Check Table:

public static String getString (string[] strarray,int Index) {return strarray[index];}

7.6) Keyboard Entry score Array case:

Import java.util.scanner;class TestArr1 {public static void main (string[] Args) {Scanner s = new Scanner (system.in);d ouble [] scores = new Double[5];d ouble sum = 0;double avg = 0;double min = 100;double max = 0;for (int i = 0;i<scores.length; I++) {scores[i]=s.nextdouble ();} for (int i = 0;i<scores.length;i++) {sum + = scores[i];if (scores[i]<min) {min = scores[i];} If (scores[i]>max) {max = scores[i];}} AVG = sum/scores.length; System.out.println ("average score:" +avg+ ", lowest score:" +min+ ", highest score:" +max);}}

Two-dimensional arrays

2.1) the element is an array of one-dimensional arrays.

2.2) format:

A: data type [] array name = new data type [m][n];

B: data type [] array name = new data type [m][];

C: data type [] array name = new data type [][]{{...},{...},{...}};

D: data type [] array name = {{...},{...},{}};

2.3) Traversal case

Import java.util.scanner;class testarr {public static void main (String[]  Args)  {/*int[][] arr = {{1,2,3},{2,3,4},{3,4,5}};int[][] _arr = {};int[][]  arr1 = new int[3][2]; int[][] _arr1 = new int[][]{{1,2,3},{2,3,4 },{3,4,5}};int [][] arr2 = new int[3][];arr2[0] = new int[]{1,2,3}; Arr2[1] = new int[]{2,3,4};arr2[2] = new int[]{5,6,7};//int[][][] arr3  = new int[5][6][6];//traversal of a two-dimensional array  //a layer of loops is the traversal of all one-dimensional arrays in the two-dimensional for (int i = 0;i< Arr2.length;i++) {//two layers Loop through all elements in a one-dimensional array for (int j = 0;j<arr2[i].length;j++) {System.out.print (ARR2 I [j]+ "\ t");} System.out.println ();} *///defines a two-dimensional array of length 3 with 3 one-dimensional arrays of length 3//adds numbers to It,//1 2 3//4 5 6//7 8 9scanner s  = new scanner (system.in); int n = s.nextint (); int[][] arr =&nBsp;new int[n][n];int number = 1;for (int i = 0;i<arr.length;i++) {for (int  j = 0;j<arr[i].length;j++) {arr[i][j] = number; number++;}} For (int i = 0;i<arr.length;i++) {for (int j = 0;j<arr[i].length;j++) { System.out.print (arr[i][j]+ "\ t");} System.out.println ();}}}

2.4) Index Case:

Class Test24_2 {public static void main (string[] Args) {int [] arr = new Int[]{2,3,4,45,66,7,4};int MaxNumber = INTEGER.MI N_value;int Minnumber = Integer.max_value;int Maxindex = 0;int Minindex = 0;for (int i = 0;i<arr.length;i++) {if (ARR[I]&G T;maxnumber) {maxnumber = Arr[i];maxindex = i;} If (arr[i]<minnumber) {minnumber = Arr[i];minindex = i;}} System.out.println ("max:" +maxnumber+ "index is:" +maxindex+ ", min:" +minnumber ");}}


This article is from "never give up!" Ningzhiyuan "blog, declined reprint!

Java Array Chapter Summary

Related Article

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.