本文目录导读:

在Java中查找数组元素有多种方法,我来详细介绍几种常见的方式:
线性查找(最基础)
适用于无序数组,遍历所有元素。
public class LinearSearch {
// 查找元素,返回索引,没找到返回-1
public static int linearSearch(int[] arr, int target) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
return i; // 找到,返回索引
}
}
return -1; // 没找到
}
public static void main(String[] args) {
int[] arr = {5, 2, 8, 1, 9, 3};
int target = 8;
int index = linearSearch(arr, target);
System.out.println("元素 " + target + " 在索引 " + index);
}
}
二分查找(效率高,要求有序数组)
import java.util.Arrays;
public class BinarySearch {
// 手动实现二分查找
public static int binarySearch(int[] arr, int target) {
int left = 0;
int right = arr.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2; // 防止溢出
if (arr[mid] == target) {
return mid; // 找到目标
} else if (arr[mid] < target) {
left = mid + 1; // 在右半部分继续查找
} else {
right = mid - 1; // 在左半部分继续查找
}
}
return -1; // 没找到
}
public static void main(String[] args) {
int[] arr = {1, 3, 5, 7, 9, 11, 13}; // 必须是有序数组
int target = 7;
// 方法1:手动实现
int index1 = binarySearch(arr, target);
System.out.println("二分查找结果:" + index1);
// 方法2:使用Arrays工具类
int index2 = Arrays.binarySearch(arr, target);
System.out.println("Arrays工具类查找:" + index2);
}
}
查找对象数组
import java.util.Arrays;
import java.util.List;
public class ObjectSearch {
static class Student {
String name;
int id;
Student(String name, int id) {
this.name = name;
this.id = id;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj instanceof Student) {
Student other = (Student) obj;
return this.id == other.id;
}
return false;
}
}
public static void main(String[] args) {
// 创建学生数组
Student[] students = {
new Student("张三", 1001),
new Student("李四", 1002),
new Student("王五", 1003)
};
// 方法1:遍历查找
Student target = new Student("李四", 1002);
for (Student s : students) {
if (s.equals(target)) {
System.out.println("找到学生:" + s.name);
}
}
// 方法2:转换为List查找
List<Student> list = Arrays.asList(students);
int index = list.indexOf(target);
System.out.println("索引位置:" + index);
}
}
使用Stream API(Java 8+)
import java.util.Arrays;
import java.util.OptionalInt;
public class StreamSearch {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
int target = 30;
// 查找第一个匹配的元素
OptionalInt result = Arrays.stream(arr)
.filter(x -> x == target)
.findFirst();
if (result.isPresent()) {
System.out.println("找到元素:" + result.getAsInt());
} else {
System.out.println("未找到元素");
}
// 查找所有匹配的元素索引
int[] indices = Arrays.stream(arr)
.filter(x -> x == target)
.toArray();
// 查找满足条件的元素
boolean anyMatch = Arrays.stream(arr)
.anyMatch(x -> x > 40);
System.out.println("是否存在大于40的元素:" + anyMatch);
}
}
实用小工具方法
import java.util.Arrays;
public class SearchUtils {
// 查找元素首次出现的位置
public static int findFirst(int[] arr, int target) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
return i;
}
}
return -1;
}
// 查找元素最后出现的位置
public static int findLast(int[] arr, int target) {
for (int i = arr.length - 1; i >= 0; i--) {
if (arr[i] == target) {
return i;
}
}
return -1;
}
// 统计元素出现次数
public static int countOccurrences(int[] arr, int target) {
int count = 0;
for (int value : arr) {
if (value == target) {
count++;
}
}
return count;
}
// 查找所有匹配的索引
public static int[] findAll(int[] arr, int target) {
return Arrays.stream(arr)
.filter(x -> x == target)
.toArray();
}
// 判断元素是否存在
public static boolean contains(int[] arr, int target) {
return findFirst(arr, target) != -1;
}
}
性能对比和选择建议
- 无序数组:使用线性查找 O(n)
- 有序数组:使用二分查找 O(log n) 或
Arrays.binarySearch() - 频繁查找:考虑使用 HashSet/HashMap 实现 O(1)
- 大数据量:使用二分查找或 Stream 的并行流
实际案例:学生管理系统
import java.util.*;
public class StudentManager {
private List<Map<String, Object>> students = new ArrayList<>();
// 添加学生
public void addStudent(int id, String name, int age) {
Map<String, Object> student = new HashMap<>();
student.put("id", id);
student.put("name", name);
student.put("age", age);
students.add(student);
}
// 按ID查找学生
public Map<String, Object> findById(int id) {
for (Map<String, Object> student : students) {
if ((int) student.get("id") == id) {
return student;
}
}
return null;
}
// 按姓名查找学生
public List<Map<String, Object>> findByName(String name) {
List<Map<String, Object>> result = new ArrayList<>();
for (Map<String, Object> student : students) {
if (student.get("name").equals(name)) {
result.add(student);
}
}
return result;
}
public static void main(String[] args) {
StudentManager manager = new StudentManager();
manager.addStudent(1001, "张三", 20);
manager.addStudent(1002, "李四", 21);
manager.addStudent(1003, "张三", 22);
// 测试查找
Map<String, Object> student = manager.findById(1002);
System.out.println("按ID查找:" + student);
List<Map<String, Object>> students = manager.findByName("张三");
System.out.println("按姓名查找:" + students);
}
}
选择哪种查找方法取决于你的具体需求:数组是否有序、查找频率、数据量大小等因素,希望这些示例对你有帮助!
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播