数组
数组的基本概念及作用
Java数组:存储一组数据类型相同的数据
在内存中空间是连续的,长度一旦给定,就不能改变了。
数组属于引用数据类型(对象),也可以存储引用数据类型。
数组的创建
数组的声明的两种方式:
数据类型 [] 数组名字 int[] a;
数据类型 数组名字 [] int a[];
注:int[] a,b,c; a,b,c都是数组;
int b[],e,f; b是数组,e,f不是数组;
创建数组:
Int[] ary0 = new int[10];//创建一个长度为10的连续空间。
int[] ary1 = new int[]{1,2,3,4,5};
int[] ary2 = {1,2,3,4,5};
代码:
import java.util.Arrays;
public class Demo01 {
public static void main(String[] args) {
//创建数组方式1,动态创建
int[] a = new int[10];//创建一个长度为10的连续空间。
System.out.println(Arrays.toString(a));
//创建数组方式2
int[] b = new int[]{1,2,3,4,5};
System.out.println(Arrays.toString(b));
//创建数组方式3
int[] c = {6,7,8,9,0};
System.out.println(Arrays.toString(c));
//创建引用数据类型
String[] s = new String[]{"a","b","c"};
System.out.println(Arrays.toString(s));
}
}
运行结果:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 2, 3, 4, 5]
[6, 7, 8, 9, 0]
[a, b, c]
数组的访问与迭代
数组索引(index):就是每一个空间的标号
数组元素索引(下标) a[3] = 3;
索引特点:整数 最小值:0 最大值:长度-1
代码:
import java.util.Arrays;
public class Demo02 {
public static void main(String[] args) {
int[] a = new int[10];
a[0] = 2;//为数组指定空间位置赋值
a[1] = 3;
System.out.println(Arrays.toString(a));
int[] b = {1,2,3,4,5};
System.out.println(b[2]);//获得数组指定位置的值
}
}
运行结果:
[2, 3, 0, 0, 0, 0, 0, 0, 0, 0]
3
数组元素的遍历,使用循环生成数组索引,获取每一个位置的值
代码:
public class Demo03 {
public static void main(String[] args) {
int[] a = {0,1,2,3,4,5,6,7,8};
System.out.println(a.length);
for (int i = 0; i < a.length; i++) {
System.out.println(i);
//System.out.println(a[i]);
if (a[i] == 8) {
System.out.println("包含8");
break;
}
}
}
}
运行结果:
9
0
1
2
3
4
5
6
7
8
包含8
数组排序
冒泡排序:每次拿出两个相邻比较大小,满足条件交换位置
代码:
import java.util.Arrays;
public class Demo04 {
public static void main(String[] args) {
int[] a = {4,5,2,3,1};
//外层循环控制排序次数,循环次数为 a.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];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
System.out.println(Arrays.toString(a));
//字符串效果输出
System.out.println(a);
}
}
运行结果:
[1, 2, 3, 4, 5]
[I@1b6d3586
选择排序:
代码:
import java.util.Arrays;
public class Demo05 {
public static void main(String[] args) {
int[] a = {4,5,3,2,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;
}
}
}
System.out.println(Arrays.toString(a));
}
}
运行结果:
[1, 2, 3, 4, 5]
注:debug 调试功能
断点:开始调试程序的位置
以debug模式启动程序
F8:逐行运行 F9:结束调试,程序运行结束
二维数组
数组的数组,二维数组的每一个元素是一个一维数组
例如: int [][] a = {{1,2,3},{1,2,3},{1,2,3}};
声明 创建
int [][] a = new int[3][4];
int[][] a = new int[][]{{1,2,3},{4,5,6},{7,8,9}};
第一个[]表示二维数组的长度(里面有几个一维数组),第二个[]表示一维数组的长度
new int[3][] 表示二维数组的长度为3,里面一维数组的长度为null,没有创建。
完整表示方法:
int [][] a = new int[3][];
a[0] = new int[4];
a[1] = new int[5];
a[2] = new int[10];
System.out.println(Arrays.toString(a));
int[][] a = new int[][]{{1,2,3},{4,5,6},{7,8,9}};
int[][] b = {{1,2,3},{4,5,6},{7,8,9}};
//输出方法
System.out.println(b[1][2]);
//遍历方法
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b.length; j++) {
System.out.println(b[i][j]+"");
}
System.out.println();
//System.out.println(Arrays.toString(b[i]));
}
}