Arrays类
//一维数组
int [] a = new int [10];
// 二维数组
String[][] s = new String[3][4];
//或 列可以先不写,后面要创建
String[][] s = new String[2][];
s[0] = new String[2];
s[1] = new String[3];
//定义包含 first 属性的类
class MyObject {
int first;
// 构造函数
public MyObject(int first) {
this.first = first;
}
}
public class Main {
public static void main(String[] args) {
// 创建一个包含三个 MyObject 对象的数组
MyObject[] myArray = new MyObject[2];
// 初始化数组中的每个元素
myArray[0] = new MyObject(10);
myArray[1] = new MyObject(20);
myArray[0].first = 12;
// 访问数组中的第一个元素的 first 属性
int value = myArray[0].first;
System.out.println("Value of first in the first element: " + value);
// Value of first in the first element: 12
}
}
JDK 1.5 引进了一种新的循环类型,被称为 For-Each 循环或者加强型循环,它能在不使用下标的情况下遍历数组。
for(type element: array)
{
System.out.println(element);
}
double[] myList = {1.9, 2.9, 3.4, 3.5};
// 打印所有数组元素
for (double element: myList) {
System.out.println(element);
}
一维数组填充
int [] map = new int [5];
Arrays.fill(map,1); // 将 map数组 填充为 1
二维数组填充
int[][] map=new int[4][5];
Arrays.fill(map,-1); // 失败
int[][] map=new int[4][5];
int[] ten=new int[10];
Arrays.fill(ten, -1);
Arrays.fill(map,ten); // 成功
//特别注意:map 的每一项指向的都是同一个一维数组 ten。修改一个会影响其他地址的值 ,
//如:修改 map[0][1] = 100 ,则 map[1][1] ,map[2][1]等都是100.
// 升序
Arrays.sort(int[] a); // 从小到大,升序
Arrays.sort(int[] a, int fromIndex, int toIndex);//对数组部分排序,区间左闭右开
//多维数组
// 先f[0]升序,若f[0]相同,按f[1]降序
Arrays.sort(f, (o1, o2) -> o1[0] == o2[0] ? o2[1] - o1[1] : o1[0] - o2[0]);//排序
建议使用Collections.sort()方法,比Arrays.sort()效率高
public static void sort(int[] arr) {
List<Integer> list = new ArrayList<>();
for (int i : arr) {
list.add(i);
}
Collections.sort(list);
for (int i = 0; i < arr.length; i++) {
arr[i] = list.get(i);
}
}
// 降序
//首先要注意的是不能用int这个类型了,要用Integer,不能使用基本类型(int,double, char)
//如果是int型需要改成Integer,float要改成Float
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
public class Main {
public static void main(String[] args) {
Integer[] arr2={1,4,3,6,7,9,11};
Arrays.sort(arr2, Collections.reverseOrder());
System.out.println(Arrays.toString(arr2));
}
}
// 冒泡排序:降序
public static void sort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
// 如果相邻元素逆序,则交换它们
if (arr[j] < arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
Arrays.equals() 适用于一维数组的比较。
Arrays.deepEquals() 更适合用于多维数组,特别是当数组中包含其他数组时。
例子:
int[] arr1 = {1, 2, 3};
int[] arr2 = {1, 2, 3};
boolean judge = Arrays.equals(arr1, arr2)); // true
int[][] matrix1 = {{1, 2}, {3, 4}};
int[][] matrix2 = {{1, 2}, {3, 4}};
boolean judge =Arrays.deepEquals(matrix1, matrix2); // true
// 方法源代码
public static boolean equals(long[] a, long[] a2) {
if (a.length != a2.length) return false;
for (int i = 0; i < a.length; i++) {
if (a[i] != a2[i]) return false;
}
return true;
}
public static int binarySearch(Object[] a, Object key)
用二分查找算法在数组中搜索给定值的对象(Byte,Int,double等)。数组在调用前必须排序好。如果查找值包含在数组中,则返回索引;否则返回 -1
Arrays.binarySearch(数组, 开始, 结束, 值);
Arrays.binarySearch( arr , 0 , 10 , 5 );在下表为 0 - 9 中搜索
public static void main(String[] args) {
int[] array = {5, 2, 9, 1, 5, 6};
// 对数组进行排序
Arrays.sort(array);
// 要查找的值
int targetValue = -9;
// 使用binarySearch查找值的下标
int index = Arrays.binarySearch(array, targetValue);
System.out.println(index);
}
Arrays.asList()
方法
String[] a = {"apple", "banana", "orange", "grape"};
List<String> list = new ArrayList<>(Arrays.asList(a));
// 不支持结构修改(add、remove 操作)
List<String> list = Arrays.asList(a);