本文将考虑(并且在更大程度上提供独立分析)涉及数组的问题。我们将在以下方面提高我们的技能:
对于所有问题,我们将使用整数数组。
涉及一维数组的问题
继续序列
在这一系列任务中,您将需要:- 确定形成这个或那个数字序列的模式。
- 编写一个函数,将给定序列的前 N 个元素形成为整数数组,并将数组元素显示在屏幕上。
1, 2, 3, 4, 5…
第 1 步:识别模式。这里的一切都是基本的——它是一系列自然数。步骤 2:编写一个函数,以数组的形式形成给定行的前 N 个元素:
public class Main {
public static void main(String[] args) {
System.out.println(Arrays.toString(createArrayOfIntegers(10)));
// Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
}
public static int[] createArrayOfIntegers(int n) {
int[] array = new int[n];
for (int i = 1; i <= n; i++) {
array[i-1] = i;
}
return array;
}
}
同样,有必要编写将继续以下序列的函数:
A. 2, 4, 6, 8, 10...
B. 1, 3, 5, 7, 9...
C. 1, 4, 9, 16, 25...
D. 1, 8, 27, 64, 125...
E. 1, -1, 1, -1, 1, -1...
F. 1, -2, 3, -4, 5, -6...
G. 1, -4, 9, -16, 25....
H. 1, 0, 2, 0, 3, 0, 4....
I. 1, 2, 6, 24, 120, 720...
J. 1, 1, 2, 3, 5, 8, 13, 21…
搜索元素
在这一系列任务中,需要在完成的数组中找到具有某些属性的元素。例如,具有最大值的元素。为了进行演示,让我们为一个函数编写代码,该函数接受一个数组并确定数组中最大元素的索引,然后将该元素及其索引输出到控制台。我还将演示如何使用该类java.util.concurrent.ThreadLocalRandom
快速生成由随机元素组成的给定长度的整数数组:
public class Main {
public static void main(String[] args) {
int[] array = generateRandomIntArray(10);
findMax(array);
/*
Output:
Generated random array: [50551934, -551646189, 410352642, 1822778873, -1744293692, -1140287711, 878876868, -2116893120, -797503442, -703924530]
Max element is [1822778873] with index [3]
*/
}
public static int[] generateRandomIntArray(int n) {
int[] array = ThreadLocalRandom.current().ints().limit(n).toArray();
System.out.println("Generated random array: " + Arrays.toString(array));
return array;
}
public static void findMax(int[] array) {
int maxIndex = 0;
int max = array[maxIndex];
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
maxIndex = i;
}
}
System.out.println(String.format("Max element is [%d] with index [%d]", max, maxIndex));
}
}
现在是任务本身。从随机整数元素数组中,我们需要找到:
- 最大限度。
- 最低限度。
- 平均的。
- 最大和最小元素之间的元素数量。
- 第一个质数。
- 最后一个素数。
- 数组中素数的数量。
排序
在这一系列问题中,您需要使用各种排序算法对整数元素数组进行排序。网上关于各种算法的资料相当多。先尝试理解算法的本质,然后尝试自己实现。首先在纸上(框图、伪代码,任何适合你的东西),然后在 IDEA 中。实现算法:- 冒泡排序;
- 选择排序;
- 插入排序;
- 合并排序。
涉及二维数组的问题
画一个矩阵
在接下来的一系列任务中,需要绘制(输出到控制台)以某种方式组成的二维数组(矩阵):它们必须对应于任务指定的模式。让我举一个例子。我们需要构建并显示一个以下形式的矩阵m*n
(其中m
是行数,n
是行中元素的数量):
1, 2, 3, 4
5, 6, 7, 8
9,10,11,12
让我们编写一个函数来为我们做所有事情,并提供一个函数来将矩阵精美地输出到控制台(您可能会发现它很有用):
public class Main {
public static void main(String[] args) {
createAndDrawMatrix(3, 4);
}
public static void createAndDrawMatrix(int m, int n) {
int[][] matrix = new int[m][n];
int value = 1;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = value;
value ++;
}
}
printMatrix(matrix);
}
public static void printMatrix(int[][] matrix) {
for (int[] array : matrix) {
for (int anInt : array) {
System.out.print(String.format("%3d", anInt));
}
System.out.println();
}
}
}
现在的任务: 显示m*n
以下形式的矩阵: 4, 3, 2, 1 8, 7, 6, 5, 12,11,10, 9 ----------- 1, 4, 7 2, 5, 8 3, 6, 9 ----------- 9, 8, 7 6, 5, 4 3, 2, 1 ----------- 12,11,10, 9 8, 7, 6, 5, 4, 3, 2, 1
搜索元素
在本节中,需要实现对矩阵不同部分中的各种元素的搜索:在特定列中或在特定行中。假设我们有一个以下形式的矩阵: -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
例如,让我们找到第三行中负元素的数量:
public class Main {
public static void main(String[] args) {
int [][] matrix = {
{ -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}
};
findNegativeCountInRow(matrix, 3);
}
private static void findNegativeCountInRow(int[][] matrix, int rowNumber) {
int negativeCount = 0;
for (int element : matrix[rowNumber]){
if (element < 0) {
negativeCount ++;
}
}
System.out.println("Matrix: ");
printMatrix(matrix);
System.out.println(String.format("Has %d negative elements in #%d row ", negativeCount, rowNumber));
}
public static void printMatrix(int[][] matrix) {
for (int[] array : matrix) {
for (int anInt : array) {
System.out.print(String.format("%5d", anInt));
}
System.out.println();
}
}
}
执行main方法后,控制台将输出以下内容:
Matrix:
-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
Has 3 negative elements in #3 row
现在任务。对于任何奇数,n
打印n*n
以下形式的矩阵:
1, -2, 3,
-4, 5, -6,
7, -8, 9
从该矩阵导出:
- 所有负面因素;
- 字符串中的所有负元素
i
; - 该列中的所有负元素
j
; - 所有对角元素(从左上角开始);
- 所有对角元素(从左下角开始)。
海战
最后一部分带星号:没有提示或示例。矩阵是海战游戏编程的理想选择。该游戏的编程阶段之一是将船只放置在比赛场地上。如果你解决了一些这样的问题,那么你就可以放心地应对海战了。因此,给定一个由零组成的 10x10 矩阵。假设这是一个进行海战的场地。在此字段中,0 是空单元格,1 是一艘船或船的一部分。编写函数,每个函数按照海战游戏的规则,随机放置:- 10艘单甲板船;
- 10个双层珊瑚;
- 5艘三层甲板船;
- 安排海战比赛的所有舰船(4艘单甲板、3艘双层、2艘三甲板、1艘四甲板)。
GO TO FULL VERSION