Java数组
原创约 2005 字大约 7 分钟...
189-Java数组.md
注意
本博文仅供学术研究和交流参考,严禁将其用于商业用途。如因违规使用产生的任何法律问题,使用者需自行负责。
概念




















示例代码
- 一维数组
package com.tencent.chapter04;
/**
* <p>Title: _01YiWeiArray</p>
* <p>
* Description:
* 一维数组:一种引用类型的数据
特点:1.指定数据类型 2.指定长度
声明:
数据类型 数组名[];
数据类型[] 数组名;
创建:
数组名 = new 数据类型[N];
声明的同时创建:
数据类型 数组名[] = new 数据类型[N];
声明的同时完成初始化:
数据类型 数组名[] = new 数据类型[]{元素1,元素2,元素3.....};
数据类型 数组名[] = {元素1,元素2,元素3.....};
赋值:
数组名[索引] = 值;
取值:
数组名[索引];
属性:
获取数组的元素个数(长度):length
遍历:
for 或者 forEach
----------------------------------------------------------------------------------
数组下标越界异常 java.lang.ArrayIndexOutOfBoundsException
----------------------------------------------------------------------------------
stack(栈):由系统分配,大小限制,先进后出
一般用于保存基本数据类型的局部变量 与 引用类型的地址值
heap(堆):由程序员控制,当使用new关键字时,开辟heap(堆)内存空间,用于保存引用对象的元素的值,先进先出
一般用于保存引用对象的属性值 或 数组的元素值
* </p>
* @author xianxian
* @date 2023年1月3日
*/
public class _01YiWeiArray {
/**
* <p>Title: main</p>
* <p>
* Description:
* </p>
* @param args
*/
public static void main(String[] args) {
//1.声明
int[] intArray = null; //开辟stack(栈)内存空间,用于保存引用对象的地址值
String strArray[];
//System.out.println(intArray.toString());//null 不能使用对象中的任何方法,此时地址为null,空指针异常 java.lang.NullPointerException
//2.创建
intArray = new int[5]; //使用new关键字时,开辟heap(堆)内存空间,用于保存引用对象的元素的值
System.out.println(intArray.toString());//[I@689d6d87
//3.声明的同时创建
int[] intArray2 = new int[4]; //动态初始化:使用new创建数组后,所有元素其实已经被初始化,元素都是默认值
//4.声明的同时完成初始化
int[] intArray3 = new int[]{1,2,3,4,5};
String[] strArray2 = {"a","b","c","d"}; //静态初始化:不使用new,而是在声明数组的同时就完成创建和初始化工作
//5.赋值
int[] intArray4 = new int[4];
intArray4[0] = 11;
intArray4[1] = 22;
intArray4[2] = 33;
intArray4[3] = 44;
//6.取值
System.out.println(intArray4[0]);
System.out.println(intArray4[1]);
System.out.println(intArray4[2]);
System.out.println(intArray4[3]);
//System.out.println(intArray4[4]);//数组下标越界异常 java.lang.ArrayIndexOutOfBoundsException: 4,因为数组的长度为4,下标为0-3之间
//7.属性
System.out.println(intArray4.length);//4
//8.遍历
//使用for循环遍历
for (int i = 0; i < intArray4.length; i++) {
int element = intArray4[i];
System.out.println("数组元素:" + element);
}
//使用forEach遍历
for (int element : intArray4) {
System.out.println("foreach的数组元素:" + element);
}
}
}
- 二维数组
package com.tencent.chapter04;
/**
* <p>Title: _02ErWeiArray</p>
* <p>
* Description:
* </p>
* @author xianxian
* @date 2023年1月3日
*/
public class _02ErWeiArray {
/**
* <p>Title: main</p>
* <p>
* Description:
* 二维数组:在数组中保存数组元素
声明:
数组类型[][] 数组名;
数组类型 数组名[][];
数组类型[] 数组名[];
创建:
数组名 = new 数组类型[R][C];
数组名 = new 数组类型[R][];
数组名 = new 数组类型[][C];----错误
声明的同时创建:
数组类型[][] 数组名 = new 数组类型[R][C];
数组类型[][] 数组名 = new 数组类型[R][];
声明的同时完成初始化:
数组类型[][] 数组名 = new 数组类型[][]{
{元素1,元素2,元素3},
{元素4,元素5,元素6},
{元素7,元素8,元素9},
};
数组类型[][] 数组名 = {
{元素1},
{元素4,元素5,元素6},
{元素7,元素8},
};
* </p>
* @param args
*/
public static void main(String[] args) {
//1.声明
int[][] intArray;
String strArray[][];
double[] doubleArray[];
//2.创建
intArray = new int[3][3]; //三行三列
strArray = new String[3][];//三行N列
//doubleArray = new double[][6];//三行N列 --------错误
//3.声明的同时创建
int[][] intArray2 = new int[4][3];//{ {0,0,0},
// {0,0,0},
// {0,0,0},
// {0,0,0}
//}
//4.声明的同时完成初始化
int[][] intArray3 = new int[][]{
{1,2,3},
{4,5,6},
{7,8,9}
};
int[][] intArray4 = {
{11},
{44,55,66},
{77,88}
};
//5.赋值
int[][] intArray5 = new int[3][4];
//第一行
int[] oneRow = new int[]{1,2,3,4};
intArray5[0] = oneRow;
//第二行
int[] twoRow = new int[]{5,6,7,8};
intArray5[1] = twoRow;
//第三行
int[] threeRow = new int[]{9,10,11,12};
intArray5[2] = threeRow;
int[][] intArray6 = new int[3][4];
//第一行
intArray6[0][0] = 1;
intArray6[0][1] = 2;
intArray6[0][2] = 3;
intArray6[0][3] = 4;
//第二行
intArray6[1][0] = 5;
intArray6[1][1] = 6;
intArray6[1][2] = 7;
intArray6[1][3] = 8;
//第三行
intArray6[2][0] = 9;
intArray6[2][1] = 10;
intArray6[2][2] = 11;
intArray6[2][3] = 12;
//6.取值
System.out.println(intArray6[2][3]);//12
//7.遍历
//方式一
for (int i = 0; i < intArray6.length; i++) {
//每一行的元素
int[] row = intArray6[i];//{1,2,3,4}
//遍历获取每一行的每一列值
for (int j = 0; j < row.length; j++) {
int column = row[j];
System.out.println("第" + i + "行第 " + j + "列的值:" + column);
}
}
//方式二
for (int i = 0; i < intArray6.length; i++) {
//遍历获取每一行的每一列值
for (int j = 0; j < intArray6[i].length; j++) {
int column = intArray6[i][j];
System.out.println("第" + i + "行第 " + j + "列的值:" + column);
}
}
}
}
- 数组的复制与排序
package com.tencent.chapter04;
import java.util.Arrays;
/**
* <p>Title: _03ArrayCopyAndSort</p>
* <p>
* Description:
* </p>
* @author xianxian
* @date 2023年1月3日
*/
public class _03ArrayCopyAndSort {
/**
* <p>Title: main</p>
* <p>
* Description:
* </p>
* @param args
*/
public static void main(String[] args) {
/**
* 数组的复制:System.arraycopy(src, 0, dest, 0, src.length)
* src: 源数组(source)
* srcPos:从源数组开始复制的下标(source position)
*
* dest: 目标数组(destination)
* destPos:从指定的下标开始放置复制的元素(destination position)
*
* length:复制的元素的个数
*
*
* Arrays.toString(数组):将数组按指定格式的字符串输出所有的元素
* Arrays.sort(数组):将数组根据升序排序
*/
int[] src = new int[]{1,2,3,4,5};
int[] dest = new int[]{6,7,8,9,10,11,12,13,14};
System.out.println("复制前src:" + Arrays.toString(src)); //[1, 2, 3, 4, 5]
System.out.println("复制前dest:" + Arrays.toString(dest));//[6, 7, 8, 9, 10, 11, 12, 13, 14]
//System.arraycopy(src, 0, dest, 0, src.length);
System.out.println("复制后src:" + Arrays.toString(src)); //[1, 2, 3, 4, 5]
System.out.println("复制后dest:" + Arrays.toString(dest));//[1, 2, 3, 4, 5, 11, 12, 13, 14]
/**
* 合并两个数组
*/
//0.定义新的数组,长度为src的长度+dest的长度
int[] merge = new int[src.length + dest.length];
//1.复制src数组
System.arraycopy(src, 0, merge, 0, src.length);
System.out.println("复制src之后的merge数组:" + Arrays.toString(merge));//[1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0]
//2.复制dest数组
System.arraycopy(dest, 0, merge, src.length, dest.length);
System.out.println("复制dest之后的merge数组:" + Arrays.toString(merge));//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
/**
* Arrays.sort(array) : 数组的排序
*/
int[] array = new int[]{46,65,1,88,12,6,100};
System.out.println("排序前:" + Arrays.toString(array));//[46, 65, 1, 88, 12, 6, 100]
Arrays.sort(array);//排序,默认升序排序
System.out.println("排序后:" + Arrays.toString(array));//[1, 6, 12, 46, 65, 88, 100]
//降序
for (int i = array.length-1; i >= 0; i--) {
System.out.print(array[i] + " \t");
}
}
}
- 冒泡排序
package com.tencent.chapter04;
import java.util.Arrays;
/**
* <p>Title: _04MaoPaoSort</p>
* <p>
* Description:
* </p>
* @author xianxian
* @date 2023年1月3日
*/
public class _04MaoPaoSort {
/**
* <p>Title: main</p>
* <p>
* Description:
* </p>
* @param args
*/
public static void main(String[] args) {
int[] array = new int[]{5,4,3,2,1};
/*
* 总共N个数组,比较N-1轮
*
* 第 1轮比较:[4, 3, 2, 1, 5] 比较4次
第 2轮比较:[3, 2, 1, 4, 5] 比较3次
第 3轮比较:[2, 1, 3, 4, 5] 比较2次
第 4轮比较:[1, 2, 3, 4, 5] 比较1次
*/
//升序排序
for(int i=1 ; i< array.length;i++)
{
for(int j=0; j<array.length-i;j++)
{
if(array[j] > array[j+1])
{
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
System.out.println("第 " + i + "轮比较:" + Arrays.toString(array));
}
System.out.println("---------------------------------------");
//降序排序
for(int i = 1 ; i< array.length;i++)
{
for(int j = 0; j<array.length - i;j++)
{
if(array[j] < array[j+1])
{
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
System.out.println("第 " + i + "轮比较:" + Arrays.toString(array));
}
}
}
分割线
相关信息
以上就是我关于 Java数组 知识点的整理与总结的全部内容,希望对你有帮助。。。。。。。
扩展知识
分割线
Powered by Waline v2.15.4