实验三
实验名称: 数组与集合
实验目的: 熟悉 JAVA 的集合框架,熟练掌握以下接口和类的使用, Collection, Map,
List,Set,SortedSet, ArrayList, LinkedList, Vector, HashMap, Hashtable 等。
实验时间: (2 学时)
实验类型: 验证型
实验内容:
1. 数组拷贝
CopyArray.java
定义数组 int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }和 b。
(1)将数组
a 中的所以元素拷贝到数组
b 中,打印
b 中元素。(用循环实现)
结果参考:
1,
2,
3,
4,
5,
6,
7,
8,
9,
(2)将数组
a 中从第
3 个元素起连续
5 个元素拷贝到数组
b 中,打印
b 中的元素(用
api 中提供的数组拷贝方法实现)
结果参考:
3,
4,
5,
6,
7
2. 排序 UpSort.java
定义一个数组,例如 int[] a = { 5, 4, 9, 2, 7 }
将数组中的元素按升序或降序排列。
(可以用
sort 方法实现)
3. 定义一个矩阵类 Matrix,具有的功能:转置,相加,按行优先存储,按列优先存储,求鞍点等功能。
(1)CopyArray.java
1 import java.util.Arrays; 2 3 public class CopyArray { 4 public static void main(String[] args) { 5 int[] a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 6 int[] b = new int[a.length]; 7 8 // 将数组 a 中的所有元素拷贝到数组 b 中,用循环实现 9 for (int i = 0; i < a.length; i++) { 10 b[i] = a[i]; 11 } 12 13 // 打印 b 中元素 14 System.out.println(Arrays.toString(b)); 15 16 // 将数组 a 中从第 3 个元素起连续 5 个元素拷贝到数组 b 中,用数组拷贝方法实现 17 int[] c = new int[5]; 18 System.arraycopy(a, 2, c, 0, 5); 19 20 // 打印 b 中的元素 21 System.out.println(Arrays.toString(c)); 22 } 23 }
(2)UpSort.java
1 import java.util.Arrays; 2 3 public class UpSort { 4 public static void main(String[] args) { 5 int[] a = {5, 4, 9, 2, 7,12,-6}; 6 7 // 升序排列 8 Arrays.sort(a); 9 System.out.println("升序排列结果:" + Arrays.toString(a)); 10 11 // 降序排列 12 for (int i = 0; i < a.length / 2; i++) { 13 int temp = a[i]; 14 a[i] = a[a.length - 1 - i]; 15 a[a.length - 1 - i] = temp; 16 } 17 System.out.println("降序排列结果:" + Arrays.toString(a)); 18 } 19 }
(3)
Matrix.java
1 public class Matrix { 2 private int[][] data; 3 4 public Matrix(int rows, int columns) { 5 data = new int[rows][columns]; 6 } 7 8 public Matrix(int[][] data) { 9 this.data = data; 10 } 11 12 // 获取矩阵的行数 13 public int getRowCount() { 14 return data.length; 15 } 16 17 // 获取矩阵的列数 18 public int getColumnCount() { 19 return data[0].length; 20 } 21 22 // 获取矩阵中指定位置的元素 23 public int get(int row, int column) { 24 return data[row][column]; 25 } 26 27 // 设置矩阵中指定位置的元素 28 public void set(int row, int column, int value) { 29 data[row][column] = value; 30 } 31 32 // 转置矩阵 33 public Matrix transpose() { 34 int rows = getRowCount(); 35 int columns = getColumnCount(); 36 Matrix result = new Matrix(columns, rows); 37 for (int i = 0; i < rows; i++) { 38 for (int j = 0; j < columns; j++) { 39 result.set(j, i, get(i, j)); 40 } 41 } 42 return result; 43 } 44 45 // 矩阵相加 46 public Matrix add(Matrix other) { 47 int rows = getRowCount(); 48 int columns = getColumnCount(); 49 Matrix result = new Matrix(rows, columns); 50 for (int i = 0; i < rows; i++) { 51 for (int j = 0; j < columns; j++) { 52 result.set(i, j, get(i, j) + other.get(i, j)); 53 } 54 } 55 return result; 56 } 57 58 // 按行优先存储,返回一维数组 59 public int[] toRowArray() { 60 int rows = getRowCount(); 61 int columns = getColumnCount(); 62 int[] result = new int[rows * columns]; 63 for (int i = 0; i < rows; i++) { 64 for (int j = 0; j < columns; j++) { 65 result[i * columns + j] = get(i, j); 66 } 67 } 68 return result; 69 } 70 71 // 按列优先存储,返回一维数组 72 public int[] toColumnArray() { 73 int rows = getRowCount(); 74 int columns = getColumnCount(); 75 int[] result = new int[rows * columns]; 76 for (int j = 0; j < columns; j++) { 77 for (int i = 0; i < rows; i++) { 78 result[j * rows + i] = get(i, j); 79 } 80 } 81 return result; 82 } 83 84 // 获取矩阵中的鞍点,如果没有鞍点则返回 null 85 public int[] getSaddlePoint() { 86 int rows = getRowCount(); 87 int columns = getColumnCount(); 88 for (int i = 0; i < rows; i++) { 89 int minIndex = 0; 90 int minValue = get(i, 0); 91 for (int j = 1; j < columns; j++) { 92 if (get(i, j) < minValue) { 93 minIndex = j; 94 minValue = get(i, j); 95 } 96 } 97 boolean isSaddlePoint = true; 98 for (int k = 0; k < rows; k++) { 99 if (get(k, minIndex) > minValue) { 100 isSaddlePoint = false; 101 break; 102 } 103 } 104 if (isSaddlePoint) { 105 return new int[]{i, minIndex}; 106 } 107 } 108 return null; 109 } 110 111 // 将矩阵转换为字符串形式,用于打印输出 112 @Override 113 public String toString() { 114 StringBuilder builder = new StringBuilder(); 115 int rows = getRowCount(); 116 int columns = getColumnCount(); 117 for (int i = 0; i < rows; i++) { 118 for (int j = 0; j < columns; j++) { 119 builder.append(get(i, j)); 120 builder.append(' '); 121 } 122 builder.append('\n'); 123 } 124 return builder.toString(); 125 } 126 }
MatrixTest.java
1 import java.util.Arrays; 2 public class MatrixTest { 3 public static void main(String[] args) { 4 Matrix matrix = new Matrix(new int[][]{ 5 {1, 2, 3}, 6 {4, 5, 6}, 7 {7, 8, 9}, 8 }); 9 System.out.println(matrix); 10 System.out.println("Transpose:"); 11 System.out.println(matrix.transpose()); 12 System.out.println("Add:"); 13 System.out.println(matrix.add(new Matrix(new int[][]{ 14 {9, 8, 7}, 15 {6, 5, 4}, 16 {3, 2, 1}, 17 }))); 18 System.out.println("Row Array:"); 19 System.out.println(Arrays.toString(matrix.toRowArray())); 20 System.out.println("Column Array:"); 21 System.out.println(Arrays.toString(matrix.toColumnArray())); 22 System.out.println("Saddle Point:"); 23 System.out.println(Arrays.toString(matrix.getSaddlePoint())); 24 } 25 }