java - 如何在java中的for语句中自主切换变量名?

0 java variables

int[] col1 = new int[4];
int[] col2 = new int[4];
int[] col3 = new int[4];
int[] col4 = new int[4];
int reduction;
int x = 1;

for(int i=1; i<=16; i++){
    if(i % 4 == 0){
        x++;
        reduction += 4;
    }
    [col(x)[(i-1)-reduction] = pool[i-1];
}
Run Code Online (Sandbox Code Playgroud)

在 Java 中,我将如何自动更改变量名称,以便不必为每个数组创建单独的 for 循环?我正在尝试用池数组值填充 4 个单独的数组。

Ell*_*sch 7

使用二维数组。并且不要尝试1用作初始数组索引;或初始循环值。

int[][] cols = new int[4][4];
int reduction = 0;
int x = 0;

for (int i = 0; i < 16; i++) {
    if (i != 0 && i % 4 == 0) {
        x++;
        reduction += 4;
    }
    cols[x][i - reduction] = pool[i];
}
Run Code Online (Sandbox Code Playgroud)