dataType[] arrayRefVar;
dataType arrayRefVar[]; // 也可以,但不推荐
new
操作符来创建数组dataType[] arrayRefVar = new dataType[arraySize];
arrays.length
// 1. 声明数组,此时array存放在栈中
int[] array = null;
// 2. 创建数组(初始化),此时在堆中开辟了内存,存放对象
array = new int[10];
// 3. 给数组中元素赋值,此时修改对应的值
array[0] = 1;
int[] a = {1, 2, 3};
Man[] mans = {new Man(1,1), new Man(2,3)};
int[] a = new int[2];
a[0] = 1;
a[1] = 2;
数组的特点
数组边界
[0, length-1]
ArrayIndexOutOfBoundsException
小结
for-each
循环for (int array : arrays){
}
pbulic void printArray(int[] arrays){
}
public static int[] reverseArray(int[] arrays){
}
int[][] a = new int[2][5];
java.util.Arrays
Arrays
工具类,对数组对象进行一些基本操作static
修饰的静态方法,使用时可以直接使用类名进行调用,无需使用对象调用(也可以)。fill
方法sort
方法,升序排列equals
方法,比较元素值binarySearch
方法,对排序好的数组进行二分查找package com.kuang.arrays;
import java.util.Arrays;
/**
* Arrays工具类
*
* @author maple_w
* Created on 21/07/11 011 16:32
*/
public class Demo01_Arrays {
public static void main(String[] args) {
int[] a = {1,3,4,5,22,34566,123};
System.out.println(a); // [I@1b6d3586
// 打印数组元素, Arrays.toString()
System.out.println(Arrays.toString(a)); // [1, 3, 4, 5, 22, 34566, 123]
// 升序排序, Arrays.sort()
Arrays.sort(a);
System.out.println(Arrays.toString(a)); // [1, 3, 4, 5, 22, 123, 34566]
// 填充数组, Arrays.fill()
Arrays.fill(a, 2,4, 0);
System.out.println(Arrays.toString(a)); // [1, 3, 0, 0, 22, 123, 34566]
}
}
两层循环,外层冒泡轮数,里层依次比较;
嵌套循环,时间复杂度为 \(O(n^2)\)。
package com.kuang.arrays;
import java.util.Arrays;
/**
* 冒泡排序
* 比较数组中两个相邻元素,如果第一个比第二个大,交换位置
* 每次比较都会产生一个最大或最小的数字
* 下一轮可以少一次排序
* 依次循环,直到结束
*
* @author maple_w
* Created on 21/07/11 011 16:42
*/
public class Demo02_BubbleSort {
public static void main(String[] args) {
int[] a = {1, 5, 3, 2, 6, 7, 213, 21};
int[] sortesd = bubbleSort(a);
System.out.println(Arrays.toString(sortesd)); // [1, 2, 3, 5, 6, 7, 21, 213]
}
public static int[] bubbleSort(int[] array) {
// 临时变量
int temp = 0;
// 外层循环,判断要走多少次
for (int i = 0; i < array.length - 1; i++) {
// 优化:通过flag标识减少无意义比较
boolean flag = false;
// 内层循环,比较判断两个数,如果第一个数更大,交换位置
for (int j = 0; j < array.length - 1 - i; j++) {
if (array[j + 1] < array[j]) {
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
flag = true;
}
}
if (flag == false) {
break;
}
}
return array;
}
}
package com.kuang.arrays;
import java.util.Arrays;
/**
* 稀疏数组
*
* @author maple_w
* Created on 21/07/11 011 16:52
*/
public class Demo03_SparseArray {
public static void main(String[] args) {
// 二维数组表示棋盘
int[][] array1 = new int[11][11];
array1[1][2] = 1;
array1[2][3] = 2;
// 0 0 0 0 0 0 0 0 0 0 0
// 0 0 1 0 0 0 0 0 0 0 0
// 0 0 0 2 0 0 0 0 0 0 0
// 0 0 0 0 0 0 0 0 0 0 0
// 0 0 0 0 0 0 0 0 0 0 0
// 0 0 0 0 0 0 0 0 0 0 0
// 0 0 0 0 0 0 0 0 0 0 0
// 0 0 0 0 0 0 0 0 0 0 0
// 0 0 0 0 0 0 0 0 0 0 0
// 0 0 0 0 0 0 0 0 0 0 0
// 0 0 0 0 0 0 0 0 0 0 0
System.out.println("打印棋盘");
for (int[] ints : array1) {
for (int anInt : ints) {
System.out.print(anInt + "\t");
}
System.out.println();
}
// 获取有效值个数
int sum = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (array1[i][j] != 0) {
sum++;
}
}
}
System.out.println("有效值的个数:" + sum); // 2
// 使用稀疏数组存储
int[][] array2 = new int[sum + 1][3];
array2[0][0] = 11;
array2[0][1] = 11;
array2[0][2] = sum;
int count = 0;
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array1[i].length; j++) {
if (array1[i][j] != 0) {
count++;
array2[count][0] = i;
array2[count][1] = j;
array2[count][2] = array1[i][j];
}
}
}
// 输出稀疏数组
// 11 11 2
// 1 2 1
// 2 3 2
System.out.println("稀疏数组:");
for (int i = 0; i < array2.length; i++) {
System.out.println(array2[i][0] + "\t" + array2[i][1] + "\t" + array2[i][2]);
}
// 还原棋盘
System.out.println("还原棋盘");
// 读取稀疏数组
int[][] array3 = new int[array2[0][0]][array2[0][1]];
// 还原值
for (int i = 1; i < array2.length; i++) {
array3[array2[i][0]][array2[i][1]] = array2[i][2];
}
// 打印棋盘
for (int[] ints : array3) {
for (int anInt : ints) {
System.out.print(anInt + "\t");
}
System.out.println();
}
// 0 0 0 0 0 0 0 0 0 0 0
// 0 0 1 0 0 0 0 0 0 0 0
// 0 0 0 2 0 0 0 0 0 0 0
// 0 0 0 0 0 0 0 0 0 0 0
// 0 0 0 0 0 0 0 0 0 0 0
// 0 0 0 0 0 0 0 0 0 0 0
// 0 0 0 0 0 0 0 0 0 0 0
// 0 0 0 0 0 0 0 0 0 0 0
// 0 0 0 0 0 0 0 0 0 0 0
// 0 0 0 0 0 0 0 0 0 0 0
// 0 0 0 0 0 0 0 0 0 0 0
}
}
原文:https://www.cnblogs.com/maple-w/p/14998982.html