数组整理
Java数组基础
数组是一种数据结构,可以存储多个同一类型的值。在Java中,数组是对象,我们可以通过 new
操作符创建数组。数组的元素可以通过数组名和索引访问。
1.创建数组的语法:
dataType[] arrayName; //声明
arrayName = new dataType[arraySize]; //初始化
或者更简单的:
dataType[] arrayName = new dataType[arraySize];
例如:
int[] myArray = new int[10];
也可以在声明的时候就指定数组的内容:
int[] myArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
2.遍历数组:
int[] myArray = {1, 2, 3, 4, 5};
for (int i = 0; i < myArray.length; i++) {
System.out.println(myArray[i]);
}
3.计算数组元素的总和和平均值:
int[] myArray = {1, 2, 3, 4, 5};
int sum = 0;
for (int num : myArray) {
sum += num;
}
double average = (double) sum / myArray.length;
System.out.println("Sum = " + sum);
System.out.println("Average = " + average);
4.寻找数组中的最大和最小元素:
int[] myArray = {1, 2, 3, 4, 5};
int max = myArray[0];
int min = myArray[0];
for (int num : myArray) {
if (num > max) {
max = num;
}
if (num < min) {
min = num;
}
}
System.out.println("Max = " + max);
System.out.println("Min = " + min);
5.数组反转:
int[] myArray = {1, 2, 3, 4, 5};
for (int i = 0, j = myArray.length - 1; i < j; i++, j--) {
int temp = myArray[i];
myArray[i] = myArray[j];
myArray[j] = temp;
}
// 打印反转后的数组
for (int num : myArray) {
System.out.print(num + " ");
}
6.查找数组中的特定元素:
int[] myArray = {1, 2, 3, 4, 5};
int target = 3;
for (int i = 0; i < myArray.length; i++) {
if (myArray[i] == target) {
System.out.println("Element found at index " + i);
break;
}
}
7.数组排序:
Java 的 Arrays
类提供了 sort()
方法来对数组进行排序。
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] myArray = {5, 2, 8, 9, 1};
Arrays.sort(myArray);
System.out.println(Arrays.toString(myArray)); // 输出: [1, 2, 5, 8, 9]
}
}
8.数组复制
如果我们想创建一个现有数组的副本,我们可以使用 Arrays.copyOf
方法或 System.arraycopy
方法。
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] original = {1, 2, 3, 4, 5};
int[] copy = Arrays.copyOf(original, original.length);
System.out.println(Arrays.toString(copy)); // 输出: [1, 2, 3, 4, 5]
}
}
9.多维数组
Java支持多维数组,最常见的是二维数组。下面是一个二维数组的例子:
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// 打印出所有的元素
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
10.Java的 Arrays
类
Java有一个内置的 Arrays
类,提供了一些用于操作数组的方法,例如排序和搜索。
public class Main {
public static void main(String[] args) {
int[] nums = {5, 2, 9, 1, 5, 6};
Arrays.sort(nums);
System.out.println(Arrays.toString(nums)); // 输出: [1, 2, 5, 5, 6, 9]
int index = Arrays.binarySearch(nums, 5); // 注意:使用binarySearch前,需要确保数组已排序
System.out.println(index); // 输出: 2 (返回值是5在数组中的索引位置,如果有多个相同元素,返回任意一个)
}
}
11.数组的局限性
需要注意的是,Java的数组在初始化后大小就固定了,不能进行动态扩容。如果需要一个可以动态改变大小的数据结构,应该考虑使用 ArrayList
或其他集合类型。
1.填充数组
Java 的 Arrays
类提供了 fill()
方法,可以快速将数组的所有元素设置为指定的值。
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] nums = new int[10];
Arrays.fill(nums, 5);
System.out.println(Arrays.toString(nums)); // 输出: [5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
}
}
2.转换为列表
使用 Arrays
类的 asList()
方法,可以将数组转换为 List
。
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
Integer[] nums = {1, 2, 3, 4, 5};
List<Integer> list = Arrays.asList(nums);
System.out.println(list); // 输出: [1, 2, 3, 4, 5]
}
}
3.转换为流
Java 8 引入的 Stream API 可以让你更容易地处理数组。你可以使用 Arrays.stream()
方法将数组转换为流,然后进行各种操作。
public class Main {
public static void main(String[] args) {
int[] nums = {1, 2, 3, 4, 5};
int sum = Arrays.stream(nums).sum();
System.out.println(sum); // 输出: 15
}
}
4.数组比较
使用 Arrays.equals()
方法可以比较两个数组的内容是否相同。
public class Main {
public static void main(String[] args) {
int[] nums1 = {1, 2, 3, 4, 5};
int[] nums2 = {1, 2, 3, 4, 5};
System.out.println(Arrays.equals(nums1, nums2)); // 输出: true
}
}
5.数组转字符串
可以使用 Arrays.toString()
方法将数组转换为字符串,方便打印和查看。
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] nums = {1, 2, 3, 4, 5};
System.out.println(Arrays.toString(nums)); // 输出: [1, 2, 3, 4, 5]
}
}
12.多维数组的深度遍历
对于多维数组,我们不仅可以进行简单的遍历,还可以进行深度遍历。下面是一个二维数组的深度遍历例子:
public class Main {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// 使用递归进行深度遍历
traverse(matrix, 0, 0);
}
private static void traverse(int[][] matrix, int i, int j) {
System.out.println(matrix[i][j]);
// 遍历下一行
if (i + 1 < matrix.length) {
traverse(matrix, i + 1, j);
}
// 遍历下一列
if (j + 1 < matrix[i].length) {
traverse(matrix, i, j + 1);
}
}
}
13.使用数组返回多个值
在Java中,一个函数只能返回一个值。但是,我们可以使用数组(或其他容器,如对象或Pair类)来返回多个值。
public class Main {
public static void main(String[] args) {
int[] result = calculate(10, 20);
System.out.println("Sum = " + result[0]);
System.out.println("Difference = " + result[1]);
}
// 计算和和差,并返回
public static int[] calculate(int a, int b) {
int sum = a + b;
int diff = a - b;
return new int[]{sum, diff};
}
}
public class Main {
public static void main(String[] args) {
int[] result = calculateValues();
for (int value : result) {
System.out.println(value);
}
}
public static int[] calculateValues() {
int[] result = new int[3];
result[0] = 1; // 第一个返回值
result[1] = 2; // 第二个返回值
result[2] = 3; // 第三个返回值
return result;
}
}