一、静态数组要分配内存空间
-
语法:type[] ary = new type[number];
- int[] ary; 声明数组是什么类型;内存空间为5的数组;
- ary=new int[5];
- 用法:
public class XueArray {
// 执行程序
public static void main(String[] args) {
int[] arrInt = new int[5]; // 数组长度为5
byte[] arrByte = new byte[5];
String[] arrString = new String[5];
double[] arrDouble = new double[5];
System.out.println("arrDouble[0] = " + arrDouble[0]); // 0.0
System.out.println("arrInt[0] = " + arrInt[0]); // 0
System.out.println("arrString[0] = " + arrString[0]); // null
System.out.println("arrByte[0] = " + arrByte[0]); // 0
}
}
- 第一种
int[] ary ={1,2,3,4} ;
// Arrays.toString(ary) 转化可打印的数组
System.out.println(Arrays.toString(ary)); // 输出 [1,2,3,4]
- 第二种
int[] ary = new int[]{1,2,3,4};
System.out.println(Arrays.toString(ary)); // 输出 [1,2,3,4]
三、创建一个二维数组
int[][] erAry = new int[][]{{1, 2}, {3, 4}}; // [[1,2],[3,4]]
四、数组常见操作方法(Arrays)
- 控制台打印一维数组(Arrays.toString(ary))
int[] ary={1,2,3,4};
System.out.println("ary = " + Arrays.toString(ary));
- 数组拷贝:(System.arraycopy(ary1,index,ary2,index,length))
- System.arraycopy() 新数组和旧数组地址不一样
- ary2=ary1 新数组和就数组地址一样
{
int[] ary = {1, 2, 3, 4};
int[] ary2 = new int[4];
int[] ary3 = new int[4];
ary3 = ary; // ary3根arr是同一个地址
System.arraycopy(ary, 0, ary2, 0, 4);
ary[0] = 10;
System.out.println("ary2 = " + Arrays.toString(ary2)); // [1,2,3,4]
System.out.println("ary3 = " + Arrays.toString(ary3)); // [10,2,3,4]
System.out.println("ary = " + Arrays.toString(ary)); // [10,2,3,4]
}
- 遍历数组
- for循环
- forEach
int[] ary = {1, 2, 3, 4, 5, 6};
for (int i = 0; i < ary.length; i++) {
System.out.println("item = " + ary[i]);
}
for (int item : ary) {
System.out.println("item = " + item);
}
- 替换元素(Arrays.fill())
- 替换所有元素
public static void main(String[] args) {
int[] ary = {1, 2, 3, 4, 5, 6};
Arrays.fill(ary,9);
System.out.println("aryAry = " + Arrays.toString(ary)); // [9,9,9,9,9,9]
}
- 替换单独元素
public static void main(String[] args) {
int[] ary = {1, 2, 3, 4, 5, 6};
Arrays.fill(ary,2,3,9);
System.out.println("aryAry = " + Arrays.toString(ary)); // [1,2,9,4,5,6]
}
- 数组排序(Arrays.sort(ary))
- 字符串是按字典来排序;
- 数字是按从小到大排序
- 复制数组 (Arrays.copyOf(ary,length))
- 复制新的数组不是同一个地址
// 执行程序
public static void main(String[] args) {
int[] ary = {1, 2, 3, 4, 5, 6};
int[] ary1 = Arrays.copyOf(ary, 3);
ary[0]=100;
System.out.println("ary1 = " +Arrays.toString(ary1) ); // [1,2,3]
}
五、基本算法
- 冒泡算法
- 原理:冒泡排序也是一种交换排序算法。冒泡排序的过程,是把数组元素中较小的看作是“较 轻”的,对它进行“上浮”操作。从底部开始,反复地对数组进行“上浮”操作 n 次,最后 得到有序数组。
- 快速排序
https://www.zybuluo.com/xujun94/note/424850 - 选择性排序
public void sort(int[] ary) {
int index;
for (int i = 1; i < ary.length; i++) {
// 先拿出第一项出来
index = 0;
// 判断是否最大值
for (int j = 1; j <= ary.length - i; j++) {
// 如果匹配到最大值把index索引替换了
if (ary[j] > ary[index]) {
index = j;
}
}
int temp = ary[ary.length - i]; // 拿出来最后一项存起来
ary[ary.length - i] = ary[index]; // 最后一项赋值最大的的值
ary[index] = temp; // 最大项的位置给最后一项
}
System.out.println("ary = " + Arrays.toString(ary));
}
- 数组反转
思路:最后一个和第一个换位置,第二个和倒数第二个,所以循坏长度为总长度的一半
public void reverse(int[] ary) {
int len = ary.length;
for (int i = 0; i < len / 2; i++) {
int temp = ary[i]; // 第一项存起来
ary[i] = ary[len - 1 - i]; // 第一项等于最后一项
ary[len - 1 - i] = temp; // 最后一项等于第一项
}
}