Introduction to the array

The concept of an array: A container for storing multiple elements of the same data type.

Defined format:

A: Data type [] array name;

B: data type array name [];

For example:

A: Defines an array of type int called A,int[] A

B: Define an array variable int a[]

Note: the effect is the same, both define an array of type int, the syntax is different, recommend the first type.

packageJava base 04_ one-dimensional array;/* * How to initialize an array data * A: the so-called initialization is to create A chunk of memory for the array, without assigning A value to each array element * B: there are two ways to initialize an array: * A: dynamic initialization: only the length is given, and the system gives the initial value. Dynamic initialization: * data type [] array name = new data type [array length (positive integer)]; * example: int[] as = new int[3]; How does * * store elements? * What is an index value? * The index value is similar to the real number, which starts from 1 and the index value starts from 0. * Storage: * array name [index value]= data; Array length = maximum index value +1, maximum index value = array length -1; * * /
public class ArrayDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// Define an array of type int with length 3
		int[] as = new int[3];
		as[2] =36;
		int i=as[2];
		System.out.println(i);
		int o = as[2];
		System.out.println(o);
		/* * define two arrays, output each element * and reassign each array element * */
		int[] a =new int[2];
		int[] b = new int[3];
		System.out.println("First array:");
		System.out.println(a[0]);
		System.out.println(a[1]);
		System.out.println("Second array:");
		System.out.println(b[0]);
		System.out.println(b[1]);
		System.out.println(b[2]);
		/ / assignment:
		a[0] =1;
		a[1] =2;
		b[0] =1;
		b[1] =2;
		b[2] =3;
		System.out.println("First array after assignment:");
		System.out.println(a[0]);
		System.out.println(a[1]);
		System.out.println("Second array after assignment:");
		System.out.println(b[0]);
		System.out.println(b[1]);
		System.out.println(b[2]);
		System.out.println("= = = = = = = = = = = = = = = = = = = = = = = = = =");
		/* * define the first array. Define a second array, assign the address of the first array to the second array, and then assign to the second array. Finally, print the address of the first array again: arr2 = arr1; * /
		int[] arr3 = new int[3];
		arr3[0] =1;
		arr3[1] =2;
		arr3[2] =3;
		System.out.println(arr3[0]);
		System.out.println(arr3[1]);
		System.out.println(arr3[2]);
		System.out.println("Address value:"+arr3);
		int[] arr4 =arr3;
		System.out.println(arr4[0]);
		System.out.println(arr4[1]);
		System.out.println(arr4[2]);
		arr4[2] =333;// Reassign arr4[2] and arr3[2] will also change.
		System.out.println(arr3[2]);// print 333, because the address value is the same, operating on the same fast memory space

	}

Copy the code
packageJava base 04_ one-dimensional array;/* * Static initialization of arrays: * Format: data type [] Array name = new data type []{element 1, element 2, element 3,...... }; * Simplified format: data type [] Array name ={element 1, element 2, element 3,...... }; Int [] arr1 = new int[]{1,2,3,4,5}; Int [] arr1 ={1,2,3,4,5}; * Note: You cannot declare dynamic and static at the same time. * /
public class ArrayDemo2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// Statically define a one-dimensional array
		int[] arr1 = new int[] {1.2.3.4.5};
		int[] arr2 = {1.2.3.4.5.6.7.8};
	    String[] tanke={"Zhang fei"."Liu bei"."Guan yu"};
	    // Get the third element in arr1
	    System.out.println(arr1[2]);
	    // Get the length of tanke
	    int tk = tanke.length;
	    System.out.println(tk);
	    // Get the last element in tanke.
	   String str = tanke[tk-1];
	   System.out.println(str);
	   / * * array operations of two common small problems: * 1. The array subscript bounds: Java. Lang. ArrayIndexOutOfBoundsException * 2. Null pointer exception: Java. Lang. NullPointerException * reason: array has no memory address values, access, and you still use an array name must be a null pointer exception * /
		int[] i = {1.2.3};
		//System.out.println(i[3]); // 1. Array subscript is out of bounds
		i=null;
		System.out.println(i[3]);//2. The null pointer is abnormal}}Copy the code