1.数组的基本概念及作用
- 数组是相同数据类型元素的集合
- 数组本身是引用数据类型,即对象。但是数组可以存储基本数据类型,也可以存储引用数据类型。
2.数组的创建
- 数组的声明的两种方式:
数据类型 [] 数组名字 例如:int [] a;
(常用)
数据类型 例如:int a [];
- 数组创建的三种方式:
声明数组的同时,根据指定的长度分配内存,但数组中元素值都为默认的初始化值:
int[] ary0 = new int[10];
声明数组并分配内存,同时将其初始化
int[] ary1 = new int[]{1, 2, 3, 4, 5};
与前一种方式相同,仅仅只是语法相对简略
int[] ary2 = {1, 2, 3, 4,5};
- 从另一个角度,数组创建可以分为动态和静态两种。
动态创建数组(没有为元素赋值,可以结合for循环进行赋值)
char[] chAry = new char[10];
静态创建数组,在创建的时候,即为每个元素赋初值
int[] ary1 = new int[]{1, 2, 3, 4, 5};
数组的长度:length属性
int [] b1 = new int []{1,2,3,4,5,6,7};
System.out.println(b1.length);
public class Demo1 {
public static void main(String[] args) {
//声明数组
int[] a;
//创建数组方式1,动态创建
int[] b = new int[10];
System.out.println(Arrays.toString(b));
//方式2
char x = 'a';//97
int[] c = new int[]{1, 2, 3, 4, 5, x};
System.out.println(Arrays.toString(c));
//方式3
int[] d = {1, 2, 3, 4, 5};
System.out.println(Arrays.toString(d));
}
}
public class Demo2 {
public static void main(String[] args) {
/*
数组元素的访问
*/
int[] a = new int[5];
//数组元素的赋值
a[0] = 1;
System.out.println(a[0]);
}
}
3.数组的访问与迭代
- 数组元素的访问:
数组名字[索引] 例如:a[0],a[1];
注意:
(1)数组的索引从0开始。
(2)索引的数据类型是整型
(3)索引最大值和数组长度始终差1 - 数组迭代的两种方式:
第一种:for循环
int [] b = new int []{1,2,3,4,5,6,7};
//数组的迭代
for (int i = 0; i < b.length; i++) {
System.out.println(b[i]);
}
第二种:增强for循环
int [] b1 = new int []{1,2,3,4,5,6,7};
for(数组元素的类型 临时变量名字 :数组的名字){
System.out.println(临时变量名字 );
}
即:
int[] b = new int[]{1, 2, 3, 4, 5, 6, 7};
for (int x : b) {
System.out.println(x);
}
public class Demo3 {
/*
数组遍历
*/
public static void main(String[] args) {
int[] a = new int[5];
for (int i = 0; i <= 4; i++) {
a[i] = i + 2;
}
System.out.println(Arrays.toString(a));
//方式1 for循环
int[] b = {1, 2, 3, 4, 5};
for (int i = 0; i < b.length; i++) {
System.out.println(b[i]);
}
//方式2 增强for循环
for (int t : b) {
System.out.println(t);
}
}
}
4.数组排序
- 冒泡排序
import java.util.Arrays;
public class BubbleSort {
/*
冒泡排序:每次拿出两个相邻的比较大小,满足条件交换位置。
3,4,2,5,1
3 2 4 1 5 1
2 3 1 4 5 2
2 1 3 4 5 3
1 2 3 4 5 4
*/
public static void main(String[] args) {
int[] a = {3, 4, 2, 5, 1};
//外层循环控制排序的次数length-1次
for (int i = 0; i < a.length - 1; i++) {
//内层循环控制相邻元素比较
for (int j = 0; j < a.length - 1 - i; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j + 1];
a[j + 1] = a[j];
a[j] = temp;
}
}
}
System.out.println(Arrays.toString(a));
}
}
- 选择排序
public class SelectSort1 {
public static void main(String[] args) {
/*
选择排序:
3,4,2,5,1
1 4 3 5 2 1
1 2 4 5 3 2
1 2 3 5 4 3
1 2 3 4 5 4
*/
int[] a = {3, 4, 2, 5, 1};
//外层循环控制排序的次数从第0个到第length-1
for (int i = 0; i < a.length - 1; i++) {
//循环制造比较的数,从i+1到最后一个
for (int j = i + 1; j < a.length; j++) {
if (a[i] > a[j]) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
}
- 选择排序优化
import java.util.Arrays;
public class SelectSort2 {
public static void main(String[] args) {
/*
选择排序优化:
3,4,2,5,1
*/
int[] a = {3, 4, 2, 5, 1};
//外层循环控制排序的次数从第0个到第length-1
for (int i = 0; i < a.length - 1; i++) {
//循环制造比较的数,从i+1到最后一个
int minIndex = i;
for (int j = i + 1; j < a.length; j++) {
if (a[minIndex] > a[j]) {
minIndex = j;//记录较小的值的位置
}
}
int temp = a[minIndex];
a[minIndex] = a[i];
a[i] = temp;
}
System.out.println(Arrays.toString(a));
}
}
5.二维数组
- 二维数组的定义:数组的数组——二维数组的每一个元素是一个一维数组。
例如:
int[][] a = {{1, 2, 3}, {1, 2, 3}, {1, 2, 3}};
- 二维数组的声明:
int [][] a;
int a2[][];
- 二维数组的创建:
int[][] a = new int[][]{{1, 2, 3}, {1, 2, 3}, {1, 2, 3}};
int[][] b = {{1, 2, 3}, {1, 2, 3}, {1, 2, 3}};
int[][] c = new int[3][5];
例如:int[][] arr = new int[3][5];
定义了一个整型的二维数组 ,这个二维数组有3 个一维数组,每一个一维数组包含5个元素.
- 二维数组的迭代:
int[][] b = new int[][]{{1, 2}, {3, 4}};
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[i].length; j++) {
System.out.print(b[i][j]);
}
}
import java.util.Arrays;
public class Demo1 {
/*
二维数组:数组中的每一个元素又是一个数组
*/
public static void main(String[] args) {
/*
声明 创建
2表示二维数组的长度(里面有几个一维数组) 3表示一维数组的长度
{{1, 2, 3}, {4, 5, 6}}
0 1 2
0 1
*/
int[][] a = new int[2][3];
int[][] b = new int[][]{{1, 2, 3}, {4, 5, 6}};
int[][] c = {{1, 2, 3}, {4, 5, 6}};
//new int[3][];表示二维数组的长度是3,里面的一维数组为null,没有创建,运行时根据需要创建一维数组
int[][] d = new int[3][];
d[0] = new int[3];
d[1] = new int[4];
d[2] = new int[5];
System.out.println(Arrays.toString(d));
/*
对二维数组进行赋值
*/
int[][] e = {{1, 2, 3}, {4, 5, 6}};
System.out.println(e[0][1]);
e[0][1] = 7;
System.out.println(e[0][1]);
/* 遍历二维数组*/
for (int i = 0; i < e.length; i++) {
for (int j = 0; j < e[i].length; j++) {
System.out.print(e[i][j] + " ");
}
System.out.println();
}
}
}