数组
数组:相同数据类型元素的集合,是一种线性数据结构,在内存中是连续的 ,本身是引用类型, 但可以存基本类型的 数据,也可存引用类型的数据
1声明、创建
两种声明方式 : 1 数据类型 [ ] 数组名; 2 数据类型 数组名 [ ];
数组的创建: 1 声明的同时根据长度分配内存,其中元素值默认为初始化值 例:int [] c = new int [5];
2 声明的同时分配内存并初始化值 例:int [] e = new int[]{9,5,4,2};
3 第二种的简写:int[] e1 = {9,5,4,2};
public static void main(String[] args) {
/*
数组的两种声明
*/
int a [];
int [] b;
/*
数组的三种创建
*/
int [] c = new int [5];
int [] d = new int[]{};//0个值
int [] e = new int[]{9,5,4,2};
int[] e1 = {9,5,4,2};//e数组的简写
System.out.println(c);//输出的是对象的hashCode值:[I@1b6d3586
System.out.println(Arrays.toString(c));//将对象以字符串形式输出:[0, 0, 0, 0, 0]
/*
数组中存储基本类型,存的是具体值
存储引用类型,存的是对象的引用(地址)
*/
Car car0 = new Car("奔驰0");
Car car1 = new Car("奔驰1");
Car car2 = new Car("奔驰2");
Car car3 = new Car("奔驰3");
//存储引用类型,存的是对象的引用(地址):[com.ff.javaarray.day1.Car@4554617c,
Car [] cars = new Car[]{car0,car1,car2,car3};
System.out.println(Arrays.toString(cars));
}
2数组访问
数组访问: 数组名[索引]
索引 : 1 从0开始
2 数据类型是整形
3 索引最大值和数组长度始终差1
4 区间 0 ~ 数组长度减1
public static void main(String[] args) {
int[] a = new int[]{7,5,2,9,3};
//索引 0 1 2 3 4
System.out.println(a[3]);
//重新赋值 输出
a[4] = 99;
System.out.println(a[4]);
/*
运行会抛出异常,因为索引非法(数组越界);
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at com.ff.javaarray.day1.ArrayDemo2.main(ArrayDemo2.java:21)
*/
System.out.println(a[5]);
}
3数组遍历
/*
数组遍历: 使用for循环(因为区间已知:即数组的索引区间)
*/
public class ArrayDemo3 {
public static void main(String[] args) {
int[] a = new int[]{7,5,2,9,3};
/*for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);//取出数组第i个元素
}*/
/*
foreach循环(jdk5以后):
for (数据类型(和数组中元素的类型一样) 变量 : 要遍历的数组) { }
*/
for (int t:a) {
System.out.println(t);
}
}
}
4二维数组
public static void main(String[] args) {
//声明
int[][] a;
//三种创建方式
int [][] a1 = new int[3][2];
//{{1,2},{1,2},{1,2}}
int [][] a2 = new int[][]{{1,2},{1,2},{1,2}};
int [][] a3 = {{1,2,3},{4,5,6},{7,8,9}};
//遍历(先取出二维数组中的一维数组,再遍历一维数组)
for (int i = 0; i < a3.length; i++) {//取出二维数组中的一维数组
//System.out.println(a3);输出三个一维数组的hashCode值
for (int j = 0; j < a3[i].length; j++) {//遍历一维数组
System.out.print(a3[i][j]+"\t");
}
System.out.println();
}
}
4.1冒泡排序
public class BubbleSort {
private static int [] array = {9,7,3,5,1,4};
//7,3,5,1,4,9
//3,5,1,4,7, 9
//3,1,4,5, 7,9
//1,3,4, 5,7,9
//1,3, 4,5,7,9
//1, 3,4,5,7,9
public static void main(String[] args){
BubbleSort.sort(array);//像方法中传递的是数组的引用(静态方法只能调用静态成员变量)
System.out.println(Arrays.toString(array));
}
/*
顺序排
*/
private static void sort(int[] a){//静态方法只能调用静态成员变量
for (int i = 0; i < a.length; i++) {//比较的趟数
for (int j = 0; j < a.length-1-i; j++) {//一趟比较的次数
int temp;
if (a[j] > a[j+1]){//如果前一个大于后一个,交换位置
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}
}
4.2插入排序
public class InsertSort {
public static void main(String[] args) {
int [] array = {9,7,3,5,1,4};
//7,9
//3,7,9
//3,5,7,9
//1,3,5,7,9
//1,3,4,5,7,9
InsertSort.sort(array);
System.out.println(Arrays.toString(array));
}
public static void sort(int[] a){
for (int i = 0; i < a.length; i++) {
int temp = a[i+1];//(取出)第一个要比较的元素
int pre = i;//第一个要比较的位置
if (temp < a[pre]){
a[i+1] = a[pre];
pre--;
}
a[pre+1] = temp;
}
}
}
4.3选择排序
public class SelectionSort {
public static void main(String[] args) {
int [] array = {9,7,3,5,1,4};
//1,9,7,5,3,4
//1, 3,9,7,5,4
//1,3 4,9,7,5
//1,3,4 5,9,7
//1,3,4,5, 7,9
SelectionSort.sort2(array);
System.out.println(Arrays.toString(array));
}
private static void sort(int[] a){//不优化,每比较一次都要交换一次
for (int i = 0; i < a.length; i++) {//记录排序趟数,选出第次要比较的数
for (int j = i+1; j < a.length; j++) {
if (a[i] > a[j]){
int temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
/*
优化排序方法: 1 先假设第一个是最小的元素
2 把每次比较完后的最小元素的索引记录
3 一次比较结束后把最小索引的元素和第一个交换
*/
private static void sort2(int [] a){
for (int i = 0; i < a.length; i++) {
int minIndex = i;//假设第i(从0开始的)个元素是最小的,记录其下标
for (int j = i+1; j < a.length; j++) {
//for (int j = i+1; j < a.length; j++) { int j = i+1不对,因为还要和第一个比较
if (a[minIndex] > a[j]){//用后面的和最小的比较
minIndex = j;//小于a[minIndex],记录其下标(重新给minTdex赋值)
}
}
int temp;
temp = a[i];
a[i] = a[minIndex];
a[minIndex] = temp;
}
}
}