如何在Java的for循环语句中自动切换变量名称?

huangapple go评论72阅读模式
英文:

How do I autonomously switch variable names in a for statement in java?

问题

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];
}

在Java中,如何自动更改变量名,以便我不必为每个数组编写单独的for循环?我试图使用pool数组的值填充4个单独的数组。

英文:
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&lt;=16; i++){
    if(i % 4 == 0){
        x++;
        reduction += 4;
    }
    [col(x)[(i-1)-reduction] = pool[i-1];
}

In Java how would I go about automatically altering the variable name so that I don't have to make a separate for loop for each array? I'm trying to fill 4 separate arrays with the pool array values.

答案1

得分: 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];
}
英文:

Use a two dimensional array. And don't try to use 1 as an initial array index; or an initial loop value.

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

for (int i = 0; i &lt; 16; i++) {
	if (i != 0 &amp;&amp; i % 4 == 0) {
		x++;
		reduction += 4;
	}
	cols[x][i - reduction] = pool[i];
}

答案2

得分: 0

正如其他人所说,你的代码中有一些语法错误(主要是从1开始而不是从0开始),但我认为在这里最重要的是要学习一下Java中的二维数组(也称为2D数组)是如何工作的!

根据你上面的操作,以下类似的代码可能会有用:

    int[][] cols = new int[4][4];

在Java中,可以通过所谓的“行主序”来访问二维数组。这意味着要访问col1,例如,你可以使用cols[1]。要访问col2,你可以使用cols[2],依此类推。

为了访问二维数组中的内部元素,你可以将其想象成一个坐标系。因为我们通过“行主序”来访问二维数组,所以x和y坐标有点颠倒。这个“图”看起来可能是这样的:

cols = 
[A B C],
[D E F],
[G H I];

=>
cols[0][1] = B //第0行,第1列
cols[1][2] = F //第1行,第2列
英文:

As others have said there are a couple of syntax mistakes with your code above (starting from 1 instead of 0 mostly) but I think the most important thing to learn here is how arrays of array (also known as 2d arrays) work in java!

From what you were doing above something like this would be useful.

    int[][] cols = new int[4][4];

In Java, 2d arrays can be accessed in what is known as "row major order". This means that to access col1 for example, you would do cols[1]. To access col2 you would do cols[2] and so on.

In order to access internal elements from 2d arrays, you can think about it like a coordinate system. Because we access 2d arrays via "row major order", the x and y coordinates are sort of flipped. The "graph" would look something like this:

cols = 
[A B C],
[D E F],
[G H I];

=&gt;
cols[0][1] = B //Row 0, Col 1
cols[1][2] = F //Row 1, Col 2

huangapple
  • 本文由 发表于 2020年9月20日 08:31:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63974526.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定