You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Java将二维数组逆时针旋转45度以拆分后多线程处理?

Java实现二维数组逆时针45度旋转及多线程拆分处理

一、旋转逻辑分析

对于n×n的二维数组,逆时针45度旋转后的结果由原数组中i-j差值相同的元素组成同一行,其中:

  • i为原数组行索引,j为列索引
  • 差值范围从-(n-1)到n-1,共2n-1行
  • 每行元素按原数组的行索引从小到大排列(保证顺序正确)

以输入的3×3数组为例:

1 2 3
4 5 6
7 8 9

旋转后每行对应的i-j差值:

  • 行[3]:i-j=0-2=-2
  • 行[2,6]:i-j=0-1=-1、1-2=-1
  • 行[1,5,9]:i-j=0-0=0、1-1=0、2-2=0
  • 行[4,8]:i-j=1-0=1、2-1=1
  • 行[7]:i-j=2-0=2

二、实现步骤

1. 数组旋转实现

编写方法将原数组转换为旋转后的行集合:

import java.util.ArrayList;
import java.util.List;

public class ArrayRotation {
    // 逆时针45度旋转二维数组
    public static List<List<Integer>> rotateCounterClockwise45(int[][] matrix) {
        int n = matrix.length;
        List<List<Integer>> rotated = new ArrayList<>();
        
        // 初始化所有行的容器
        for (int i = 0; i < 2 * n - 1; i++) {
            rotated.add(new ArrayList<>());
        }
        
        // 遍历原数组,将元素放入对应行
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                // 计算当前元素对应的旋转后行索引:(i - j) + (n - 1)
                int rowIndex = (i - j) + (n - 1);
                rotated.get(rowIndex).add(matrix[i][j]);
            }
        }
        
        return rotated;
    }
}

2. 多线程拆分处理

将旋转后的行集合拆分为两部分,用两个线程分别处理(这里以打印行为例,可替换为自定义处理逻辑):

public class MultiThreadProcessor {
    public static void processInTwoThreads(List<List<Integer>> rotatedRows) {
        int mid = rotatedRows.size() / 2;
        // 第一部分:前mid行
        Runnable task1 = () -> {
            System.out.println("线程1处理内容:");
            for (int i = 0; i < mid; i++) {
                printRow(rotatedRows.get(i));
            }
        };
        
        // 第二部分:剩余行
        Runnable task2 = () -> {
            System.out.println("\n线程2处理内容:");
            for (int i = mid; i < rotatedRows.size(); i++) {
                printRow(rotatedRows.get(i));
            }
        };
        
        // 启动线程
        new Thread(task1).start();
        new Thread(task2).start();
    }
    
    // 辅助方法:打印一行元素
    private static void printRow(List<Integer> row) {
        StringBuilder sb = new StringBuilder();
        for (int num : row) {
            sb.append(num).append(" ");
        }
        System.out.println(sb.toString().trim());
    }
}

3. 测试主方法

public class Main {
    public static void main(String[] args) {
        // 输入示例3×3数组
        int[][] matrix = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };
        
        // 执行旋转
        List<List<Integer>> rotated = ArrayRotation.rotateCounterClockwise45(matrix);
        
        // 打印旋转结果(单线程验证)
        System.out.println("旋转后的完整结果:");
        for (List<Integer> row : rotated) {
            MultiThreadProcessor.printRow(row);
        }
        
        // 多线程拆分处理
        MultiThreadProcessor.processInTwoThreads(rotated);
    }
}

三、运行结果

旋转后的完整输出:

3
2 6
1 5 9
4 8
7

多线程输出(顺序可能因线程调度略有不同):

线程1处理内容:
3
2 6

线程2处理内容:
1 5 9
4 8
7

四、扩展说明

  • 若需处理非方阵(m×n),只需调整行索引计算逻辑:rowIndex = (i - j) + (n - 1) 中的n替换为列数,同时总行数变为m + n - 1
  • 自定义处理逻辑可替换printRow方法,比如求和、过滤等操作
  • 线程拆分方式可按需调整,比如按奇偶行拆分、按元素数量拆分等

内容的提问来源于stack exchange,提问作者IPCZ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 12:18:15