文章目录
1、数组的基本概念
1、数组的创建
int[] array = new int[10];//创建一个存放10个int类型元素的数组
double[] array2 = new double[5]; // 创建一个可以存放5个double类型元素的数组
String[] array3 = new double[3]; // 创建一个可以存放3个字符串元素的数组
2、数组的初始化
1、动态初始化
创建数组时,Can specify the number of elements in the array;
int[] array = new int[10];
//'10'Is I specify the number of elements in the array,This can be arbitrarily change
2、静态初始化
int[] array1 = new int[]{
0,1,2,3,4,5,6,7,8,9};
double[] array2 = new double[]{
1.0, 2.0, 3.0, 4.0, 5.0};
String[] array3 = new String[]{
"hell", "Java", "!!!"};
注意:
1、静态初始化虽然没有指定数组的长度,编译器在编译时会根据{}中 Number of elements to determine the length of the array;
2、静态初始化时, {}中数据类型必须与[]前数据类型一致;
3、静态初始化可以简写,省去后面的new xxx[ ];
Static initialization also can be written as follows:
int[] array;
double[] array1;
float[] array2;
We note that these array is not initialized to him,What they are stored in?
看下表:
如果数组中存储元素类型为引用类型,默认值为null
3、数组的使用
1、数组元素的访问
与c语言相同,We through the subscript on a visit to an array of.同样的,We can also change the original value of the array by subscripting.
int[] array = {
1,2,3,4};
array[2] = 99;
System.out.println(array[2]);
如图所示:array[2]的值变成了99;
与c语言不同的是,javaIn an array of things will direct error
报错内容:
2、遍历数组
什么是 ‘遍历’?
All the elements in the array are visit again, 访问是指对数组中的元素进行某种操作.
注意:数组对象.length 来获取数组的长度
可以使用for each来遍历数组
int[] array = {
1, 2, 3};
for (int x : array) {
//int x ; x代表数组中的元素,int代表元素类型
System.out.println(x);
}
运行结果
for-each 是 for 循环的另外一种使用方式. 能够更方便的完成对数组的遍历. 可以避免循环条件和更新语句写错
2、数组是引用类型
1、认识JVM内部结构
1、程序计数器 : 只是一个很小的空间, 保存下一条执行的指令的地址.
2、虚拟机栈: 与方法调用相关的一些信息,每个方法在执行时,都会先创建一个栈帧,栈帧中包含有:局部变量表、操作数栈、动态链接、返回地址以及其他的一些信息,保存的都是与方法执行时相关的一些信息.比如:局部变量.当方法运行结束后,栈帧就被销毁了,即栈帧中保存的数据也被销毁了.
3、本地方法栈: 本地方法栈与虚拟机栈的作用类似. 只不过保存的内容是Native方法的局部变量.
4、堆: JVM所管理的最大内存区域. 使用 new 创建的对象都是在堆上保存 (例如前面的 new int[]{1, 2,3} ),堆是随着程序开始运行时而创建,随着程序的退出而销毁,堆中的数据只要还有在使用,就不会被销
毁.
5、方法区: 用于存储已被虚拟机加载的类信息、常量、静态变量、即时编译器编译后的代码等数据. 方法编译出的的字节码就是保存在这个区域.
Now we know about the heap and virtual machine stack the two parts can be.
2、基本类型变量与引用类型变量的区别
1、基本数据类型创建的变量:称为基本变量,The variable space directlyStorage is the corresponding value;
2、引用数据类型创建的变量:一般称为对象的引用,其空间中存储的是对象所在空间的地址.
举例:
public static void main() {
int a = 10;
int b = 20;
int[] arr = new int[]{
1,2,3};
}
在上述代码中,a、b、arr,都是函数内部的变量,因此其空间都在main方法对应的栈帧中分配.
a、b是内置类型的变量,因此其空间中保存的就是给该变量初始化的值.
array是数组类型的引用变量,其内部保存的内容可以简单理解成是数组在堆空间中的首地址.
Look at the picture to explain more clearly:
引用变量并不直接存储对象本身,可以简单理解成存储的是对象在堆中空间的起始地址.通过该地址,引用变量便可以去操作对象
3、再谈引用变量
看代码:
int[] array1 = new int[3];
array1[0] = 10;
array1[1] = 20;
array1[2] = 30;
int[] array2 = new int[]{
1,2,3,4,5};
array2[0] = 100;
array2[1] = 200;
array1 = array2;
array1[2] = 300;
array1[3] = 400;
array2[4] = 500;
for (int i = 0; i < array2.length; i++) {
System.out.println(array2[i]);
}
运行结果:
There may be a friend wondering why is this?
Let me draw to explain
This is the beginning;
经过==array1 = array2;==后,array2中的内容放到了array1中,因此,The contents of the memory into a chart like this
此时,array1指向的是array2所指向的数组;因此对array1进行操作,实际上是对array2数组操作;
4、认识null
Believe that learning from language friend fornull并不陌生,在C语言中null代表空指针;在 Java 中null表示 “空引用” , 也就是一个不指向对象的引用;
int[] arr = null;
System.out.println(arr[0]);
报错:
注意:注意: Java 中并没有约定 null 和 0 号地址的内存有任何关联;
3、数组的应用场景
1、保存数据
Array can store a set of data of the same type
2、Array can be used as a function parameter
There are two kinds of function parameters can be,一种是基本数据类型,如 int、short等,Another kind is the reference data type,例如数组
public static void main(String[] args) {
int[] arr = {
1, 2, 3};
func(arr);
System.out.println("arr[0] = " + arr[0]);
}
public static void func(int[] a) {
a[0] = 10;
System.out.println("a[0] = " + a[0]);
}
// 执行结果
a[0] = 10
arr[0] = 10
//因为数组是引用类型,按照引用类型来进行传递,是可以修改其中存放的内容的
总结:所谓的 “引用” Essentially just opened up a piece of space on the stack only saved inside an address..Java 将数组设定成引用类型, 这样的话后续进行数组参数传参, 其实只是将数组的地址传入到函数形参中,这样可以避免对整个数组的拷贝(提高效率,减少浪费).
3、作为函数的返回值
public static int[] Findmax(int[] array){
int[] ret = {
array[0]};
for (int i = 0; i < array.length; i++) {
if(array[i] > ret[0]){
ret[0] = array[i];
}
}
return ret;
}
public static void main(String[] args) {
int[] array = {
1,3,7,2};
int[] ret = Findmax(array);
System.out.println(Arrays.toString(ret));
}
如上面代码所示,函数参数是数组,And the return value is an array,Finally, in the form of a string to print outarray数组中的最大值
4、二维数组
1、二维数组的创建与初始化
二维数组本质上也是一维数组,只不过每个元素又是一个一维数组.
数据类型[][] 数组名称 = new 数据类型 [行数][列数] {
初始化数据 };
//举个简单的例子,Create a two-dimensional array to store the integer
int[][] array = new int[2][3]{
1,2,3,4,5,6}
int[][] arr = {
{
1, 2, 3, 4},{
5, 6, 7, 8},{
9, 10, 11, 12}};
for (int row = 0; row < arr.length; row++) {
for (int col = 0; col < arr[row].length; col++) {
System.out.printf("%d\t", arr[row][col]);
}
System.out.println("");//换行
}
运行结果:
2、二维数组的存储
Believe everyone for two dimensional array in memory is how to store must be full of curiosity,So let me answer for you:
public static void main(String[] args) {
int[][] array = new int[2][3];
System.out.println(Arrays.toString(array));
}
当我们执行上述代码时,我们会惊讶的发现,We print out two addressIf we have to print out the contents of the two dimensional array,You need to use operating an array of toolsArrays.deepToString,即深度打印
public static void main(String[] args) {
int[][] array = new int[2][3];
System.out.println(Arrays.deepToString(array));
}
java的二维数组与CLanguage is stored in a slightly different way
int[][] array = new int[2][3];
System.out.println(array[0]);
System.out.println(Arrays.toString(array[0]));
System.out.println(array[1]);
System.out.println(Arrays.toString(array[1]));
Look at the code we it is not hard to seearray0The subscript is deposited with a subscript address,After only converts it to a string,Only can print out the value
版权声明
本文为[hania_w]所创,转载请带上原文链接,感谢
https://cdmana.com/2022/218/202208060748118459.html