19 array definition and access

Chapter 19 array definition and access

Author: Zhang Zimo

1, Concept of container

A container is a storage structure that can store multiple data together. Each data is called an element of the container.

  • **Containers in life: * * water cup, wardrobe, classroom

2, The concept of array

Array is a kind of container with fixed data length, which ensures that the data types of multiple data should be consistent.

3, Definition of array

1. Definition method ①

Array storage data type [] array name = new array storage data type [length];
  • Detailed explanation of array definition format

    • Data type of array storage: what data type can be stored in the created array container.

    • []: represents an array.

    • Array name: set a variable name for the defined array to meet the identifier specification. You can use the name to operate the array.

    • new: keyword, the keyword used to create the array.

    • Data type of array storage: what data type can be stored in the created array container.

    • [length]: the length of an array, indicating how many elements can be stored in the array container.

    • Note: array has fixed length property. Once the length is specified, it cannot be changed.

      • It's the same with the water cup. If you buy a 2-liter water cup, the total capacity is 2 liters, which can't be more or less.
  • For example: define an array container that can store 3 integers, the code is as follows:

    int[] arr = new int[3];
    

2. Definition method ②

Data type [] array name = new data type [] {element 1, element 2, element 3,...};
  • Example: define an array container to store integers 1, 2, 3, 4, 5

    int[] arr = new int[]{1, 2, 3, 4, 5};
    

3. Definition mode ③

Data type [] array name = {element 1, element 2, element 3,...};
  • Example: define an array container to store integers 1, 2, 3, 4, 5

    int[] arr = {1, 2, 3, 4, 5};
    

4. Example

	/*
	The concept of array: a container that can hold multiple data values at the same time.
	
	Array features:
		1.Array is a reference data type
		2.Multiple data in the array must be of the same type
		3.The length of the array cannot be changed during program operation
		
	Initialization of array: create an array in memory and assign some default values to it.
	
	There are two common initialization methods:
		1.Dynamic initialization (specify length)
		2.Static initialization (specify content)
		
	Format of dynamic initialization array:
		Data type [] array name = new data type [array length];
		
		Meaning:
			Left data type: that is, the data stored in the array is the same type
			Bracket on the left: represents an array
			Array name on the left: give the array a name
			new on the right: represents the action of creating an array
			Data type on the right: must be consistent with the data type on the left
			The length of the bracket on the right: that is, in the array. How many data can be saved is an int number
	*/
	public class Demo01Array {
		public static void main(String[] args) {
			/*int score1 = 100;
			int score2 = 98;
			int score3 = 99;
			System.out.println(score3); //99
			
			score3 = 100;
			System.out.println(score3); //100*/
			
			//Create an array that can hold 300 int data
			//Format: data type [] array name = new data type [array length];
			int[] arrayA = new int[300];
			
			//Create an array that can hold 10 data of double type
			double[] arrayB = new double[10];
			
			//Create an array that can hold 5 strings
			String[] arrayC = new String[5];
		}
	
	}
	
	
	
	/*
	Dynamic initialization: when creating an array, directly specify the number of data elements in the array
	
	Static initialization: when creating an array, you do not directly specify the number of data, but directly specify the specific data content
	
	Static initialization basic format:
		Data type [] array name = new data type [] {element 1, element 2,...};
		
	Note: Although the static initialization does not tell the length directly, the length can also be calculated automatically according to the specific contents of the elements in the braces
	*/
	public class Demo02Array {
		public static void main(String[] args) {
			//Create an array directly, which contains all int numbers, specifically: 5, 15, 25
			int[] arrayA = new int[] {5, 15, 25, 40};
			
			//Create an array to hold strings: "Hello", "World", "Java"
			String[] arrayB = new String[] {"Hello", "World", "Java"};
		}
	
	}
	
		
	
	/*
	When using static initialization of arrays, the format can also be omitted.
	
	standard format
		Data type [] array name = new data type [] {element 1, element 2,...};
		
	Omitted format:
		Data type [] array name = {element 1, element 2,...};
	
	matters needing attention:
		1.Static initialization does not directly specify the length, but it still automatically calculates the length
		2.The static initialization standard format can be split into two steps
		3.Dynamic initialization can also be split into two steps
		4.Once static initialization uses the omitted format, it cannot be split into two steps
		
	Usage suggestions: if you are not sure about the specific contents of the array, use dynamic initialization; otherwise, you have determined the specific contents, use static initialization.
	*/
	public class Demo03Array {
		public static void main(String[] args) {
			//Omit static initialization of format
			int[] arrayA = {10, 20, 30};
			
			//The standard format of static initialization can be divided into two steps
			int[] arrayB;
			arrayB = new int[] {11, 21, 31};
			
			//Dynamic initialization can also be divided into two steps
			int[] arrayC;
			arrayC = new int[5];
			
			//The omitted format of static initialization cannot be split into two steps
			/*int[] arrayD;
			arrayD = {10, 20, 30};*/
		}
	
	}

4, Access to arrays

1. Index

Every element stored in the array will automatically have a number. Starting from 0, this automatic number is called the array index. You can access the elements in the array through the index of the array.

2. Array access format

Array name [index]

3. Array length property

Each array has a length and is fixed. Java gives an attribute of an array to get the length of the array. The statement is: array name. Length, the execution result of attribute length is the length of the array, and the result of int type. It can be inferred that the maximum index value of an array is the array name. length-1.

4. Index access to elements in an array

  • Array name [index] = value, assign value to element in array

  • Variable = array name [index], get the elements in the array

5. Example

	/*
	Print the array name directly to get the memory address hash value corresponding to the array.
	
	Format to access array elements: array name [index value]
	
	Index value: it is an int number, representing the number of elements in the array.
	
	[Note] index values start at 0 and end at array length - 1.
	*/
	public class Demo04ArrayUse {
		public static void main(String[] args) {
			//Omitted format of static initialization
			int[] array = {10, 20, 30};
			
			System.out.println(array); // [I@185d8b6
			
			//Length of printout array
			System.out.println(array.length);
			
			//Print the elements in the array directly
			System.out.println(array[0]); // 10
			System.out.println(array[1]); // 20
			System.out.println(array[2]); // 30
			
			//Assign values to array elements
			array[2] = 100;
			System.out.println(array[2]);
			
			//You can also assign a single element of an array to a variable
			int num = array[1];
			System.out.println(num); // 20
		}
	
	}
Published 22 original articles, won praise 2, visited 666
Private letter follow

Tags: Java Attribute less

Posted on Sun, 15 Mar 2020 03:47:47 -0700 by straycat