Article directory
Introduction to Arrays
Arrays can store multiple data of the same type. An array is also a data type and is a reference type.
Use of arrays
Usage 1-definition of dynamically initialized array
Data type array name [] = new data type [size]
int a[] = new int[5];//创建了一个数组,名字a,存放5个int
Explanation: This is one way to define an array.
import java.util.Scanner;
public class Array02 {
//编写一个main方法
public static void main(String[] args) {
//演示 数据类型 数组名[]=new 数据类型[大小]
//循环输入5个成绩,保存到double数组,并输出
//步骤
//1. 创建一个 double 数组,大小 5
//(1) 第一种动态分配方式
//double scores[] = new double[5];
//(2) 第2种动态分配方式, 先声明数组,再 new 分配空间
double scores[] ; //声明数组, 这时 scores 是 null
scores = new double[5]; // 分配内存空间,可以存放数据
//2. 循环输入
// scores.length 表示数组的大小/长度
//
Scanner myScanner = new Scanner(System.in);
for( int i = 0; i < scores.length; i++) {
System.out.println("请输入第"+ (i+1) +"个元素的值");
scores[i] = myScanner.nextDouble();
}
//输出,遍历数组
System.out.println("==数组的元素/值的情况如下:===");
for( int i = 0; i < scores.length; i++) {
System.out.println("第"+ (i+1) +"个元素的值=" + scores[i]);
}
}
}
Usage 2 - dynamic initialization
1. Declare the array first
Syntax: data type array name []; also data type [] array name;
int a[]; or int[] a;
2. Create an array
Syntax: array name = new data type [size];
a=new int[10];
Usage 3 - static initialization
Initialize the array
grammar:
Data type array name[] = {element value, element value...}
int a[]={2,5,6,7,8,89,90,34,56},
Array Usage Notes and Details
-
The elements in the array can be of any data type, including basic types and reference types, but they cannot be mixed.
-
After the array is created, if there is no assignment, the default value is
int 0
short 0
byte 0
long 0
float 0.0
double 0.0
char \u0000
boolean false
String null
- Arrays are reference types, and array data is objects
Array application case
Create an array of 26 elements of type char, place 'A'-'Z' respectively. Use a for loop to iterate over all elements and print them. Tip: char type data operation 'A'+2 -> 'C'
public class ArrayExercise01 {
//编写一个main方法
public static void main(String[] args) {
/*
创建一个char类型的26个元素的数组,分别 放置'A'-'Z'。
使用for循环访问所有元素并打印出来。
提示:char类型数据运算 'A'+1 -> 'B'
思路分析
1. 定义一个 数组 char[] chars = new char[26]
2. 因为 'A' + 1 = 'B' 类推,所以老师使用for来赋值
3. 使用for循环访问所有元素
*/
char[] chars = new char[26];
for( int i = 0; i < chars.length; i++) {
//循环26次
//chars 是 char[]
//chars[i] 是 char
chars[i] = (char)('A' + i); //'A' + i 是int , 需要强制转换
}
//循环输出
System.out.println("===chars数组===");
for( int i = 0; i < chars.length; i++) {
//循环26次
System.out.print(chars[i] + " ");
}
}
}
Array assignment mechanism
Arrays are passed by reference by default, and the assigned value is an address. (compared to: variables are often passed by value)
int[] arr1 = {
1,2,3};
int[] arr2 = arr1;
array copy
Write code to implement array copy (content copy)
int[] arr1 = {
10,20,30};
int[] arr2 = new int[arr1.length];
for(int i = 0; i < arr1.length; i++) {
arr2[i] = arr1[i];
}
Array addition/expansion
Requirements: Realize the effect of dynamically adding elements to the array, and realize the expansion of the array.
- Raw arrays use static allocation int[] arr = {1,2,3}
- The added element 4 is placed directly at the end of the array arr = {1,2,3,4}
- The user can decide whether to continue to add through the following methods, if the addition is successful, whether to continue? y/n
1. 定义初始数组int[] arr = {
1,2,3}//下标0-2
2. 定义一个新的数组int[] arrNew = new int[arr.length+1];
3. 遍历arr 数组,依次将arr 的元素拷贝到arrNew 数组
4. 将4 赋给arrNew[arrNew.length - 1] = 4;把4 赋给arrNew 最后一个元素
5. 让arr 指向arrNew ; arr = arrNew; 那么原来arr 数组就被销毁
6. 创建一个Scanner可以接受用户输入
7. 因为用户什么时候退出,不确定,使用do-while + break 来控制
Multidimensional array Two-dimensional array
dynamic initialization 1
- Syntax: type[][] array name=new type[size][size]
- For example: int a[][]=new int[2][3]
- The existence form of two-dimensional array in memory (!! drawing)
Dynamic initialization 2
First declare: type array name[][];
Redefine (open up space) array name = new type [size] [size]
Assignment (there is a default value, such as int type is 0)
static initialization
define type array name[][] = {
{value1,value2...},{value1,value2...},{value1,value2...}}
int[][] arr = {
{1,1, 1}, {8,8,9}, {100}};
Application case of two-dimensional array
Print a 10-line Yang Hui triangle using a two-dimensional array
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1. The first row has 1 element, and the nth row has n elements
2. The first element and the last element of each row are both 1
3. Starting from the third row, for the value of the element that is not the first element and the last element.
arr[i][j] = arr[i-1][j] + arr[i-1][j-1];
public class YangHui {
//编写一个main方法
public static void main(String[] args) {
/*
使用二维数组打印一个 10 行杨辉三角
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
规律
1.第一行有 1 个元素, 第 n 行有 n 个元素
2. 每一行的第一个元素和最后一个元素都是 1
3. 从第三行开始, 对于非第一个元素和最后一个元素的元素的值. arr[i][j]
arr[i][j] = arr[i-1][j] + arr[i-1][j-1]; //必须找到这个规律
*/
int[][] yangHui = new int[12][];
for(int i = 0; i < yangHui.length; i++) {
//遍历yangHui的每个元素
//给每个一维数组(行) 开空间
yangHui[i] = new int[i+1];
//给每个一维数组(行) 赋值
for(int j = 0; j < yangHui[i].length; j++){
//每一行的第一个元素和最后一个元素都是1
if(j == 0 || j == yangHui[i].length - 1) {
yangHui[i][j] = 1;
} else {
//中间的元素
yangHui[i][j] = yangHui[i-1][j] + yangHui[i-1][j-1];
}
}
}
//输出杨辉三角
for(int i = 0; i < yangHui.length; i++) {
for(int j = 0; j < yangHui[i].length; j++) {
//遍历输出该行
System.out.print(yangHui[i][j] + "\t");
}
System.out.println();//换行.
}
}
}
Two-dimensional array usage details and precautions
- One-dimensional arrays are declared in the following ways:
int[] x 或者int x[]
- Two-dimensional arrays are declared in the following ways:
int[][] y 或者int[] y[] 或者int y[][]
- A two-dimensional array is actually composed of multiple one-dimensional arrays, and the lengths of each one-dimensional array can be the same or different. For example: map[][] is a two-dimensional array
int map [][] = {
{
1,2},{
3,4,5}}
Map[0] is a one-dimensional array containing two elements, and map[1] is a one-dimensional array containing three elements. We also call it a two-dimensional array with different numbers of columns.
Articles and codes have been archived in [Github warehouse: https://github.com/timerring/java-tutorial] or the public account [AIShareLab] can also be obtained by replying to java .