代码拉取完成,页面将自动刷新
package test_01;
public class test_04_01 {
/* public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int sum = numbers.stream()
.filter(n -> n % 2 == 0) // 过滤偶数 2 4
.map(n -> n * 2) // 将偶数翻倍 4 8
.reduce(0, Integer::sum); // 求和 4 + 8 = 12
System.out.println("处理后的结果:" + sum); // 处理后的结果:12
}*/
/* public static void main(String[] args) {
List<String> fruits = Arrays.asList("Apple", "Banana", "Cherry", "Date", "Elderberry");
// `过滤` 长度大于5的水果
System.out.println("过滤后的水果:");
fruits.stream()
.filter(fruit -> fruit.length() > 5)
.forEach(System.out::println); // 循环输出,打印过滤的结果
// 将水果名称 `转换为大写`
System.out.println("映射后的水果名称:");
fruits.stream()
.map(String::toUpperCase)
.forEach(System.out::println);
// 对水果名称进行 `排序`
System.out.println("按字母顺序排序的水果名称:");
fruits.stream()
.sorted()
.forEach(System.out::println);
// 对水果名称进行 `连接`
System.out.println("所有水果名称连接后的结果:" );
fruits.stream()
.reduce("", (partialResult, fruit) -> partialResult + " " + fruit);
}*/
public static void main(String[] args) {
int[] array = {3, 38, 5, 44, 15, 47, 36, 26, 27, 2, 46, 4, 19, 50, 48};
System.out.println("排序前数组:");
printArray(array);
bubbleSort(array);
System.out.println("\n排序后数组:");
printArray(array);
}
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// 交换 arr[j] 和 arr[j+1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
public static void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。