Detailed explanation of java array

 array

concept

A collection of data of the same type. In fact, an array is a container.

The benefits of arrays

The elements in the array can be automatically numbered from 0 , which is convenient for manipulating these elements.

Format 1 :

element type []  array name  = new  element type [ number of elements or array length ];

Example: int[] arr = new int[5];

Format 2 :

elementtype []  arrayname  = new  elementtype []{ element, element, ...};

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

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

 

If you need to store a large amount of data, for example, if you need to read 100 numbers, then you need to define 100 variables. Obviously, it doesn't make much sense to repeat the code 100 times. How to solve this problem, the Java language provides an array data structure, which is a container that can store elements of the same data type, and can store 100 numbers in an array.

1 The concept of an array

    A collection of data of the same type. In fact, an array is a container. There is a lot of data involved in the operation, so what needs to be done first. It is not how to operate but how to save the data for later operations, then the array is a way to store data, where we can store data. It is called a container. The things in the container are the elements of the array. The array can hold any type of data. Although it can hold any type of data, the defined array can only hold one type of element, that is, once the array is defined, then The type of data stored in it is also determined.

2 The benefits of arrays

    What is the difference between saving data and not saving data? The biggest advantage of arrays is that they can automatically number the stored elements. Note that the numbering starts from 0. It is convenient to manipulate these data.

For example, the student's ID, you can find the corresponding student by using the student ID.

3 Format of the array

element type[] array name = new element type [number of elements or array length];

Example: int[] arr = new int[5]; 

Case:

Requirement: want to define a container that can store 3 integers

accomplish:

1 declare an array variable

In order to use an array, the array must be declared in the program, and the element type of the array must be specified

= Left half:

    First write the left side to make it clear that the element type is int, and the container uses an array, so how to identify the array? . Then use a special symbol [] brackets to represent. If you want to use an array, you need to give the array a name, so here we name the array x . Then follow the equals sign.

Code reflects:  

int [] x 

Note: int x[] is also a format for creating arrays. It is recommended to declare arrays in the form int[]x.

2 Create an array

= Right half:

To use a new keyword. Called new. new is used to generate a container entity in the memory, the data needs to have space to store, and the space for storing a lot of data is opened up with the new operator, new int[3]; this 3 is the number of elements. The part on the right defines a real array in memory that can store 3 elements.

new int[3] does two things, first creates an array using new int[3], and then assigns a reference to the array to the array variable x.

 

int [] x=new int[3];

what type is x ?

Any variable must have its own data type. Note that this x is not of type int. int represents the type of the elements in the container. Then x is of type array.

Arrays are a separate data type. There are two types of data types: basic data types and reference data types. The second group is the reference data type. So you have now come into contact with one of the three reference data types. It is the array type [] The brackets represent the array.

4. int[] arr = new int[5]; what happened in memory? 

内存任何一个程序,运行的时候都需要在内存中开辟空间.int[] arr = new int[5]; 这个程序在内存中是什么样?这就涉及到了java虚拟机在执行程序时所开辟的空间,那么java开辟启动了多少空间呢?继续学习java的内存结构。

数组的定义

格式1:

元素类型[] 数组名 = new 元素类型[元素个数或数组长度];

示例:int[] arr = new int[5];

格式2:

元素类型[] 数组名 = new 元素类型[]{元素,元素,……};

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

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

注意:给数组分配空间时,必须指定数组能够存储的元素个数来确定数组大小。创建数组之后不能修改数组的大小。可以使用length 属性获取数组的大小。

遍历数组

数组初始化

数组的格式

int[] x = new int[3];

x[0] = 1;

x[1] = 2;

另一种定义:该形式可以直接明确数组的长度,以及数组中元素的内容

int[] x = { 1, 2, 3 };

 

int[] x=new int[]{1,2,3};

 

 

初始化方式1:不使用运算符new

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

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

初始化方式2:

int[] arr3=new int[3];

arr3[0]=1;

arr3[1]=5;

arr3[2]=6;

 

如果数组初始化中不使用运算符new。需要注意:下列写法是错误的。

int[] arr;

arr={1,2,3,4,5};

此时初始化数组,必须将声明,创建,初始化都放在一条语句中个,分开会产生语法错误。

所以只能如下写:

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

 

数组遍历

public static void main(String[] args) {

int[] x = { 1, 2, 3 };

for (int y = 0; y < 3; y++) {

System.out.println(x[y]);

// System.out.println("x["+y+"]="+x[y]); 打印效果 x[0]=1;

// 那么这就是数组的第一个常见操作.遍历

}

数组中有一个属性可以获取到数组中元素的个数,也就是数组的长度. 数组名.length

public static void main(String[] args) {

int[] x = { 1, 2, 3 };

for (int y = 0; y < x.lengthy++) {

System.out.println(x[y]);

// System.out.println("x["+y+"]="+x[y]); 打印效果 x[0]=1;

// 那么这就是数组的第一个常见操作.遍历

}

 

数组的常见异常

数组中最常见的问题:

1. NullPointerException 空指针异常
原因: 引用类型变量没有指向任何对象,而访问了对象的属性或者是调用了对象的方法。\


2. ArrayIndexOutOfBoundsException 索引值越界。
原因:访问了不存在的索引值。


一数组角标越界异常:,注意:数组的角标从0开始。

public static void main(String[] args) {

int[] x = { 1, 2, 3 };

System.out.println(x[3]);

//java.lang.ArrayIndexOutOfBoundsException

}

 

 

 

二 空指针异常:

public static void main(String[] args) {

int[] x = { 1, 2, 3 };

x = null;

System.out.println(x[1]);

// java.lang.NullPointerException

}

 

数组:

什么时候使用数组:当元素较多时为了方便操作这些数组,会先进行来临时存储,所使用的容器就是数组。

特点:

数组长度是固定的。

数组的内存分析





 二维数组

Arrays的使用

遍历: toString()    将数组的元素以字符串的形式返回

排序: sort()        将数组按照升序排列

查找: binarySearch()在指定数组中查找指定元素,返回元素的索引,如果没有找到返回(-插入点-1) 注意:使用查找的功能的时候,数组一定要先排序。

 

二维数组:实质就是存储是一维数组。

数组定义:

       数组类型[][] 数组名 = new 数组类型[一维数组的个数][每一个一维数组中元素的个数];



疑问: 为什么a.length = 3, a[0].length = 4?


数组的初始化:

静态初始化:

int [][] a = new int[][]{ {12,34,45,89},{34,56,78,10},{1,3,6,4} };

动态初始化:



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324142131&siteId=291194637