1. Definition of arrays
A set of related variables
An array is actually a series of variables
The variables in the array must be of the same data type
Arrays can be divided into one-dimensional arrays, two-dimensional arrays, and multidimensional arrays
For example: numbers[100] to replace the direct declaration of 100 independent variables Number0,number1,....,number99
2. One-dimensional arrays
2.1 Declaration of an array
Two ways of declaring:
数据类型[] 数组名 //首选数据类型 数组名[] //效果相同,C语言的格式,不推荐
Example:
int[] myList //推荐int myList[] //不推荐
2.2 Initialization of an array 2.2.1 dynamic initialization
The array definition is separate from the operation of assigning space and assignment to the arrays
Array name = new array type [array size]
For example:
myList = new int[10];
2.2.2 Static initialization
Allocates space and assigns values to array elements while defining arrays
Array name = {element 1, element 2, Element 3, ...}
Array name = array type []{element 1, Element 2, Element 3, ...}
For example:
int[] a = {1,2,3,4};int[] a = new int[]{1,2,3,4};
2.2.3 Initialization of the error
Note: You can only customize data with curly braces on an array declaration, otherwise you can assign each subscript element individually
int[] month_days = new int[12];month_days[0] = 31;month_days[1] = 28; ……
2.3 One-dimensional array exercises and practices
1. What does an array name mean?
2, arr[0] What does it mean?
3, how to get the array length?
4, how to use the array subscript to take the value?
5. How do I use array traversal to fetch values for & foreach?
6. How to sum all the elements of an array?
7, how to find the maximum value of the array, the minimum value?
8. What is the difference between a shallow copy and a deep copy of an array?
9, Exercise: A class has 10 students of the math score needs to record, to calculate the total class results, the average score, the highest score, the lowest score, passing numbers and other data.
int[] studentScores = { 55, 66, 77, 88, 35, 89, 90, 52, 22, 76 }; int studentScores[] = { 55, 66, 77, 88, 35, 89, 90, 52, 22, 76 };
3. Two-bit array
3.1 Declaration and initialization of the 3.1.1 Declaration
int[][] a; //推荐int a[][]; //不推荐
3.1.2 Initialization
Dynamic initialization
数据类型[][] 数组名称 = new 数据类型 [行数][列数] ;
For example:
int [][] num = new int [3][3]; //定义了三行三列的二维数组 num[0][0] = 1; //给第一行第一个元素赋值 num[0][1] = 2; //给第一行第二个元素赋值 num[0][2] = 3; //给第一行第三个元素赋值 num[1][0] = 4; //给第二行第一个元素赋值 num[1][1] = 5; //给第二行第二个元素赋值 num[1][2] = 6; //给第二行第三个元素赋值 num[2][0] = 7; //给第三行第一个元素赋值 num[2][1] = 8; //给第三行第二个元素赋值
Static initialization
数据类型 [][] 数组名称 = {{第一行元素},{第二行元素},...} ;
For example:
int[][] num = {{1,2,3},{4,5,6},{7,8,9}};int[][] num = new int[][]{{1,2,3},{4,5,6},{7,8,9}};
A two-dimensional array is actually a matrix
3.2 Two-dimensional array exercises
Case: A school Kindergarten has 3 classes, each class has 5 people, calculating the average age of each class? Calculate the average age of the whole garden? Calculate the total number of people?
int students[][] = { { 3, 6, 4, 5, 4 }, { 2, 3, 5, 3, 2 }, { 3, 4, 6, 2, 4 } }; int[][] students = { { 3, 6, 4, 5, 4 }, { 2, 3, 5, 3, 2 }, { 3, 4, 6, 2, 4 } };
4. Sorting algorithm 4.1 bubble sort (must be mastered)
4. Array and sorting algorithm 1