#Java数组概述
##1.数组的定义
- 数组是相同类型数据的有序集合
- 数组描述的是相同类型的若干个数据,按照一定的先后次序排列组合而成
- 其中,每一个数据乘坐一个数组元素,每个数组元素可以通过一个下标来访问他们
##2.数组声明创建
- 首先必须声明数组变量,才能在程序中使用数组。下面是声明数组变量的语法 如下
int [] age; //首选的方法
int age []; //效果相同,但不是首选方法
- 在Java中使用new操作符来创建数组 如下
int [] age= new int [arraySize];
- 数组的元素是通过索引访问的 数组索引从0开始
- 获取数组的长度:arrays.length
- 如下
public class Practice{
public static void main(String[] args) {
//1.声明一个数组
int[] nums ;
//2.给数组开辟了空间,这里面可以存放10个int类型的数字
nums = new int[10];
//3.给数组元素赋值
nums[0] = 1;
nums[1] = 2;
nums[2] = 3;
nums[3] = 4;
nums[4] = 5;
nums[5] = 6;
nums[6] = 7;
nums[7] = 8;
nums[8] = 9;
nums[9] = 10;
//4.取值
System.out.println(nums[0]);
}
}
##3.数组声明,创建,元素赋值三合一
- 先声明再使用
public class Practice {
public static void main(String[] args) {
int []hobbies;
hobbies=new int[3];
hobbies[0]=1;
hobbies[1]=2;
System.out.println(hobbies[0]);//输出第一个数
System.out.println(hobbies.length);
}
}
- 声明并创建
public class Practice {
public static void main(String[] args) {
int[ ]moenys = new int[3];
moenys[0]=1;
moenys[1]=5;
System.out.println(moenys.length);
System.out.println(moenys[0]);
}
}
- 声明,创建并赋值
public class Practice {
public static void main(String[] args) {
int[]moneys = new int[]{1,2,3};
for(int i=0;i<moneys.length;i++){
System.out.println(moneys[i]);
}
}
}
##4.数组的基本特点
- 数组一旦被创建,它的大小是不能改变的。其长度是确定的
- 数组里的元素必须是相同类型
- 数组中的类型可以是任何类型,包括基本类型和引用类型
- 数组变量属引用类型,数组也可以看成是对象,数组的每个元素相当于该对象成员的变量。数组本身就是对象,Java中对象实在堆中的,因此数组无论保存原始类型还有其他对象类型
- 数组对象本身实在堆中的
##5.数组边界
- 下标的合法区间:[0,length-1]
- ArrayIndexOutOfBoundsException:数组下标越界异常
##6.数组的使用
- for循环
- For-Each循环
- 数组作为方法入参.
- 数组作返回值
如下
public class Practice{
public static void main(String[] args) {
//打印数组内所有元素
int[] arrays = {1,2,3,4,5};
for (int i = 0; i < arrays.length; i++) {
System.out.println(arrays[i]);
}
System.out.println("===============================");
//计算总数和
int sum = 0;
for (int i = 0; i < arrays.length; i++) {
sum = sum + arrays[i]; //sum+=arrays[i];
}
System.out.println(sum);
System.out.println("===============================");
//查找最大元素
int max = arrays[0];
for (int i = 0; i < arrays.length; i++) {
if (arrays[i]>max){
max = arrays[i];
}
}
System.out.println(max);
}
}
public class Practice {
public static void main(String[] args) {
int[] arrays = {1,2,3,4,5};
int[] result = reverse(arrays);
printArray(result);
}
//反转数组
public static int[] reverse(int[] arrays){
//定义一个新数组,长度与arrays长度相同
int[] result = new int[arrays.length];
//反转操作
for (int i = 0,j = result.length-1; i < arrays.length; i++,j--) {
result[j] = arrays[i];
}
//返回result数组
return result;
}
//打印数组元素
public static void printArray(int[] arrays) {
for (int i = 0; i < arrays.length; i++) {
System.out.println(arrays[i]);
}
}
}
##7.多维数组
- 多维数组可以看出数组的数组 ,比如二维数组就是一个特殊的一维数组。其每一个元素都是一个一维数组 ,下面拿二维数组做例子
##8.二维数组的创建与使用
int a[][] = new int[2][5];
public class Practice {
public static void main(String[] args) {
//一维数组
// int[] arrays= {1,2,3,4,5};
//二维数组
int[][] array = {{1,2},{2,3},{3,4},{4,5}};
printArray(array);
}
//打印数组元素
public static void printArray(int[][] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++)
{
System.out.print(array[i][j]);
}
}
}
}
##9.二维数组的初始化的使用
- 静态初始化的使用 如下
public class Practice {
public static void main(String[] args) {
int[][]arr=new int[][]{{1,2,3,9},{4,5},{6,7,8}};
System.out.println(arr.length);//输出行数
System.out.println(arr[0].length);//输出列数
}
}
- 动态初始化的使用 如下
public class Practice {
public static void main(String[] args) {
int[][] arr = new int[][]{{4, 5, 6}, {2, 3, 9}};
int[][] arrtrans = new int[arr[0].length][arr.length];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
arrtrans[j][i] = arr[i][j];
}
}
for (int i = 0; i < arrtrans.length; i++) {
for (int j = 0; j < arrtrans[0].length; j++) {
System.out.println(arrtrans[i][j]);
}
}
}
}
##10.二维数组遍历
- 双重for循环遍历 如下
public class Practice {
public static void main(String[] args) {
int[][] arr = new int[2][3];
//动态创建2个元素(外围数组) 每一个元素中各有三个元素(内围数组)
arr[0] = new int[]{1, 2, 3};
arr[1][0] = 22;//给第2个元素中的第1个元素赋值22
arr[1][1] = 13;//给第2个元素中的第2个元素赋值13
arr[1][2] = 81;//给第2个元素中的第3个元素赋值81
for (int i = 0; i < arr.length; i++) {
//System.out.println(arr[i]);//arr中2个数组的地址
//遍历arr[0]。arr中第1个数组
for (int j = 0; j < arr[i].length; j++) {
System.out.println(arr[i][j] + ",");
}
}
}
}
- 增强for循环foreach遍历
public class Practice {
public static void main(String[] args) {
int[][] arr = {{65,6},{12,1,45,23},{0,9,5}};//静态创建
for(int[] is : arr){
for(int i : is){
System.out.println(i+",");
}
}
}
}
##11.Arrays类
-
数组的工具类java.util.Arrays
-
由于数组对象本身并没有什么方法可以供我们调用,但API中提供了一个工具类Arrays供我们调用,从而进行对数据对象进行一系列的操作
-
JDK文档可查看
-
Arrays类中的方法都是static修饰的静态方法,在使用的时候可以直接使用类名进行调用,而“不用”使用对象来调用(注意是“不用”而不是“不能”)
-
具备以下常用功能:
给数组赋值:fill方法
给数组排序:sort方法(升序)
比较数组:equals方法查找数组元素:binarySearch方法
如下
import java.util.Arrays;
public class Practice {
public static void main(String[] args) {
int[] arrays = {1,2,3,454,87,878,54654,979,113,4654,8789,};
//打印数组
System.out.println(Arrays.toString(arrays));
//数组排序(升序)
Arrays.sort(arrays);
System.out.println(Arrays.toString(arrays));
//数组填充
Arrays.fill(arrays,0);
System.out.println(Arrays.toString(arrays));
Arrays.fill(arrays,2,4,0);
System.out.println(Arrays.toString(arrays));
}
}