java中“一维数组和二维数组”涉及到哪些题型?
时间: 2025-06-28 18:04:24 浏览: 5
### Java 中一维数组和二维数组常见题型
#### 一维数组题目示例
在一维数组方面,常见的考题可能涉及初始化、遍历以及简单的算法操作。
**示例1:求最大值**
编写一段代码来找出给定整数数组中的最大元素并打印出来[^1]:
```java
public class MaxValue {
public static void main(String[] args) {
int[] numbers = {3, 7, 2, 8, 5};
int max = numbers[0];
for(int i = 1; i < numbers.length; ++i){
if(numbers[i] > max){
max = numbers[i];
}
}
System.out.println("The maximum value is: " + max);
}
}
```
此段代码展示了如何通过循环迭代整个数组找到其中的最大数值。它先假设第一个元素就是最大的,在后续过程中不断更新这个假定直到完成全部检查为止。
**示例2:反转数组顺序**
另一个典型的一维数组问题是要求实现一个方法用来颠倒数组内元素的位置:
```java
public class ReverseArray {
public static void main(String[] args) {
int[] original = {1, 2, 3, 4, 5};
reverse(original);
// 打印翻转后的数组
for (int num : original) {
System.out.print(num + " ");
}
}
private static void reverse(int[] array) {
int n = array.length;
for (int i = 0; i < n / 2; ++i) {
int temp = array[i];
array[i] = array[n - i - 1];
array[n - i - 1] = temp;
}
}
}
```
这段程序实现了原地交换两个指针对应位置处的数据项从而达到逆转的效果。
#### 二维数组题目示例
对于二维数组而言,则更侧重于行列关系的理解及其应用。
**示例1:计算特定条件下的总和**
考虑这样一个问题——在一个由`0`和`1`构成的矩形网格里统计连续两次出现`1`之间间隔的数量之和[^3]:
```java
public class ConsecutiveOnesSum {
public static void main(String[] args) {
int[][] grid = {{0, 0, 1, 0, 0},
{0, 0, 1, 0, 0},
{0, 0, 1, 0, 1},
{1, 0, 1, 0, 1},
{1, 1, 1, 1, 1}};
calculateConsecutiveOneIntervals(grid);
}
private static void calculateConsecutiveOneIntervals(int[][] matrix) {
int sumOfGapsBetweenTwos = 0;
for (int row = 0; row < matrix.length; ++row) {
boolean foundFirstOneInRow = false;
int gapCounterForRow = 0;
for (int col = 0; col < matrix[row].length; ++col) {
if(matrix[row][col] == 1 && !foundFirstOneInRow){
foundFirstOneInRow = true;
} else if(foundFirstOneInRow && matrix[row][col] != 1){
++gapCounterForRow;
} else if(foundFirstOneInRow && matrix[row][col] == 1){
sumOfGapsBetweenTwos += gapCounterForRow;
gapCounterForRow = 0;
}
}
}
System.out.println("Total gaps between consecutive ones across all rows: "
+ sumOfGapsBetweenTwos);
}
}
```
上述例子中,逻辑上是逐行扫描每一列寻找相邻`1`s之间的距离,并累加这些间距作为最终的结果输出。
**示例2:处理特殊模式的矩阵变换**
还有一类有趣的挑战涉及到对角线的操作,比如将方阵两条主副对角线上各元素增加固定量再求其累积和[^5]:
```java
public class DiagonalElementsIncrementAndSum {
public static void main(String[] args) {
final int INCREMENT_VALUE = 10;
int size = 5;
int[][] squareMatrix = new int[size][size];
initializeSquareMatrix(squareMatrix);
incrementDiagonalsBy(INCREMENT_VALUE, squareMatrix);
displayMatrixWithUpdatedValues(squareMatrix);
computeAndPrintSumOfModifiedDiagonals(squareMatrix);
}
private static void initializeSquareMatrix(int[][] matr) {
for (int r = 0; r < matr.length; ++r) {
for (int c = 0; c < matr[r].length; ++c) {
matr[r][c] = ((r * matr[r].length) + c + 1); // Just an example initialization.
}
}
}
private static void incrementDiagonalsBy(final int incVal, int[][] sqMatr) {
for (int idx = 0; idx < sqMatr.length; ++idx) {
sqMatr[idx][idx] += incVal; // Main diagonal update.
if(idx != sqMatr.length - idx - 1){ // Avoid double counting center element when N is odd.
sqMatr[sqMatr.length - idx - 1][idx] += incVal; // Secondary diagonal update.
}
}
}
private static void displayMatrixWithUpdatedValues(int[][] updatedSqMtr) {
for (int[] row : updatedSqMtr) {
for (int elem : row) {
System.out.printf("%d ", elem);
}
System.out.println();
}
}
private static void computeAndPrintSumOfModifiedDiagonals(int[][] modifiedSqMtx) {
int diagSum = 0;
for (int pos = 0; pos < modifiedSqMtx.length; ++pos) {
diagSum += modifiedSqMtx[pos][pos]; // Add from primary diagonal.
if(pos != modifiedSqMtx.length - pos - 1){ // Skip central point repetition check again here too.
diagSum += modifiedSqMtx[modifiedSqMtx.length - pos - 1][pos]; // From secondary one also.
}
}
System.out.println("\nThe total of both diagonals after modification is:" + diagSum);
}
}
```
这里展示了一个完整的流程,包括创建初始数据结构、修改选定路径上的成员变量值、最后汇总变化前后差异等环节。
阅读全文
相关推荐


















