作者:阮晓龙发布时间:2024/9/13 13:11:46
基本信息:
章节名称:智能医学工程专业-计算机程序设计-第07章-数组与字符串
授课教师:信息技术学院互联网技术教学团队
完成时间:2024年09月
适用年级:2023级
适用专业:智能医学工程
文档内容:
讲稿课件(教学版):智能医学工程-计算机程序设计-课件-第07章-数组与字符串-教学版.pdf
讲稿课件(笔记版):智能医学工程-计算机程序设计-课件-第07章-数组与字符串-笔记版.pdf
讲稿课件(在线版):
学习资源:
代码:eg7.1.2
public class ArrayExample { public static void main(String[] args) { int[] numbers = new int[5]; // 声明一个长度为5的整数数组 numbers[0] = 10; // 初始化数组元素 numbers[1] = 20; System.out.println("第一个元素: " + numbers[0]); // 输出第一个元素 } }
代码:eg7.1.3
public class ArrayOperations { public static void main(String[] args) { int[] numbers = {1, 2, 3, 4, 5}; // 初始化数组 for (int i = 0; i < numbers.length; i++) { System.out.println("元素 " + i + ": " + numbers[i]); // 遍历数组 } } }
代码:eg7.2.1
public class ArrayAlgorithms { public static void main(String[] args) { int[] numbers = {10, 20, 30, 40, 50}; int sum = 0; int max = numbers[0]; int min = numbers[0]; for (int num : numbers) { sum += num; if (num > max) max = num; if (num < min) min = num; } double average = (double) sum / numbers.length; System.out.println("平均值: " + average); System.out.println("最大值: " + max); System.out.println("最小值: " + min); } }
代码:eg冒泡排序
public class BubbleSortExample { public static void main(String[] args) { int[] array = {5, 1, 12, -5, 16}; System.out.println("原始数组:"); printArray(array); bubbleSort(array); System.out.println("\n排序后的数组:"); printArray(array); } public static void bubbleSort(int[] array) { int n = array.length; for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - 1 - i; j++) { if (array[j] > array[j + 1]) { // 交换 array[j] 和 array[j + 1] int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } } } public static void printArray(int[] array) { for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } } }
代码:eg选择排序
public class SelectionSortExample { public static void main(String[] args) { int[] arr = {9, 5, 2, 7, 1, 6, 3, 8, 4}; // 待排序的数组 selectionSort(arr); // 调用选择排序方法 System.out.println("Sorted array:"); for (int num : arr) { System.out.print(num + " "); } } public static void selectionSort(int[] arr) { int n = arr.length; for (int i = 0; i < n - 1; i++) { // 找到当前未排序部分的最小元素的索引 int minIndex = i; for (int j = i + 1; j < n; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } // 将找到的最小元素与第一个未排序位置的元素交换 if (minIndex != i) { int temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; } } } }
代码:eg插入排序
public class InsertionSort { /** * 实现插入排序算法 * @param arr 待排序的数组 */ public static void insertionSort(int[] arr) { int n = arr.length; // 从第二个元素开始遍历 for (int i = 1; i < n; ++i) { int key = arr[i]; int j = i - 1; // 将 arr[i] 插入到已排序部分 while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j = j - 1; } arr[j + 1] = key; } } public static void main(String[] args) { // 初始化数组 int[] arr = {12, 11, 13, 5, 6}; // 调用插入排序方法 insertionSort(arr); // 输出排序后的数组 System.out.println("排序后的数组:"); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } }
代码:eg7Stream
import java.util.*; import java.util.stream.Collectors; public class CollectionSortExample { public static void main(String[] args) { List<String> list = Arrays.asList("banana", "apple", "cherry", "date"); // 按字典顺序排序 List<String> sortedList = list.stream() .sorted() .collect(Collectors.toList()); // 按字符串长度降序排序 List<String> sortedByLengthDesc = list.stream() .sorted(Comparator.comparingInt(String::length).reversed()) .collect(Collectors.toList()); System.out.println(sortedList); System.out.println(sortedByLengthDesc); // 创建一个ArrayList并添加一些元素 List<Integer> numbers = new ArrayList<>(); numbers.add(3); numbers.add(1); numbers.add(4); numbers.add(0); numbers.add(5); numbers.add(9); numbers.add(2); // 对集合进行升序排序 Collections.sort(numbers); // 打印排序后的列表(升序) System.out.println("升序排序: " + numbers); // 对集合进行降序排序 Collections.sort(numbers, (o1, o2) -> o2.compareTo(o1)); System.out.println("降序排序: " + numbers); } }
代码:eg7.2.3
import java.util.Arrays; public class ArrayCopy { public static void main(String[] args) { int[] original = {1, 2, 3, 4, 5}; int[] copy = new int[original.length]; //System```java System.arraycopy(original, 0, copy, 0, original.length); System.out.println("原始数组: " + Arrays.toString(original)); System.out.println("复制数组: " + Arrays.toString(copy)); } }
代码:eg7.3.1
public class TwoDimensionalArray { public static void main(String[] args) { 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(); } } }
代码:eg7.3.2
public class IrregularArray { public static void main(String[] args) { int[][] irregularArray = { {1, 2}, {3, 4, 5}, {6} }; for (int i = 0; i < irregularArray.length; i++) { for (int j = 0; j < irregularArray[i].length; j++) { final int k = irregularArray[i][j]; System.out.print(k + " "); } System.out.println(); } System.out.println(); for (int i = 0; i < irregularArray.length; i++) { for (int j = 0; j < irregularArray[i].length; j++) { final int k = irregularArray[i][j]; System.out.print(k + " "); if(k%2 == 0){ System.out.println("偶数"); }else{ System.out.println("奇数"); } } System.out.println(); } } }
代码:eg杨辉三角
public class YangHuiTest { public static void main(String[] args) { //1.声明并初始化二维数组 int[][] yangHui = new int[10][]; //2.给数组的元素赋值 for(int i = 0;i < yangHui.length;i++){ yangHui[i] = new int[i + 1]; //2.1 给首末元素赋值 yangHui[i][0] = yangHui[i][i] = 1; //2.2 给每行的非首末元素赋值 //if(i > 1){ //能去掉的原因:当 i 为 0/1 时,yangHui[0].length - 1 值为 0,因此该内层循环也进不去; for(int j = 1;j < yangHui[i].length - 1;j++){ yangHui[i][j] = yangHui[i-1][j-1] + yangHui[i-1][j]; } //} } //3.遍历二维数组 for(int i = 0;i < yangHui.length;i++){ for(int j = 0;j < yangHui[i].length;j++){ System.out.print(yangHui[i][j] + " "); } System.out.println(); } }
代码:eg7.4.1
public class StringDemo { public static void main(String[] args) { // 声明并初始化一个字符串常量 String greeting = "Hello, World!"; // 打印字符串常量 System.out.println(greeting); // 字符串常量可以直接用于表达式中 String anotherString = "Java is " + "awesome"; System.out.println(anotherString); // 字符串常量可以作为方法的参数 System.out.println(greeting.toUpperCase()); // 将字符串转换为大写 // 字符串常量也可以与变量一起使用 String name = "Alice"; String message = "Hello, " + name + "!"; System.out.println(message); } }
代码:eg7.4.2
public class StringOperations { public static void main(String[] args) { String text = "Java Programming"; System.out.println("长度: " + text.length()); System.out.println("字符 at 5: " + text.charAt(5)); System.out.println("子字符串: " + text.substring(5, 11)); System.out.println("位置: " + text.indexOf("Programming")); System.out.println("替换: " + text.replace("Java", "Python")); System.out.println("分割: " + Arrays.toString(text.split(" "))); } }
代码:eg7.4.3
public class StringModification { public static void main(String[] args) { String str = "Hello"; str = str.concat(" World"); // 创建新字符串 System.out.println(str); // 输出: Hello World } }
软件资源: