<>1 array

<>1.1 concept

array Array, The logo is [ ] , A collection used to store multiple data of the same type
You want to get the values of the elements in the array , You can use the footmark ( subscript ) To get
Array subscript is from 0 The beginning , The maximum value of the subscript is the length minus the length of the array 1

<>1.2 Create array

There are two ways to create arrays: dynamic initialization and static initialization

* dynamic initialization
int[] a = new int[5];
* initiate static
int[] b = new int[]{1,2,3,4,5};
int[] c = {1,2,3,4,5};
<>1.3 Analysis of creating array process

Program create array int[] a = new int[5]; What happened at the time ?

* Open up continuous space in memory , Used to store data , The length is 5
* Complete the initialization process for the array , Give each element a default value ,int The default value of the type is 0
* When the array is initialized, a unique address value is assigned
* Give a unique address value to a variable of reference type a To save
The array name is a variable of reference type , It holds the address of the array , Is not data in an array

<>1.4 The length of the array

The length of the array is length Property , Once the array is created , The length cannot be changed
The length of the array is allowed to be 0

<>1.5 practice : Saving data to an array hello

create package : cn.tedu.array
Create class : TestCreateArray.java
package cn.tedu.array; import java.util.Arrays; /** This class is used to practice the creation and basic use of array */ public
class TestCreateArray { public static void main(String[] args) { //1. Create array
//1.1 Static creation -- You already know the specific data in the array char[] c1 = {'h','e','l','l','o'}; char[] c2 = new char
[] {'h','e','l','l','o'}; //1.2 Dynamic creation -- Know the length of the array , Later dynamic storage data char[] c3 = new char[5];
// Open up a continuous space in memory , For storage 5 Characters // Subscript by array , Operates on each element in the array , Assigning values to array elements // Array subscript from 0 start , The maximum subscript of an array is the length of the array -1
// Quick copy down :Ctrl+Alt+ Down key c3[0] = 'h';// To the second of an array 1 Element assignments c3[1] = 'e';// To the second of an array 2 Element assignments c3[2]
= 'l';// To the second of an array 3 Element assignments c3[3] = 'l';// To the second of an array 4 Element assignments c3[4] = 'o';// To the second of an array 5 Element assignments
//2. Print and display the created array System.out.println(c1); System.out.println(c2); System.out.
println(c3); //3. establish String Array of types s, Deposit "a","b","c" /** * char The bottom layer of the type is processed , You can print the contents of the array directly *
except char All arrays other than type want to see the specific elements in the array * Tool classes that require arrays Arrays Of toString( Array name ) method *
be careful :Arrays Guide package is needed when using * */ String[] s = {"a","b","c"}; System.out.println(Arrays.
toString(s)); //4. View the length of the array -- The number of elements in the array System.out.println(c2.length); System.out.
println(s.length); } }
<>1.6 Traversal of arrays

ergodic : from a to z , Access each position of the array in turn , Get the elements of each location . The form is as follows :
We manipulate arrays by subscribing to them , therefore for Loop variable operation is also array subscript

start : Start subscribing 0 end : End subscript length-1 How to change :++
for( From subscript to 0 Start from where you are ; The value of subscript <= The length of the array -1 ; subscript ++){
Circulatory body ;
}`

<>1.7 practice -1: Output the number of days per month

Create class : TestArrayExec.java
package cn.tedu.array; /** This class is used to practice array traversal * demand : Outputs the number of days in each month of the year * */ public class
TestArrayExec { public static void main(String[] args) { m ();
// This method is used to output the number of days per month , Quick repair shortcut Ctrl+1 } // Printing requirements : for example 1 Monthly 31 day public static void m () {
//1. Create an array to store 12 Days of a month int[] a = {31,28,31,30,31,30,30,31,30,31,30,31};
//2. Print the days of each month to the console // Traversal of arrays -- By means of circulation -- The operation is the subscript of the array // Cycle starts :a[0]-- The first element in the array
// End of cycle :a[a.length-1]-- The last element in the array // How to change :++ //for(int i = 0 ; i <=a.length-1 ;
i++) { for(int i = 0 ; i <a.length ; i++) { //a[i]-- According to subscript i To get the array a The value of the element at the corresponding position in System
.out.println((i+1)+" Monthly :"+a[i]+" day "); } } }
<>1.8 practice -2: Traversing arrays , Deposit 1 reach 10
// demand : Traversing arrays , Deposit 1-10 public static void m2() { //1. Create array -- Dynamic creation int[] a = new int[10]
; //2. Traversing arrays , Assign values to each position in turn // Cycle starts :a[0]-- The first element in the array // End of cycle :a[a.length-1]-- The last element in the array
// How to change :++ for(int i = 0 ; i <= a.length-1 ; i++) { //a[i]-- According to subscript i To get the array a The value of the element at the corresponding position in
a[i] = i+1; }//for End of cycle //3. Deposit in 10 After three data , Printing a array System.out.println(a);//[I@15db9742
// except char Array of types , Other types of arrays need to use tool classes to view specific contents Arrays
//Arrays.toString( The name of the array to print ) To see the specific contents of the array System.out.println(Arrays.toString(a));
//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] }
<>1.9 practice -3: Create random array

demand : Create array , The contents of the array are 100 Random numbers within
public static void m3() { //1. Create array -- dynamic int[] a = new int[10];
//2. Traversing arrays , Assign a value to each element in the array for(int i = 0 ; i <a.length ; i++) {
//100 It's custom data , Indicates that the range of generated random integers is [0,100) a[i] = new Random().nextInt(100); //a[i] =
new Random().nextInt(100)+1;// Both before and after the value range +1-->[1,101) } //3. Use the array tool class to view the elements in the array System.
out.println(Arrays.toString(a)); }
<>2 Array tool class Arrays

<>2.1 Arrays.toString( array )
Put the data in the array , Concatenate a comma into a string [ value 1, value 2]
<>2.2 Arrays.sort( array )
Sort arrays , For arrays of basic types, the optimized quick sort algorithm is used , efficient Array of reference types , It uses the optimized merge sort algorithm
<>2.3 practice : practice Arrays.sort( array )

Create class : TestArraysSort.java
package cn.tedu.array; import java.util.Arrays; /** This class is used to test the tool class of array Arrays*/ public
class TestArraysSort { public static void main(String[] args) { //1. Create unordered array int[]
a= {21,96,75,23,25}; // The sorting algorithm used in the bottom layer is the optimized quick sorting algorithm Arrays.sort(a);// Sorting unordered arrays directly
System.out.println(Arrays.toString(a)); } }
<>2.4 Arrays.copyOf( array , New length )

Assign an array to a new array of specified length
The length of the new array greater than Original array , It's like copying , And add location
The length of the new array less than Original array , It is equivalent to intercepting part of the data

<>2.5 practice : practice Arrays. copyOf( array , New length )

Create class : TestArraysCopyOf.java
package cn.tedu.array; import java.util.Arrays; /** This class is used to test the tool class of array Arrays*/ public
class TestArraysCopyOf { public static void main(String[] args) { //1. Create unordered array int
[] from = {1,2,3,4,5};// Once the array is created , The length cannot be changed //2.1 Ordinary copy of array int[] to = Arrays.copyOf(from
, 5);// Which array to copy , Length of copy System.out.println(Arrays.toString(to)); //2.2 Expansion of array
// First, create a new array of corresponding length , Then copy the data of the original array , The default value is insufficient , here int[] The default value for is 0 int[] to2 = Arrays.copyOf(from,
10);// Which array to copy , Length of copy System.out.println(Arrays.toString(to2)); //2.3 Reduction of array
// First, create a new array of corresponding length , Then copy the data in the original array according to the number of parameters , Similar interception int[] to3 = Arrays.copyOf(from, 3);
// Which array to copy , Length of copy System.out.println(Arrays.toString(to3)); //2.4 Specifies that the elements in the array are truncated first and last int
[] to4 = Arrays.copyOfRange(from, 2, 4); System.out.println(Arrays.toString(to4)
); } }
<>3 Extended two dimensional array

<>3.1 concept

An array that holds an array , That is to say, the data stored in the array is still in the form of array data

<>3.2 Create a two-dimensional array

int[][] a = {{3,5},{7,9},{1,2}};
– Create external array , The length is 3
– Create an internal array for the location of each external array , The length of each internal array is 2
– Initialize each internal array with data
– Two dimensional arrays generate unique address values
– Give the address value to the reference type variable a To save

<>3.3 Traversing two dimensional arrays

for (int i = 0; i < a.length; i++) {// Traversing external arrays
for (int j = 0; j < a[i].length; j++) {// Traversing internal arrays
System.out.println(a[i][j]);// Print the values of each element in a two-dimensional array in turn
}
}

<>3.4 Print data in 2D array
package cn.tedu.array; import java.util.Arrays; /** This class is used to print data in two-dimensional array */ public
class TestArrays2 { public static void main(String[] args) { //1. Create a two-dimensional array // 3 5
//a[0][0] a[0][1] // 7 9 //[1][0] a[1][1] // 1 2 3 //a[2][0] a[2][1] a[2][2] int
[][] a = {{3,5},{7,9},{1,2,3}}; //2. Traversing two dimensional arrays for (int i = 0; i < a.length; i++) {
// System.out.println(Arrays.toString(a[i])); for (int j = 0; j < a[i].length; j
++) { //a[i][j]-- Specific elements are located according to the subscripts of external array and internal array System.out.print(a[i][j]); } System.
out.println(); } } }

Technology
©2019-2020 Toolsou All rights reserved,
Huawei 2021 session Hardware Engineer Logical post (FPGA) Super detailed surface !!!Vue-element-admin upgrade ui edition virtual machine VMware Download and install the most detailed tutorial !C++ Move constructor and copy constructor sound of dripping water java Backstage interview pygame Realize full screen mode and adjustable window size mysql Database setting character set configuration modification my.ini file (windows)30 What's the experience of being a junior programmer at the age of 20 C++ Multithreading programming ( Summary of common functions and parameters )python_ cherry tree