山西大学计算机与信息技术学院
list[currentMinIndex] = list[i];
list[i] = currentMin;
}
}
}
}
//插入排序
public class InsertionSort {
public static void main(String[] args) {
int[] array = { 7, 4, 3, 9, 0, 6 };
System.out.println("采用插入排序后的数组为:");
insertionSort(array);
for (int i = 0; i < array.length; i++) {
System.out.printf("%2d", array[i]);
}
}
// insert list[i] into a sorted sublist list[0...i-1]
public static void insertionSort(int[] list) {
for (int i = 1; i < list.length; i++) {
int currentElement = list[i];
int k;
for (k = i - 1; k >= 0 && list[k] > currentElement; k--) { list[k + 1] = list[k];
}
list[k + 1] = currentElement;
}
}
}
运行结果贴图:
(2)编写程序实现两个矩阵的相加、相乘。
要求程序运行结果形如如下显示:
Array c
1 2 3
4 5 6
7 8 9
Array d
2 2 2
1 1 1
3 3 3
Array c+d
3 4 5
5 6 7
10 11 12
Array c*d
12 12 12
21 21 21
30 30 30
程序代码:
public class ArrayOfMultiplyAndAdd {
public static void main(String[] args) {
int[][] cArray={{1,2,3},{4,5,6},{7,8,9}};
int[][] dArray={{2,2,2},{1,1,1},{3,3,3}};
System.out.println("Array c");
for(int i=0;i for(int j=0;j System.out.printf("%-3d",cArray[i][j]); } System.out.println(); } System.out.println("Array d"); for(int i=0;i for(int j=0;j System.out.printf("%-3d",dArray[i][j]); } System.out.println(); } System.out.println("Array c+d"); int sum1; for(int i=0;i for(int j=0;j sum1=cArray[i][j]+dArray[i][j]; System.out.printf("%-3d",sum1); } System.out.println(); } System.out.println("Array c*d"); int sum2; for(int i=0;i for(int j=0;j sum2=cArray[i][j]*dArray[i][j]; System.out.printf("%-3d",sum2); } System.out.println(); } } } 运行结果贴图: (3)将用“;”和“,”分割的包含数字字符的字符串“23,21.3,33;34,2,1.9,2.1;3,3,1,3,4,4.9”中的数据解析出来放在一个double类型的二维数组中,以分号分割二维数组的每一行,以逗号分割每行 中的各个元素。(利用String 的split方法) 程序代码: public class SplitString { public static void main(String[] args) { String s = "23,21.3,33;34,2,1.9,2.1;3,3,1,3,4,4.9"; System.out.println("数组" + s + "\n去掉,和;后解析在二维数组中的形式如下:"); String[] Array = s.split(";"); double[][] iArray; iArray = new double[Array.length][]; for (int i = 0; i < Array.length; i++) { String[] temp = Array[i].split(","); iArray[i] = new double[temp.length]; for (int j = 0; j < temp.length; j++) { iArray[i][j] = Double.parseDouble(temp[j]); } } for (int i = 0; i < iArray.length; i++) { for (int j = 0; j < iArray[i].length; j++) { System.out.printf("%-5.1f", iArray[i][j]); } System.out.println(); } } } 运行结果贴图: (4)查看帮助、编写例子 利用System类中的arraycopy()方法复制数组。 分别用Arrays类中的sort方法和binarySearch方法实现数组的排序和折半查找。 程序代码: import java.util.*; (5)随机生成100个小写字母,统计每个字母出现的次数,并显示出来。 (利用Math.random()方法随机产生) 程序代码: public class CountLettersInArray { public static void main(String[] args) { char[] chars = createArray(); System.out.println("The lowercase letters are:"); displayArray(chars); int[] counts = countLetters(chars); System.out.println(); System.out.println("The occurrences of each letter are:"); displayCounts(counts); } public static char[] createArray() { char[] chars = new char[100]; for (int i = 0; i < chars.length; i++) chars[i] = RandomCharacter.getRandomLowerCaseLetter(); return chars; } public static void displayArray(char[] chars) { for (int i = 0; i < chars.length; i++) { if ((i + 1) % 20 == 0) System.out.println(chars[i]); else System.out.printf("%-2c", chars[i]); } } (6)建立一个不规则的二维数组如下,并在控制台显示,数组如下 1 3 5 2 4 6 8 1 9 16 25 36 10 20 30 1 2 3 4 5 6 程序代码: public class Irregular { public static void main(String[] args) { int[][] irregularArray = { { 1, 3, 5 }, { 2, 4, 6, 8 }, { 1, 9, 16, 25, 36 }, { 10, 20, 30 }, { 1, 2, 3, 4, 5, 6 } }; for (int i = 0; i < irregularArray.length; i++) { System.out.println(); for (int j = 0; j < irregularArray[i].length; j++) System.out.printf("%-3d", irregularArray[i][j]); } } } 运行结果贴图: (7)编写两个重载的方法分别交换两个整型变量,和整型数组的第一个和第二个元素,运行并分析结果 程序代码: public class SwapElement { public static void main(String[] args) { int[] testArray = { 88, 99 }; System.out.print("after swap {" + testArray[0] + "," + testArray[1] + "} with swap is {"); swap(testArray[0], testArray[1]); System.out.println(testArray[0] + "," + testArray[1] + "}"); System.out.print("after swap {" + testArray[0] + "," + testArray[1] + "} with swapFirstTwoInArray is {"); swapFirstTwoInArray(testArray); System.out.println(testArray[0] + "," + testArray[1] + "}"); } public static void swap(int i, int j) { int temp = i; i = j; j = temp; } public static void swapFirstTwoInArray(int[] array) { int temp = array[0]; array[0] = array[1]; array[1] = temp; } } 运行结果贴图: 课后作业题 5.16 public class DaysOfYear { public static void main(String[] args) { for (int i = 2000; i <= 2010; i++) { System.out.println("In " + i + " , " + numberOfDaysInAYear(i) + "days"); } } public static int numberOfDaysInAYear(int year) { return isLeapYear(year) ? 366 : 365; } public static boolean isLeapYear(int year) { return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0); } } 5.25 public class ConvertMillis { public static void main(String[] args) { String convert1=convertMillis(5500); String convert2=convertMillis(100000); String convert3=convertMillis(555550000); System.out.println("5500 millis is "+convert1); System.out.println("100000 millis is "+convert2); System.out.println("555550000 millis is "+convert3); } public static String convertMillis(long millis){ long totalSeconds=millis/1000; long currentSecond=totalSeconds%60; long totalMinutes=totalSeconds/60; long currentMinute=totalMinutes%60; long totalHours=totalMinutes/60; long currentHour=totalHours%24; return currentHour+":"+currentMinute+":"+currentSecond; } 6.13 public class ReturnRandom { public static int getRandom(int... numbers) { int m = (int) (1 + Math.random() * 55); int i; for (i = 0; i < numbers.length; i++) { if (m == numbers[i]) { m = (int) (1 + Math.random() * 55); i = -1; } } return m; } public static void main(String[] args) { int[] array = { 2, 3, 5, 6 }; System.out.println("输出数组中的数为:"); for (int i = 0; i < array.length; i++) System.out.print(array[i] + " "); int a = getRandom(array); System.out.println("\n返回的结果是:" + a); } } 7.1 import java.util.*; public class SumMatrix { public static void main(String[] args) { Scanner input = new Scanner(System.in); int[][] array = new int[4][4]; System.out.println("Enter a 4-by-4 matrix row by row:"); for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { array[i][j] = input.nextInt(); } } System.out.println("Sum of the matrix is " + sumMatrix(array)); } public static double sumMatrix(int[][] m) { double sum = 0; for (int i = 0; i < m[0].length; i++) for (int j = 0; j < m.length; j++) { sum += m[i][j]; } return sum; } } } 7.2 import java.util.Scanner; public class SumMajorDoagonal { public static void main(String[] args) { System.out.println("Enter a 4-by-4 matrix row by row"); Scanner input = new Scanner(System.in); int[][] array = new int[4][4]; for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { array[i][j] = input.nextInt(); } } sumMajorDiagonal(array); } public static int sumMajorDiagonal(int[][] m) { int sum = 0; for (int i = 0; i < m.length; i++) { sum += m[i][i]; } System.out.println("Sum of the elements int the major diagonal is " + sum); return sum; } } 7.6 import java.util.Scanner; public class MultiplyMatrix { public static void main(String[] args) { System.out.println("Enter matrix1:"); Scanner input = new Scanner(System.in); double[][] a = new double[3][3]; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { a[i][j] = input.nextDouble(); } } System.out.println("Enter matrix2:"); double[][] b = new double[3][3]; for (int i = 0; i < b.length; i++) { for (int j = 0; j < b[i].length; j++) { b[i][j] = input.nextDouble(); } } multiplyMatrix(a, b); } public static double[][] multiplyMatrix(double[][] a, double[][] b) { double[][] c = new double[3][3]; for (int i = 0; i < c.length; i++) for (int j = 0; j < c[i].length; j++) { c[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j] + a[i][2] * b[2][j]; } for (int i = 0; i < c.length; i++) { if (i == 0 || i == 2) System.out.println(a[i][0] + " " + a[i][1] + " " + a[i][2] + " " + b[i][0] + " " + b[i][1] + " " + b[i][2] + " " + c[i][0] + " " + c[i][1] + " " + c[i][2]); else System.out.println(a[i][0] + " " + a[i][1] + " " + a[i][2] + " * " + b[i][0] + " " + b[i][1] + " " + b[i][2] + " = " + c[i][0] + " " + c[i][1] + " " + c[i][2]); } return c; } } 7.23* import java.util.Scanner; public class Inverse { public static void main(String[] args) { System.out.println("Enter all,a12,a13,a21,a22,a23,a31,a32,a33:"); Scanner input = new Scanner(System.in); double[][] a = new double[3][3]; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { a[i][j] = input.nextInt(); } } inverse(a); } public static double[][] inverse(double[][] A) { double[][] inverse = new double[3][3]; double[][] inverse1 = new double[3][3]; double absolute1, absolute; absolute1 = A[0][0] * A[1][1] * A[2][2] + A[1][0] * A[2][1] * A[0][2] + A[2][0] * A[0][1] * A[1][2] - A[0][2] * A[1][1] * A[2][0] - A[1][2] * A[2][1] * A[0][0] - A[2][2] * A[0][1] * A[1][0]; absolute = (absolute1 > 0 ? absolute1 : -absolute1); for (int i = 0; i < inverse1.length; i++) for (int j = 0; j < inverse1[i].length; j++) { inverse1[j][i] = A[(i + 1) % 3][(j + 2) % 3] * A[(i + 2) % 3][(j + 1) % 3] - A[(i + 1) % 3][(j + 1) % 3] * A[(i + 2) % 3][(j + 2) % 3]; } System.out.println("Inverse Array is:"); if (absolute == 0) System.out.println("null"); for (int i = 0; i < inverse1.length; i++) { for (int j = 0; j < inverse1[i].length; j++) { inverse[i][j] = (1 / absolute) * inverse1[i][j]; if (absolute != 0) System.out.print(inverse[i][j] + " "); } System.out.println(); } return inverse; } }