写出所有数组和集合的使用案例,每个数组和集合写5个java案例
时间: 2023-11-28 07:48:20 浏览: 40
1. 数组使用案例
案例1:计算数组元素的总和
```java
int[] arr = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
System.out.println("数组元素的总和为:" + sum);
```
案例2:查找数组中的最大值和最小值
```java
int[] arr = {1, 2, 3, 4, 5};
int max = arr[0], min = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
}
System.out.println("数组中的最大值为:" + max);
System.out.println("数组中的最小值为:" + min);
```
案例3:数组元素排序
```java
int[] arr = {5, 3, 1, 4, 2};
Arrays.sort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
```
案例4:数组元素反转
```java
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0; i < arr.length / 2; i++) {
int temp = arr[i];
arr[i] = arr[arr.length - i - 1];
arr[arr.length - i - 1] = temp;
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
```
案例5:判断数组是否包含某个元素
```java
int[] arr = {1, 2, 3, 4, 5};
int target = 3;
boolean contains = false;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
contains = true;
break;
}
}
System.out.println("数组中是否包含" + target + ":" + contains);
```
2. 集合使用案例
案例1:使用ArrayList添加、删除、查找元素
```java
ArrayList<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");
list.add("orange");
System.out.println(list); // [apple, banana, orange]
list.remove("banana");
System.out.println(list); // [apple, orange]
System.out.println(list.contains("apple")); // true
System.out.println(list.indexOf("orange")); // 1
```
案例2:使用HashSet去重
```java
HashSet<String> set = new HashSet<>();
set.add("apple");
set.add("banana");
set.add("apple");
set.add("orange");
System.out.println(set); // [orange, banana, apple]
```
案例3:使用TreeSet排序
```java
TreeSet<Integer> set = new TreeSet<>();
set.add(5);
set.add(3);
set.add(1);
set.add(4);
set.add(2);
System.out.println(set); // [1, 2, 3, 4, 5]
```
案例4:使用HashMap存储键值对
```java
HashMap<String, Integer> map = new HashMap<>();
map.put("apple", 5);
map.put("banana", 3);
map.put("orange", 4);
System.out.println(map); // {orange=4, banana=3, apple=5}
System.out.println(map.get("banana")); // 3
System.out.println(map.containsKey("watermelon")); // false
```
案例5:使用LinkedList实现队列
```java
LinkedList<Integer> queue = new LinkedList<>();
queue.offer(1);
queue.offer(2);
queue.offer(3);
System.out.println(queue); // [1, 2, 3]
System.out.println(queue.poll()); // 1
System.out.println(queue); // [2, 3]
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)