1、在一个数组中找出最大和最小值,并输出它们的位置;
FindMaxAndMinInArray.java1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| public class FindMaxAndMinInArray { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int[] a = new int[10]; System.out.print("输入10个数:"); for (int i = 0; i < a.length; i++) { a[i] = scanner.nextInt(); } int maxIndex = 0, minIndex = 0; for (int i = 1; i < a.length; i++) { if (a[i] > a[maxIndex]) { maxIndex = i; } if (a[i] < a[minIndex]) { minIndex = i; } } System.out.println("最大值:a[" + maxIndex + "]=" + a[maxIndex] + ",最小值:a[" + minIndex + "]=" + a[minIndex]); } }
|
2、冒泡法对一个数组排序;
冒泡排序(Bubble Sort)也是一种简单直观的排序算法。它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。这个算法的名字由来是因为越小的元素会经由交换慢慢”浮”到数列的顶端。
https://www.runoob.com/w3cnote/bubble-sort.html
https://baike.baidu.com/item/%E5%86%92%E6%B3%A1%E6%8E%92%E5%BA%8F#4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
private static void bubbleSortBetter(int[] array) { boolean haveSwap = false; for (int i = 0, temp; i < array.length - 1; i++) { haveSwap = false; for (int j = 0; j < array.length - 1 - i; j++) { if (array[j] > array[j + 1]) { temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; haveSwap = true; } } if (!haveSwap) { break; } } }
|
3、 选择法对数数组排序;
https://zh.wikipedia.org/wiki/%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95
https://www.runoob.com/w3cnote/selection-sort.html
选择排序(Selection sort)是一种简单直观的排序算法。它的工作原理是:
第一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,然后再从剩余的未排序元素中寻找到最小(大)元素,然后放到已排序的序列的末尾。以此类推,直到全部待排序的数据元素的个数为零。
选择排序是不稳定的排序方法。
- 首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置。
- 再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。
- 重复第二步,直到所有元素均排序完毕。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
public static void selectionSort(int[] a) { for (int i = 0, temp; i < a.length - 1; i++) { int min = i; for (int j = i + 1; j < a.length; j++) { if (a[j] < a[min]) { min = j; } } if (min > i) { temp = a[min]; a[min] = a[i]; a[i] = temp; } } }
|
4、把一个十进制数转换成十六进制的数;
https://zh.wikihow.com/%E6%8A%8A%E5%8D%81%E8%BF%9B%E5%88%B6%E6%95%B0%E8%BD%AC%E6%8D%A2%E4%B8%BA%E5%8D%81%E5%85%AD%E8%BF%9B%E5%88%B6%E6%95%B0
和十进制转2进制的做法一样,除以16,取余数,然后逆序排列即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| public class DecimalToHexadecimal {
public static void main(String[] args) { int dividend = 16 + 15; int[] remainders = decimalToHexadecimal(dividend); System.out.println("(" + dividend + ")10 = (" + printHexadecimal(remainders) + ")16"); }
private static int[] decimalToHexadecimal(int dividend) { int[] remainderTemps = new int[20]; int remainderCounter = 0; int quotient; do { quotient = dividend / 16; remainderTemps[remainderCounter++] = dividend % 16; dividend = quotient; } while (dividend > 0); int[] remainders = new int[remainderCounter]; for (int i = 0; i < remainders.length; i++) { remainders[i] = remainderTemps[i]; } return remainders; }
private static String printHexadecimal(int[] remainders) { StringBuffer sb = new StringBuffer(remainders.length); for (int i = remainders.length - 1; i >= 0; i--) { if (remainders[i] < 10) { sb.append(remainders[i]); } else { sb.append((char) ('A' + remainders[i] - 10)); } } return sb.toString(); } }
|
5、实现一个数组的逆序存储
ReverseStoredInTheArray.java1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
public class ReverseStoredInTheArray {
public static void main(String[] args) { int[] a=new int[10]; Scanner scanner=new Scanner(System.in); for (int i = 0; i < a.length; i++) { a[a.length-1-i]=scanner.nextInt(); } for (int i = 0; i < a.length; i++) { System.out.print(a[i]+" "); } } }
|
6、在一个有序的数组插入一个数,也保证有序;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| public class OrderedArrayIsStillOrderedAfterInsertion {
public static void main(String[] args) { int[] a = new int[20]; int[] b = { 1, 5, 9, 18, 24 }; for (int i = 0; i < b.length; i++) { a[i] = b[i]; } System.out.println("有序序列:"); printArray(a);
int toBeInsert;
toBeInsert = 25; System.out.println("要插入的元素:" + toBeInsert); int insetIndex = findInsertIndex(a, b.length - 1, toBeInsert); System.out.println("插入后的位置:" + insetIndex); makeRoomForInsertion(a, b.length, insetIndex); System.out.println("为待插入元素腾出空间:"); printArray(a); a[insetIndex] = toBeInsert; System.out.println("插入后的效果:"); printArray(a); }
private static int findInsertIndex(int[] a, int orderedSequenceEndIndex, int toBeInserted) { int insetIndex = 0; int i = 0; for (; i <= orderedSequenceEndIndex; i++) { if (toBeInserted < a[i]) { insetIndex = i; break; } } if (insetIndex == 0) { insetIndex = i; return insetIndex; } return insetIndex; }
private static void makeRoomForInsertion(int[] a, int lastIndexOfNew, int insetIndex) { if (insetIndex == lastIndexOfNew) { return; } for (int i = lastIndexOfNew + 1, temp; i > insetIndex; i--) { temp = a[i]; a[i] = a[i - 1]; } }
private static void printArray(int[] a) { for (int i = 0; i < a.length; i++) { System.out.print(a[i]); if (i < a.length - 1) System.out.print(","); } System.out.println(); } }
|
7、在一个有序的数组中,利用折半法进行查找;
https://baike.baidu.com/item/%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE
https://zh.wikipedia.org/wiki/%E4%BA%8C%E5%88%86%E6%90%9C%E5%B0%8B%E6%BC%94%E7%AE%97%E6%B3%95
二分查找
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
private static int binarySearch(int[] a, int toFind) { int start = 0; int end = a.length - 1; int middle; while (start <= end) { middle = (start + end) / 2; if (a[middle] == toFind) { return middle; } else if (toFind > a[middle]) { start = middle + 1; } else { end = middle - 1; } } return -1; }
|
测试
1 2 3 4 5 6 7 8 9 10 11
| public static void main(String[] args) { int[] a = { 1, 2, 7, 8, 9, 10, 23, 45, 67, 89, 98, 123, 134 }; int toFind = 9; int index = 0; if ((index = binarySearch(a, toFind)) >= 0) { System.out.println("数组中 存在" + toFind + "这个元素,a[" + index + "]=" + a[index]); } else { System.out.println("数组中 不存在" + toFind + "这个元素"); } }
|
运行结果
8、矩阵的倒置;
水平镜像矩阵
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
private static void mirrorHorizontalMatrix(int[][] a) { int temp; for (int i = 0; i <= (a.length - 1) / 2; i++) { for (int j = 0; j < a[i].length; j++) { temp = a[i][j]; a[i][j] = a[a.length - 1 - i][j]; a[a.length - 1 - i][j] = temp; } } }
|
垂直镜像矩阵
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
private static void mirrorVertical(int[][] a) { int temp; for (int i = 0; i < a.length; i++) { for (int j = 0; j <= (a[0].length - 1) / 2; j++) { temp = a[i][j]; a[i][j] = a[i][a[i].length - 1 - j]; a[i][a[i].length - 1 - j] = temp; } } }
|
测试
1 2 3 4 5 6 7 8 9 10 11
| int[][] a = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; printMatrix(a);
mirrorHorizontalMatrix(a); printMatrix(a);
mirrorVertical(a); printMatrix(a);
|
运行结果
1 2 3 4 5 6 7 8 9 10 11
| |01,02,03| |04,05,06| |07,08,09|
|07,08,09| |04,05,06| |01,02,03|
|09,08,07| |06,05,04| |03,02,01|
|
9、矩阵相加
https://baike.baidu.com/item/%E7%9F%A9%E9%98%B5%E5%8A%A0%E6%B3%95
通常的矩阵加法被定义在两个相同大小的矩阵。两个m×n矩阵A和B的和,标记为A+B,一样是个m×n矩阵,其内的各元素为其相对应元素相加后的值。例如:
$$
\left[\begin{array}{ll}
1 & 3 \\
1 & 0 \\
1 & 2
\end{array}\right]+\left[\begin{array}{ll}
0 & 0 \\
7 & 5 \\
2 & 1
\end{array}\right]=\left[\begin{array}{ll}
1+0 & 3+0 \\
1+7 & 0+5 \\
1+2 & 2+1
\end{array}\right]=\left[\begin{array}{ll}
1 & 3 \\
8 & 5 \\
3 & 3
\end{array}\right]
$$
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
public static boolean isMatrix(int[][] A) { for (int i = 1; i < A.length; i++) { if (A[i].length != A[0].length) { return false; } } return true; }
public static boolean isSameMatrix(int[][] A, int[][] B) { if (!isMatrix(A) || !isMatrix(B)) { return false; } return A.length == B.length && A[0].length == B[0].length; }
public static int[][] matrixAdd(int[][] A, int[][] B) { int[][] C = new int[A.length][A[0].length]; for (int i = 0; i < A.length; i++) { for (int j = 0; j < A[i].length; j++) { C[i][j] = A[i][j] + B[i][j]; } } return C; }
|
测试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public static void main(String[] args) { int[][] A = { { 1, 3 }, { 1, 0 }, { 1, 2 } }; int[][] B = { { 0, 0 }, { 7, 5 }, { 2, 1 } }; MatrixTools.printMatrix(A); System.out.println(" + "); MatrixTools.printMatrix(B);
if (isSameMatrix(A, B)) { int[][] C = MatrixTools.matrixAdd(A, B); System.out.println(" = "); MatrixTools.printMatrix(C); } else { System.out.println("不是相同类型的矩阵,无法相加"); } }
|
运行结果:
1 2 3 4 5 6 7 8 9 10 11
| |1 3| |1 0| |1 2| + |0 0| |7 5| |2 1| = |1 3| |8 5| |3 3|
|
10、矩阵相乘; M*N
N*K
设$A$为$m \times p$的矩阵,$B$为$p \times n$的矩阵,那么称$m \times n$的矩阵$C$位$A$与$B$的乘积,记作$C=AB$,其中矩阵$C$中的第$i$行和第$j$列的元素可以表示为:
$$
(A B)_{i j}=\sum_{k=1}^{p} a_{i k} b_{k j}=a_{i 1} b_{1 j}+a_{i 2} b_{2 j}+\cdots+a_{i p} b_{p j}
$$
如下所示:
$$
A =\left[\begin{array}{lll}
a_{1,1} & a_{1,2} & a_{1,3} \\
a_{2,1} & a_{2,2} & a_{2,3}
\end{array}\right] \\
$$
$$
B =\left[\begin{array}{ll}
b_{1,1} & b_{1,2} \\
b_{2,1} & b_{2,2} \\
b_{3,1} & b_{3,2}
\end{array}\right] \\
$$
$$
C =A B=\left[\begin{array}{ll}
a_{1,1} b_{1,1}+a_{1,2} b_{2,1}+a_{1,3} b_{3,1}, & a_{1,1} b_{1,2}+a_{1,2} b_{2,2}+a_{1,3} b_{3,2} \\
a_{2,1} b_{1,1}+a_{2,2} b_{2,1}+a_{2,3} b_{3,1}, & a_{2,1} b_{1,2}+a_{2,2} b_{2,2}+a_{2,3} b_{3,2}
\end{array}\right]
$$
文字描述
1、当矩阵A的列数(column)等于矩阵B的行数(row)时,A与B可以相乘。
2、矩阵C的行数等于矩阵A的行数,C的列数等于B的列数。
3、乘积C的第m行第n列的元素等于矩阵A的第m行的元素与矩阵B的第n列对应元素乘积之和。
示例
$$
C=A B=\left(\begin{array}{lll}
5 & 2 & 4 \\
3 & 8 & 2 \\
6 & 0 & 4 \\
0 & 1 & 6
\end{array}\right)\left(\begin{array}{ll}
2 & 4 \\
1 & 3 \\
3 & 2
\end{array}\right)=\left(\begin{array}{cc}
24 & 34 \\
20 & 40 \\
24 & 32 \\
19 & 15
\end{array}\right)
$$
$$
\left[\begin{array}{lll}
1 & 2 & 3 \\
4 & 5 & 6
\end{array}\right] \times\left[\begin{array}{cc}
7 & 8 \\
9 & 10 \\
11 & 12
\end{array}\right]=\left[\begin{array}{cc}
58 & 64 \\
139 & 154
\end{array}\right]
$$
交换顺序 结果不一样
$$
\begin{aligned}
&{\left[\begin{array}{ll}
1 & 2 \\
3 & 4
\end{array}\right] \times\left[\begin{array}{ll}
2 & 0 \\
1 & 2
\end{array}\right]=\left[\begin{array}{ll}
4 & 4 \\
10 & 8
\end{array}\right]}
\end{aligned}
$$
$$
\begin{aligned}
&{\left[\begin{array}{ll}
2 & 0 \\
1 & 2
\end{array}\right] \times\left[\begin{array}{ll}
1 & 2 \\
3 & 4
\end{array}\right]=\left[\begin{array}{ll}
2 & 4 \\
7 & 10
\end{array}\right]}
\end{aligned}
$$
参考资料
https://baike.baidu.com/item/%E7%9F%A9%E9%98%B5%E4%B9%98%E6%B3%95
https://www.shuxuele.com/algebra/matrix-multiplying.html
矩阵乘法java代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
public static int[][] matrixMultiplication(int[][] A, int[][] B) { int[][] C = new int[A.length][B[0].length]; int sumIJ = 0; for (int i = 0; i < C.length; i++) { for (int j = 0; j < C[i].length; j++) { for (int k = 0; k < B.length; k++) { sumIJ = sumIJ + A[i][k] * B[k][j]; } C[i][j] = sumIJ; sumIJ = 0; } } return C; }
public static boolean canBeMultiplied(int[][] A, int[][] B) { return MatrixTools.isMatrix(A) && MatrixTools.isMatrix(B) && A[0].length == B.length; }
|
打印矩阵乘法运算
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| private static void printmatrixMul(int[][] A, int[][] B) { MatrixTools.printMatrix(A); System.out.println(" *"); MatrixTools.printMatrix(B); System.out.println(" ="); if (MatrixTools.canBeMultiplied(A, B)) { int[][] C = MatrixTools.matrixMultiplication(A, B); MatrixTools.printMatrixMoreBeautiful(C); } else { System.out.println("不可相乘"); } }
|
其他方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
public static boolean isMatrix(int[][] A) { for (int i = 1; i < A.length; i++) { if (A[i].length != A[0].length) { return false; } } return true; }
|
矩阵打印相关
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
public static void printMatrix(int[][] a) { for (int i = 0; i < a.length; i++) { System.out.print("|"); for (int j = 0; j < a[i].length; j++) { if (j > 0) { System.out.print(" "); } System.out.print(a[i][j]); } System.out.println("|"); } }
public static void printMatrixMoreBeautiful(int[][] a) { int max = findMax(a); int bits = 0; do { max = max / 10; bits++; } while (max > 0); printMatrixFormatted(a, bits); }
private static int findMax(int[][] a) { int max = Integer.MIN_VALUE; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { if (a[i][j] > max) { max = a[i][j]; } } } return max; }
public static void printMatrixFormatted(int[][] a, int numLen) { for (int i = 0; i < a.length; i++) { System.out.print("|"); for (int j = 0; j < a[i].length; j++) { if (j > 0) { System.out.print(" "); } System.out.printf("%0" + numLen + "d", a[i][j]); } System.out.println("|"); } System.out.println(); }
|
测试1
$$
C=A B=\left(\begin{array}{lll}
5 & 2 & 4 \\
3 & 8 & 2 \\
6 & 0 & 4 \\
0 & 1 & 6
\end{array}\right)\left(\begin{array}{ll}
2 & 4 \\
1 & 3 \\
3 & 2
\end{array}\right)=\left(\begin{array}{cc}
24 & 34 \\
20 & 40 \\
24 & 32 \\
19 & 15
\end{array}\right)
$$
1 2 3
| int[][] A = { { 5, 2, 4 }, { 3, 8, 2 }, { 6, 0, 4 }, { 0, 1, 6 } }; int[][] B = { { 2, 4 }, { 1, 3 }, { 3, 2 } }; printmatrixMul(A, B);
|
运行结果
1 2 3 4 5 6 7 8 9 10 11 12 13
| |5 2 4| |3 8 2| |6 0 4| |0 1 6| * |2 4| |1 3| |3 2| = |24 34| |20 40| |24 32| |19 15|
|
测试2
$$
\left[\begin{array}{lll}
1 & 2 & 3 \\
4 & 5 & 6
\end{array}\right] \times\left[\begin{array}{cc}
7 & 8 \\
9 & 10 \\
11 & 12
\end{array}\right]=\left[\begin{array}{cc}
58 & 64 \\
139 & 154
\end{array}\right]
$$
1 2 3
| int[][] A = { { 1, 2, 3 }, { 4, 5, 6 } }; int[][] B = { { 7, 8 }, { 9, 10 }, { 11, 12 } }; printmatrixMul(A, B);
|
运行结果
1 2 3 4 5 6 7 8 9
| |1 2 3| |4 5 6| * |7 8| |9 10| |11 12| = |058 064| |139 154|
|
测试3
$$
\begin{aligned}
&{\left[\begin{array}{ll}
1 & 2 \\
3 & 4
\end{array}\right] \times\left[\begin{array}{ll}
2 & 0 \\
1 & 2
\end{array}\right]=\left[\begin{array}{ll}
4 & 4 \\
10 & 8
\end{array}\right]}
\end{aligned}
$$
$$
\begin{aligned}
&{\left[\begin{array}{ll}
2 & 0 \\
1 & 2
\end{array}\right] \times\left[\begin{array}{ll}
1 & 2 \\
3 & 4
\end{array}\right]=\left[\begin{array}{ll}
2 & 4 \\
7 & 10
\end{array}\right]}
\end{aligned}
$$
1 2 3 4 5 6
| int[][] A = { { 1, 2 }, { 3, 4 } }; int[][] B = { { 2, 0 }, { 1, 2 } }; System.out.println("AB="); printmatrixMul(A, B); System.out.println("BA="); printmatrixMul(B, A);
|
运行结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| AB= |1 2| |3 4| * |2 0| |1 2| = |04 04| |10 08| BA= |2 0| |1 2| * |1 2| |3 4| = |02 04| |07 10|
|
11、打印杨辉三角前20行;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| public static void main(String[] args) { int length = 20; int[][] yh = yangHui(length); printYangHui(yh); }
private static int[][] yangHui(int length) { int[][] yh = new int[length][]; for (int i = 0; i < yh.length; i++) { yh[i] = new int[i + 1]; } for (int i = 0; i < length; i++) { for (int j = 0; j < yh[i].length; j++) { if (j == 0 || i == j) { yh[i][j] = 1; } else { yh[i][j] = yh[i - 1][j] + yh[i - 1][j - 1]; } } } return yh; }
private static void printYangHui(int[][] yh) { int max = yh[yh.length - 1][yh.length / 2]; printYangHuiFormatted(yh, countBits(max)); }
public static void printYangHuiFormatted(int[][] a, int maxNumLength) { for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { if (j > 0) { System.out.print(" "); } for (int k = 0; k < maxNumLength - countBits(a[i][j]); k++) { System.out.print(" "); } System.out.print(a[i][j]); } System.out.println(); } System.out.println(); }
public static int countBits(int num) { int bits = 0; do { num = num / 10; bits++; } while (num > 0); return bits; }
|
运行结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1 1 10 45 120 210 252 210 120 45 10 1 1 11 55 165 330 462 462 330 165 55 11 1 1 12 66 220 495 792 924 792 495 220 66 12 1 1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1 1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1 1 15 105 455 1365 3003 5005 6435 6435 5005 3003 1365 455 105 15 1 1 16 120 560 1820 4368 8008 11440 12870 11440 8008 4368 1820 560 120 16 1 1 17 136 680 2380 6188 12376 19448 24310 24310 19448 12376 6188 2380 680 136 17 1 1 18 153 816 3060 8568 18564 31824 43758 48620 43758 31824 18564 8568 3060 816 153 18 1 1 19 171 969 3876 11628 27132 50388 75582 92378 92378 75582 50388 27132 11628 3876 969 171 19 1
|
13、找出两个数组的交集
数组1的元素有: 21,3,33,89,16;
数组2的元素有: 33,78,15,16,48,57
则结果为:33,16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| public class HW_13_Intersection { public static void main(String[] args) { int[] A = { 21, 3, 33, 89, 16 }; int[] B = { 33, 78, 15, 16, 48, 57 }; int[] C1 = intersection(A, B); ArrayTools.printArray(C1); ArrayTools.printArray(intersection2(A, B)); }
private static int[] intersection(int[] A, int[] B) { int[] C; if (A.length < B.length) { C = new int[A.length]; } else { C = new int[B.length]; } int count = 0; for (int i = 0; i < A.length; i++) { for (int j = 0; j < B.length; j++) { if (A[i] == B[j]) { C[count++] = A[i]; } } } int[] D = new int[count]; for (int i = 0; i < count; i++) { D[i] = C[i]; } return D; }
private static int[] intersection2(int[] A, int[] B) { ArrayList<Integer> list = new ArrayList<Integer>(); for (int i = 0; i < A.length; i++) { for (int j = 0; j < B.length; j++) { if (A[i] == B[j]) { list.add(A[i]); } } } return list.stream().mapToInt(Integer::valueOf).toArray(); } }
|
运行结果