Java Fundamentals 06 Array Fundamentals

Like you who love programming!
Learn SpringBoot actual combat course https://edu.csdn.net/course/detail/31433
Learn SpringCloud introductory course https://edu.csdn.net/course/detail/31451


Preface

Array is an important knowledge point of Java. The contents of this article are as follows:

  1. Introduction to arrays
  2. Array definition
  3. Initialization of the array
  4. foreach loop
  5. Arrays tools
  6. Use of two-dimensional arrays

Introduction to arrays

Why do you need an array

Question: Suppose we need to develop a student management system, how to save the names of 40 classmates in our class?
Define 40 variables

String name1 = “zhangsan”;
...
String name40 = “lisi”;

What if there are 100 or 1,000 classmates?
Variables can only store one piece of data, which is not suitable for storing large amounts of data.
You can use an array to save multiple data.

What is an array

Insert picture description here
Similar to the lockers outside the supermarket

  1. Can store a lot of items
  2. The size of each grid is equal
  3. Each grid is sorted in order, adjacent
  4. The grid is accessed by number

The concept of array

  1. An array is a continuous storage space in memory
  2. The type of each data in the array is the same
  3. The data in the array is accessed through the subscript

Array classification

The array is divided into:

  1. One-dimensional array, a row of continuous storage space
1 2 3 4 5
  1. A two-dimensional array can be seen as a structure with multiple rows and multiple columns. It
    can be seen as a nesting of one-dimensional arrays. Each array is a one-dimensional array.
1 2 3 4 5
6 7 8 9 10

Define array

When creating an array, define the length of the array at the same time.

类型[] 数组名 = new 类型[长度];
int[] array = new int[5];

[] can also be after the array name

类型 数组名[] = new 类型[长度];
int array[] = new int[5];

You can also declare the array first, and then define the length.

类型[] 数组名;
数组名 = new 类型[长度];

Note: After the array is created, the data will have default values:
int defaults to 0, float defaults to 0.0f, double defaults to 0.0, String defaults to null

The memory allocation of the array

  • The basic data type is to directly save the data value
  • The reference data type is the memory address where the data is saved
    Insert picture description here

Initialization of the array

The initialization of the array is to assign values ​​to the array elements.
Two kinds of initialization:

  1. Static initialization
    While defining the array, assign values ​​to the data of the array
数据类型[] 数组名 = {值1,值2,值3...};
如:int[] array = {20,44,33,100,55};
数据类型[] 数组名 = new int[]{值1,值2,值3...};
如:int[] array = new int[]{20,44,33,100,55};

Note: Static initialization cannot define the length. The length of the array is determined by the number of values.

  1. Dynamic initialization After
    defining the array, use a loop to assign values ​​to the array
int[] array = new int[5];
for(int i = 0;i < array.length;i++){
	array[i] = i;
}

By array array name [index] to access the data, attention:

  • Subscripts start from 0
  • If the subscript is not in the range of 0 to array length-1, ArrayIndexOutOfBoundsException will occur

Exercise: Define a string array with a length of 5, initialize the data dynamically, and output: the
first, last, and middle string values ​​of the array.

Enhanced for loop

The foreach loop is a syntax supported after jdk1.5, used to traverse arrays or collections,
syntax:

for(数据类型 变量名 : 数组名){
	访问变量的值
}

Note: This type of for loop can only access, and cannot modify the data in the array.
If you need to modify it, you need to use a regular for loop.

Exercise:
Define a 5-length string array and initialize the array statically.
Use a foreach loop to output the array elements

Two-dimensional array

What is a two-dimensional array
can be understood as a table structure composed of rows and columns, which can be regarded as a one-dimensional array nested in a one-dimensional array

Creation of two-dimensional array

数据类型[][]  数组名 = new 数据类型[行数][列数];
如:int[][]	array = new int[3][5];

Two-dimensional array access

数组名[行下标][列下标];
如:访问上面数组第二行第三个元素,array[1][2];

Two-dimensional array initialization
static initialization

数据类型[][] 数组名 = {
   
   {值,值,值...},{值,值,值...},{值,值,值...}..};
数据类型[][] 数组名 = new 数据类型[][]{
   
   {值,值,值...},{值,值,值...},{值,值,值...}..};

Dynamic initialization

int[][]	array = new int[3][5];
for(int i = 0;i < array.length;i++){
	for(int j = 0;j < array[i].length;j++){
		array[i][j] = i * j;
	}
}

Arrays tools

The Arrays class encapsulates many methods related to arrays, which can improve development efficiency.
Common methods:

  1. Sort
    Arrays.sort (array name)
  2. Find
    int Arrays.binarySearch (array name, the number to find) to
    get the result is to find the subscript of the number
  3. Copy
    Arrays.copyOf (the copied array name, the length
    of the new array ) , the result obtained is the new array
  4. Fill
    Arrays.fill (array name, number to be filled); fill the
    array with the same value
  5. Compare
    Arrays.equals (array name 1, array name 2)
    returns whether the two array values ​​are the same

End

This article is over, I don’t know if you have mastered it, let’s leave homework to check:
1. Define an integer array with a length of 5, and give 5 initial values ​​at the same time, and output the values ​​in the array in a loop.
2. Given an integer array a with 5 elements, find the sum of all the elements in a.
3. Define the array, enter the names of 5 people and save them in the array, and output the names of the first, third and fifth people.


If you need to learn other Java knowledge, poke here ultra-detailed knowledge of Java Summary

Guess you like

Origin blog.csdn.net/u013343114/article/details/112308634
Recommended