java array definition, usage, and memory array Detailed analysis

Foreword

This article from the container to the concept of an array of three definitions of the concept of an array and then to analyze the case of memory arrays, and some common traverse the array and reversed, takes a maximum value and other operations. Aimed at a comprehensive understanding of java array. @ [Toc]

1. What is a container

Speaking before the array, it is necessary to talk about the concept of the so-called container vessel is to store multiple data together, each data element called the container.

Life in the container: glass, wardrobe, classroom

2. What is an array

The so-called data array is stored in a fixed length of the container , to ensure that the plurality of data consistent with the data type .

3, an array of three definitions

Mainly on the following three definitions under way array.

A defined way

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

Example: definition of three integers can be stored in an array of containers, such as the code: int[] arr = new int[3]

Second way is defined

数据类型[] 数组名 = new 数据类型[]{元素1,元素2,元素3...};

Example: 1,2,3,4,5 storage container defines an array of integers. int[] arr = new int[]{1,2,3,4,5};

Three ways defined

数据类型[] 数组名 = {元素1,元素2,元素3...};

Example: 1,2,3,4,5 storage container defines an array of integers int[] arr = {1,2,3,4,5};

Detailed array definition format:

Storage array data types: arrays container creates what type of data can be stored. []: An array. An array of name: from the definition of an array of variable names, identifiers meet specifications, you can use the name manipulate arrays new: keywords, creating keywords used by the array. Storage array data types: arrays container creates what type of data can be stored. [Length]: length of the array, the array represents the number of elements in the container can be stored. Note: the fixed-length array of properties, once the specified length, can not be changed. Cups and the same reason, to buy a 2-liter glass, the total capacity is 2 liters, no more and no less.

4, the array access

Index : From the 0start, the index ( indexaccess to) the element in the array.

Format :数组名[索引]

Attribute array length : length of the array is fixed, the maximum index because the index starts from 0, the value of the array数组名.length-1

public static void main(String[] args) { 
int[] arr = new int[]{1,2,3,4,5}; 
//打印数组的属性,输出结果是5 
System.out.println(arr.length); 
}
复制代码

5. What is the memory

Before talking about the principle of the memory array, one must understand the concept of memory called.

Memory is an important original computer, the temporary storage area, is to run the program. Program we write is stored in the hard disk, the program in the hard disk is not running, must be placed in memory to run, after the run is completed will clear the memory. Java virtual machine is running the program must be allocated and managed memory space.

5.1 Java virtual machine's memory division

In order to improve operational efficiency, it has been divided into different spatial regions, because each region has a specific way of processing data and memory management.

Here Insert Picture Description

5.2 array is stored in memory

5.2.1 a memory array of FIG.
public static void main(String[] args) { 
	int[] arr = new int[3]; 
	System.out.println(arr);//[I@5f150435 
	}
复制代码

The implementation of the above method, the resulting output is [I@5f150435what this is it? It is the address of the array in memory. newOut of the contents are stored in the heap memory, and the method of variables arrsaved is the address of the array.

Output arr[0], the output will be stored in memory address arr elements on the array index 0 as no specific determination value, it is the default type! such as:

 String[] arr=new String[3];
   System.out.println(arr);     //  [Ljava.lang.String;@1b6d3586
   System.out.println(arr[0]);  //  null
   
 int[] arrInt=new int[3];
   System.out.println(arrInt);    // [I@1b6d3586
   System.out.println(arrInt[0]); // 0
复制代码

Here Insert Picture Description

5.2.2 two memory arrays of FIG.
 public static void main(String[] args) { 
	 int[] arr = new int[3]; 
	 int[] arr2 = new int[2]; 
	 System.out.println(arr); 
	 System.out.println(arr2); 
 }
复制代码

Here Insert Picture Description

5.2.3 The two variables point to an array
 public static void main(String[] args) { 
	 // 定义数组,存储3个元素 
	 int[] arr = new int[3]; 
	 //数组索引进行赋值 
	 arr[0] = 5; 
	 arr[1] = 6; 
	 arr[2] = 7; 
	 //输出3个索引上的元素值 
	 System.out.println(arr[0]);
	 System.out.println(arr[1]); 
	 System.out.println(arr[2]); 
	 //定义数组变量arr2,将arr的地址赋值给arr2 
	 int[] arr2 = arr; 
	 arr2[1] = 9; 
	 System.out.println(arr[1]); 
 }
复制代码

Here Insert Picture Description

6, an array of common exceptions

There are an array of common exceptions 数组越界异常and 空指针异常this is very basic, do not introduced, where the main analysis under the null pointer exception in the case of memory

Here Insert Picture Description

7, array traversal [focus]

The so-called array array traversal is to get out each element separately, it is to walk. Through the array it is very important ! ! !

 public static void main(String[] args) { 
	 int[] arr = { 1, 2, 3, 4, 5 }; 
	 System.out.println(arr[0]); 
	 System.out.println(arr[1]); 
	 System.out.println(arr[2]); 
	 System.out.println(arr[3]); 
	 System.out.println(arr[4]); 
	 } 
复制代码

The above code is that you can traverse the entire array each element out, but if the array elements are very much, I put in this way is called fool traverse, this fool writing certainly not, so we need to transform into a cycle of writing. Indexed array is 0to lenght-1be circulated as a condition occurs. as follows

 public static void main(String[] args) { 
	 int[] arr = { 1, 2, 3, 4, 5 }; 
	 for (int i = 0; i < arr.length; i++) {
	 System.out.println(arr[i])
	    } 
	  } 
复制代码

8, the maximum value acquired array element

Realization of ideas: definition of variables, 0 backup array through the array of elements on the index, obtaining the variable on each element of the array elements and will traverse the backup array index value is compared to 0 if the value of the array element is greater than the value of the variable , recorded live in the new variable loop through the end of the array of values, the maximum value is stored in an array variable

Shannon really Warning : You be careful, do not be the first and the second wife bewitched

Here Insert Picture Description
code show as below:

public static void main(String[] args) { 
	int[] arr = { 5, 15, 2000, 10000, 100, 4000 }; 
	//定义变量,保存数组中0索引的元素 
	int max = arr[0]; 
	//遍历数组,取出每个元素 
	for (int i = 0; i < arr.length; i++) { 
	//遍历到的元素和变量max比较 
	//如果数组元素大于max 
	if (arr[i] > max) { 
	//max记录住大值 
	max = arr[i]; 
    } 
  }
System.out.println("数组最大值是: " + max);
} 
复制代码

9, an array of reverse

The so-called inversion of array elements is to position reversed

Realization idea: an array of the most distal elements are reversed for reversing, we need to exchange an array of the most remote location elements define two variables, the position of the smallest element exchange index on the minimum and maximum index index index save two arrays + +, the largest index - again exchange position of the minimum index exceeds the maximum index, ending array inversion operation

Here Insert Picture Description

Specific code as follows

public static void main(String[] args) { 
	int[] arr = { 1, 2, 3, 4, 5 }; 
	/*循环中定义变量min=0最小索引 
	max=arr.length‐1最大索引 
	min++,max‐‐ */ 
	for (int min = 0, max = arr.length ‐ 1; min <= max; min++, max‐‐){
	//利用第三方变量完成数组中的元素交换 
	int temp = arr[min]; 
	arr[min] = arr[max]; 
	arr[max] = temp;
	 }
	// 反转后,遍历数组 
	for (int i = 0; i < arr.length; i++) {
	 System.out.println(arr[i])
	 } 
 } 
复制代码

10, a method array as parameter and return value

First thing clear: array as the return value, the return address of the memory array

public static void main(String[] args) {
	 //调用方法,接收数组的返回值 
	 //接收到的是数组的内存地址 
	 int[] arr = getArray(); 
	 for (int i = 0; i < arr.length; i++) { 
	 System.out.println(arr[i])
	    } 
	 }
	 /* 创建方法,返回值是数组类型创建方法,返回值是数组类型 
	 return返回数组的地址 */
	 public static int[] getArray() { 
	 int[] arr = { 1, 3, 5, 7, 9 };
	  //返回数组的地址,返回到调用者
     return arr; 
   }
复制代码

Here Insert Picture Description

11, the difference between the method parameter type

Parameter as the basic method of the type, the data value is passed parameter method is a reference type, the address value is passed.

Analysis following code, outputs the calculation result.

 public static void main(String[] args) { 
	 int a = 1; int b = 2; 
	 System.out.println(a); 
	 System.out.println(b); 
	 change(a, b); 
	 System.out.println(a); 
	 System.out.println(b); 
	 }
	 public static void change(int a, int b) { 
	 a = a + b; b = b + a; 
 } 
复制代码
  1. Analysis following code, outputs the calculation result.
public static void main(String[] args) { 
	int[] arr = {1,3,5}; 
	System.out.println(arr[0]); 
	change(arr); 
	System.out.println(arr[0]); 
}
	
	public static void change(int[] arr) {
	arr[0] = 200; 
   }
复制代码

Summary: The parameters of the method when the basic type, the value of the parameter is a data transfer method is a reference type, the address value is passed.

I am sure you have students copy the above code to run eclipse or idea, is not it the result of an accident? And not very understanding of the following summary? The child boots, I guess you probably miss the point: the array is a reference to no effect type, the array element type itself, like the array elements are all int, the array is still a reference type, ha ha, white students really cute ~ shot baa, at the point of a gun, then praise chant support, concern the landlord, the landlord takes you to learn java, concerned about the landlord does not get lost hhhhh ~

I welcome you to focus on the public number, to explore technology, yearning technology, the pursuit of technology ...

Here Insert Picture Description

Guess you like

Origin juejin.im/post/5dc0c0695188255f7b0fa357