![afbeefd145549eaef15650ce0208595b.png](https://img-blog.csdnimg.cn/img_convert/afbeefd145549eaef15650ce0208595b.png)
一.阵列概述
数组是一个容器对象,它包含固定数量的单个类型的值。
数组的长度是在创建数组时确定的。创建后,其长度是固定的。
众所周知,Array数组是一种数据结构,我们存储相似的元素,数组从索引0开始。
数组中的每个项都称为Element元素,每个元素都由其数值index索引访问。
由于数组是Java中的objects对象,我们可以使用成员长度来确定它们的长度。
Java数组变量也可以像其他变量一样在数据类型后面用[]声明。
数组中的变量是有序的,每个变量都有一个从0开始的索引。
Java数组也可以用作static静态字段、local局部变量或方法参数。
数组的大小必须由int值指定,不能长或短。
![33ce07c25b69a9f5e966d131cefa2a60.png](https://img-blog.csdnimg.cn/img_convert/33ce07c25b69a9f5e966d131cefa2a60.png)
二.Array数组类型
有2种类型的数组
1.一维阵列。
2.多维数组。
一维阵列举例:
只存储原语或对象的数组称为一维数组。例如,让我们创建一个整数数组,在数组中放入一些值,并将每个值打印到标准输出。
![109809474765e5a5ee45869c77b73bc1.png](https://img-blog.csdnimg.cn/img_convert/109809474765e5a5ee45869c77b73bc1.png)
输出 output:
![75dcb630a241f7354d1fcd207201ef02.png](https://img-blog.csdnimg.cn/img_convert/75dcb630a241f7354d1fcd207201ef02.png)
多维数组。
多维数组存储其他数组。它是一个数组。在多维数组中,数组中的每一个元素都保存着其它数组的引用。多维数组是通过在每个维度上附加一组方括号([])创建的。
![b906e7ad1a3b288f39f9fbb1045d1770.png](https://img-blog.csdnimg.cn/img_convert/b906e7ad1a3b288f39f9fbb1045d1770.png)
输出output
![233a975275eb569d7fc0df01b39c45e8.png](https://img-blog.csdnimg.cn/img_convert/233a975275eb569d7fc0df01b39c45e8.png)
三.声明数组示例
让我们来声明一个数组像这样:
![918d20b69bdcaebab094d5528d2335cd.png](https://img-blog.csdnimg.cn/img_convert/918d20b69bdcaebab094d5528d2335cd.png)
数组声明有两个组件:数组的类型和数组的名称。
数组的类型写为type[],其中type是包含元素的数据类型;括号是表示此变量包含数组的特殊符号。数组的大小不是其类型的一部分(这就是方括号为空的原因)。
上面的程序an array之类的变量是variable,声明实际上并不创建数组;它只是告诉编译器这个变量将保存指定类型的数组。
声明数组的例子很少。
![715d0229f2026958dac6dcded877f3eb.png](https://img-blog.csdnimg.cn/img_convert/715d0229f2026958dac6dcded877f3eb.png)
也可以在数组名称后放置括号:
![c67d04ddd9a3f9b7f08a72068a22f9f8.png](https://img-blog.csdnimg.cn/img_convert/c67d04ddd9a3f9b7f08a72068a22f9f8.png)
但是,约定不鼓励这种形式;括号标识数组类型,并应与类型名称一起出现。
四.创造一个Array数组例子:
创建数组的一种方法是使用新的运算符。
![3d539cdb945273a3f076a5eece5f0383.png](https://img-blog.csdnimg.cn/img_convert/3d539cdb945273a3f076a5eece5f0383.png)
上面的代码行为10个整数元素分配一个具有足够内存的数组,并将该数组分配给anArray变量。
创建数组的其他几个示例:
![6bcac71ad48c2e086c44cb2559ba5ac6.png](https://img-blog.csdnimg.cn/img_convert/6bcac71ad48c2e086c44cb2559ba5ac6.png)
五.初始化数组示例
初始化整数数组示例
让我们用几个整型元素创建和初始化整型数组,如:
![d056521437f1a0f45e321deef91c11bb.png](https://img-blog.csdnimg.cn/img_convert/d056521437f1a0f45e321deef91c11bb.png)
初始化字符串数组示例
让我们用几个字符串String元素创建和初始化字符串String数组,如:
![e66ae13e64e4ae4f80c79dd6d30f158f.png](https://img-blog.csdnimg.cn/img_convert/e66ae13e64e4ae4f80c79dd6d30f158f.png)
初始化雇员数组示例
让我们用几个Employee对象创建和初始化Employee数组:
![2e3fad11a9da5702146bc19991721321.png](https://img-blog.csdnimg.cn/img_convert/2e3fad11a9da5702146bc19991721321.png)
![8b6ebdeb959f89ae9a2dc09c0a594c61.png](https://img-blog.csdnimg.cn/img_convert/8b6ebdeb959f89ae9a2dc09c0a594c61.png)
六.访问数组示例
访问整数数组示例
让我们创建整数数组,用少量元素初始化,用索引访问整数数组。
![ba5cf5e20329b2dcb50ee266bcce5fee.png](https://img-blog.csdnimg.cn/img_convert/ba5cf5e20329b2dcb50ee266bcce5fee.png)
输出:
![0d3815e200d67200e234146060e42569.png](https://img-blog.csdnimg.cn/img_convert/0d3815e200d67200e234146060e42569.png)
访问字符串数组示例
让我们创建字符串数组,用少量元素初始化,用索引访问字符串数组。
![896d8df82c9948c1e4f8388d0124897a.png](https://img-blog.csdnimg.cn/img_convert/896d8df82c9948c1e4f8388d0124897a.png)
输出:
![a132f759afff129227d47bad7f747ee9.png](https://img-blog.csdnimg.cn/img_convert/a132f759afff129227d47bad7f747ee9.png)
访问员工数组示例
让我们创建雇员数组,用很少的元素初始化,用索引访问雇员数组。
![87f47689e972425346a265b476e41d45.png](https://img-blog.csdnimg.cn/img_convert/87f47689e972425346a265b476e41d45.png)
![73ed47f12e94de820b9baee8eb6e8051.png](https://img-blog.csdnimg.cn/img_convert/73ed47f12e94de820b9baee8eb6e8051.png)
输出:
![e50957ef477dadbab4f6f808fc2a9909.png](https://img-blog.csdnimg.cn/img_convert/e50957ef477dadbab4f6f808fc2a9909.png)
七.创建和初始化数组的快捷语法
或者,我们可以使用快捷语法创建和初始化数组:
示例:创建和初始化整数数组的快捷语法:
![da69ad74f2fc017305e97a7420999b54.png](https://img-blog.csdnimg.cn/img_convert/da69ad74f2fc017305e97a7420999b54.png)
输出output:
![5333b9e5f078c9dd4b12f878164af41f.png](https://img-blog.csdnimg.cn/img_convert/5333b9e5f078c9dd4b12f878164af41f.png)
创建和初始化字符串数组的快捷语法示例:
![e225f8d9396fc2aceff7fdfc3208a4d4.png](https://img-blog.csdnimg.cn/img_convert/e225f8d9396fc2aceff7fdfc3208a4d4.png)
输出output:
![ada81917162689f6a1d878cfa4f9f062.png](https://img-blog.csdnimg.cn/img_convert/ada81917162689f6a1d878cfa4f9f062.png)
创建和初始化雇员数组的快捷语法示例:
![dd27d68f9e8024fd4920e011557bc7ca.png](https://img-blog.csdnimg.cn/img_convert/dd27d68f9e8024fd4920e011557bc7ca.png)
八.Java数组复制示例(四种方式)
让我们看看用Java复制数组的四种不同方法:
使用内置的System.arraycopy()方法复制数组
使用Arrays.copyOf()方法复制数组
使用Object.clone()方法复制数组
使用Array.copyOfRange()方法复制数组
-----------------------------------------------------------------------------------------------使用内置的System.arraycopy()方法复制数组
![78dc2ccea8c1d0fa51e7291d828f2a2e.png](https://img-blog.csdnimg.cn/img_convert/78dc2ccea8c1d0fa51e7291d828f2a2e.png)
使用Arrays.copyOf()方法复制数组
如果要复制数组的前几个元素或数组的完整副本,可以使用此方法。显然它不像System.arraycopy()那样通用,但它也不容易混淆和使用。此方法在内部使用System arraycopy()方法。
import java.util.Arrays;
/**
* This class shows different methods for copy array in java
* @author http://javaguides.net
*
*/
public class JavaArrayCopyExample {
public static void main(String[] args) {
int[] source = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
System.out.println("Source array = " + Arrays.toString(source));
int[] dest = Arrays.copyOf(source, source.length);
System.out.println(
"Copy First five elements of array. Result array = " + Arrays.toString(dest));
}
}
![a436e854e10efe21e1b236e787949757.png](https://img-blog.csdnimg.cn/img_convert/a436e854e10efe21e1b236e787949757.png)
使用Object.clone()方法复制数组
Object类提供clone()方法,由于java中的数组也是一个对象,所以可以使用此方法来实现完整的数组复制。如果需要数组的部分副本,则此方法不适合您。
/**
* This class shows different methods for copy array in java
* @author http://javaguides.net
*
*/
public class JavaArrayCopyExample {
public static void main(String[] args) {
int[] source = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
System.out.println("Source array = " + Arrays.toString(source));
int[] dest = source.clone();
System.out.println(
"Copy First five elements of array. Result array = " + Arrays.toString(dest));
}
}
![169322674b37b453a441556236cad5f4.png](https://img-blog.csdnimg.cn/img_convert/169322674b37b453a441556236cad5f4.png)
使用Array.copyOfRange()方法复制数组
如果要复制数组中的一些元素,而起始索引不是0,则可以使用此方法复制部分数组。同样,这个方法也在使用系统arraycopy方法本身。
/**
* This class shows different methods for copy array in java
* @author http://javaguides.net
*
*/
public class JavaArrayCopyExample {
public static void main(String[] args) {
int[] source = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
System.out.println("Source array = " + Arrays.toString(source));
int[] dest = Arrays.copyOfRange(source, source.length - 3, source.length);
System.out.println(
"Copy First five elements of array. Result array = " + Arrays.toString(dest));
}
}
![815a91c0e4b132bbb32b5e287c676ba6.png](https://img-blog.csdnimg.cn/img_convert/815a91c0e4b132bbb32b5e287c676ba6.png)
九.Java打印数组内容示例
![0bce4dd7d1a72ab786f0f09acbc9371c.png](https://img-blog.csdnimg.cn/img_convert/0bce4dd7d1a72ab786f0f09acbc9371c.png)
![3bf24a8888117f0dac137ed429cd4240.png](https://img-blog.csdnimg.cn/img_convert/3bf24a8888117f0dac137ed429cd4240.png)
输出output:
![76dcf8a5a05ad8eed74fcbd0d850d57f.png](https://img-blog.csdnimg.cn/img_convert/76dcf8a5a05ad8eed74fcbd0d850d57f.png)
建议使用Arrays.toString()来打印数组的内容。
十.Java数组的优势
代码优化:它使代码优化,我们可以很容易地检索或排序数据。
随机存取:我们可以得到任何位于索引位置的数据。
十一.数组的限制
大小限制:我们可以存储数组中元素的唯一固定大小。它在运行时不会变大。为了解决这个问题,在java中使用了集合框架。