If you need to store a large amount of data, For example, if you need to read the number of numbers , then you need to define The three variables, obviously repeatedly write The code, it does not make much sense. How to solve this problem,theJava language provides an arrayof data structures, is a container can store the same data types of elements, can be 100 the number is stored in the array. This time the array will be a great help ~
1. The concept of arrays
A collection of the same type of data. The array is actually a container. There is a lot of data involved in the Operation , so the first thing to do is what to do . Not how to do it, but how to save it for later operations, then the array is a way to store the data, where we call it a container, and the contents of the container are the elements of the array. , arrays can fit any type of data, although any type of data can be loaded , but a well-defined array can only have one element, that is, once the array is defined, the data types stored inside are determined.
2. Benefits of Arrays
What is the difference between saving data and not saving data? The best thing about arrays is that you can automatically number the elements that are stored in them . Note that the number is from 0 start. Easy to manipulate the data.
For example, the student's number, the student number can be used to find the corresponding students.
3. Format of the array
Format one:
Element type [] array name = new element type [ element number or array length ];
Example:int[] arr = new int[5];
Arr[0] = 1;
ARR[1] = 2;
Format two:
Element type [] array name = new element type []{ element, element,...};
int[] arr = new int[]{3,5,1,7};
Int[] arr = {3,5,1,7};
Note: When allocating space to an array, you must specify the number of elements that the array can store to determine the size of the array. The size of the array cannot be modified after the array is created. You can use the length property to get the size of the array.
4. Declaring Array variables
In order to use an array, you must declare the array in your program and specify the element type of the array
= Left Half part:
First write the left to make clear that the element type is int , the container uses an array, then how to identify the array? . so, with a special symbol, [] in brackets to indicate. To use an array is to give the array a name , So here we give the array the name x. then follow the equals sign.
The code reflects:
int [] X
Note:int x[] is also a format for creating arrays. It is recommended to declare an array in the form of int [] X.
5. creating an array
= Right Half part:
To use a new keyword . called New . New is used to generate a container entity in memory, the data to be stored is needed space, the space to store a lot of data with the new operator to open,new int[3]; this 3 is the number of elements. The part on the right is the definition of a real array in memory that can store 3 elements.
New Int[3] did two things, first creating an array with new int[3] and then assigning the array reference to the array variable x.
int [] x=new int[3];
What type is x?
Any variable must have its own data type. Note that this x is not of type int. int represents the type of element inside the container. Then x is the array type.
An array is a separate data type. Data types are divided into 2 major factions, which are divided into basic data types and reference data types. The second big pie is the reference data type. So now you have access to one of the three types of reference data. That is, the array type [] brackets represent the array.
int[] arr = new int[5]; What's going on in memory?
Memory any program , run the time need to open up in memory space . int[] arr = new int[5]; What does this program do in memory? ? this involves the Java The space that virtual machines open up when executing programs , so Java How much space does it open up? Continue to learn about the memory structure of Java.
6. Initialization of arrays
Way a : Do not use operators New
int []arr = {1, 2, 3, 4, 5};
mode two: using operator new
int [] arr2 = new int[] {1, 2, 3, 4, 5};
int [] arr3=new int[3];
Arr3[0]=1;
arr3[1]=5;
arr3[2]=6;
If operator newis not used in array initialization. Note: The following notation is incorrect.
int [] arr;
arr={1,2,3,4,5};
At this point, the array must be initialized, the declaration, the creation, the initialization is placed in one statement, the separation will produce a syntax error.
So you can only write as follows:
Int[] arr={1,2,3,4,5};
7. Iterating the array
public static void Main (string[] args) {int[] x = {1, 2, 3};for (int y = 0; y < 3; y++) {System.out.println (x[y]);// System.out.println ("x[" +y+ "]=" +x[y]); Print effect x[0]=1;} So this is the first common operation of the array. Traversal}
There is an attribute in the array that gets the number of elements in the array , that is, the length of the array . Array name . Length
public static void Main (string[] args) {int[] x = {1, 2, 3};for (int y = 0; y < x.length; y++) {System.out.println (x[ Y]);//System.out.println ("x[" +y+ "]=" +x[y]); Print effect x[0]=1;} So this is the first common operation of the array. Traversal}
8. Common Exceptions to arrays
An array of corner labels out of bounds exception :, note: The array's Corner label starts at 0 .
public static void Main (string[] args) {int[] x = {1, 2, 3}; System.out.println (X[3]);//java.lang.arrayindexoutofboundsexception}
Two null pointer exceptions :
public static void Main (string[] args) {int[] x = {1, 2, 3};x = null; System.out.println (x[1]);//Java.lang.NullPointerException}
Array :
When to use arrays : When the elements are more convenient to manipulate these arrays , they are first stored temporarily, and the container used is the array.
Features :
The array length is fixed.
9. Memory Analysis of Arrays
Case study One:
Case Study II:
10. Common Operations for arrays
A: Case: An array to remove the maximum value
/* Define a function that gets the maximum value: 1, determine the result: return value type Int2, unknown content: To get the maximum value of which array is not determined, the array is not determined: 1, define a variable, record the larger elements of the array. 2. Iterate through the array so that each element of the array is compared to the variable. 3. When the variable encounters an element larger than it, let the variable record the value of the element, and when the loop ends, the maximum value produces the */public static int getmax (int[] arr) {//definition variable records a large value, initialized to any element in the array. int max = arr[0];for (int x=1; x<arr.length; x + +) {if (Arr[x]>max) max = arr[x];} return Max;}
Two: direct sort
Case two: Sort the array using direct sort:/* Select sort. Compare the elements of a corner with other elements. At the first end of the inner loop, the highest value appears in the position of the header. */public static void Selectsort (int[] arr) {for (int x=0; x<arr.length-1; x + +) {for (int y=x+1; y<arr.length; y++)// Why is the initialization value of y x+1? Because each comparison,//is compared with the element on the X-corner and the next element. {if (Arr[x]>arr[y]) {int temp = arr[x];arr[x] = arr[y];arr[y] = temp;}}} }
Three: Bubble sort
/* bubble sort. Comparison mode: Two adjacent elements are compared. Position displacement is performed if the condition is met. Principle: The inner loop ends once and the maximum value appears at the tail-angle mark position. */public static void Bubblesort (int[] arr) {for (int x=0; x<arr.length-1; x + +) {for (int y=0; y<arr.length-x-1; y++)// -X: The meta-minus for each participating comparison. -1: Avoid crossing the corner mark. {if (arr[y]>arr[y+1]) {int temp = Arr[y];arr[y] = arr[y+1];arr[y+1] = temp;}}}}
Four: Binary search (dichotomy)
/* In order to improve the search efficiency, you can use the binary lookup method, note: This lookup is only valid for an ordered array. This way also becomes the binary lookup method. */public static int Halfseach (int[] arr,int key) {int min,mid,max;min = 0;max = Arr.length-1;mid = (max+min)/2;while (arr[mi D]!=key) {if (Key>arr[mid]) min = mid + 1;else if (Key<arr[mid]) max = Mid-1;if (min>max) Return-1;mid = (max+min)/2; }return Mid;}
V: Array Rollover
/* Reverse is actually the position of the angle and tail of the elements of the location of the replacement, and then in the corner to increase the mark. The tail angle label is self-reducing. When the head angle Mark < tail angle is marked, the displacement action can be performed. */public static void Reversearray (int[] arr) {for (int start=0,end=arr.length-1; start<end; start++,end--) {swap (arr, start,end);}} Permutation of the positions of the elements of an array. public static void Swap (int[] Arr,int a,int b) {int temp = Arr[a];arr[a] = arr[b];arr[b] = temp;}
11. Two-dimensional array
Use of Arrays
Traverse: toString () returns the elements of an array as a string
Sort: sort () sorts the array in ascending order
Find: BinarySearch () finds the specified element in the specified array, returns the index of the element, if no return is found (- insertion point -1) Note: When using the Find function, Arrays must be sorted first.
Two-dimensional arrays :
Smoking:
No money, retail. 1 one variable
A little bit of money. A package of one-dimensional arrays root variables
It's rich. A two-dimensional array of two - dimensional arrays
Two-dimensional arrays: the essence is that storage is a one-dimensional array.
Array definition:
Array type [] array name = new array type [ number of one-dimensional array ] [ number of elements in each one-dimensional array ];
Question: why a.length = 3, A[0].length = 4?
Initialization of the array:
Static initialization :
int [] [] A = new int[][]{{12,34,45,89},{34,56,78,10},{1,3,6,4}};
Dynamic initialization:
Common operations for two-dimensional arrays :
1. Traversing a two-dimensional array
2. Summing the two-dimensional array
Class Demo3 {//defines a function function that traverses a two-dimensional array public static void printArr2 (int [] [] a) {//1. Open a two-dimensional array for (int i = 0; i < a . length; i++) {//2. To open a one-dimensional array for data for (int j = 0; J < A[i].length; J + +) {System.out.print (a[i][j]+ ","); }}}//define a function to calculate the sum of elements in a two-dimensional array and public static long getsum (int [] [] a) {//0. Defines a result variable long sums = 0L; 1. Unpack the two-dimensional array for (int i = 0; i < a.length; i++) {//2. To open a one-dimensional array to get data for (int j = 0; j < A[i].length; {SUM+=A[I][J]; }} return sum;} Counts the number of elements in a two-dimensional array public static int getdatacount (int [] [] a) {//0. Number of record elements int count = 0; 1. Unpack the two-dimensional array for (int i = 0; i < a.length; i++) {//2. To open a one-dimensional array to get data for (int j = 0; j < A[i].length; {count++; }} return count;} public static void Main (string[] args) {int [] [] A = new int[][]{{23,4,5},{2},{4,5,78,56,90}};p rintArr2 (a); System.out.println (); SYSTEM.OUT.PRINTLN ("Cumulative sum is:" +getsum (a)); SYSTEM.OUT.PRINTLN ("Statistical elements ofNumber: "+getdatacount (a)); System.out.println ("Hello world!");}}
An explanation of the Java array