开发者

How to for loop a certain pattern?

开发者 https://www.devze.com 2023-02-04 08:26 出处:网络
dataValues[0][0] = dataValues_all[2]; dataValues[0][1] = dataValues_all[6]; dataValues[0][2] = dataValues_all[10];
dataValues[0][0] = dataValues_all[2];
dataValues[0][1] = dataValues_all[6];
dataValues[0][2] = dataValues_all[10];
dataValues[0][3] = dataValues_all[14];
dataValues[0][4] = dataValues_all[18];
dataValues[0][5] = dataValues_all[22];
dataValues[0][6] = dataValues_all[26];
dataValues[0][7] = dataValues_all[30];
dataValues[0][8] = dataValues_all[34];
dataValues[0][9] = dataValues_all[38];

dataValues[1][0] = dataValues_all[42];
dataValues[1][1] = dataValues_all[46];
dataValues[1][2] = dataValues_all[50];
dataValues[1][3] = dataValues_all[54];
dataValues[1][4] = dataValues_all[58];
dataValues[1][5] = dataValues_all[62];
dataValues[1][6] = dataValues_all[66];
dataValues[1][7] = dataValues_all[70];
dataValues[1][8] = dataValues_all[74];
dataValues[1][9] = dataValues_all[78];

dataValues[2][0] = dataValues_all[82];
dataValues[2][1] = dataValues_all[86];
dataValues[2][2] = dataValues_all[90];
dataValues[2][3] = dataValues_all[94];
dataValues[2][4] = dataValues_all[98];
dataValues[2][5] = dataValues_all[102];
dataValues[2][6] = dataValues_all[106];
dataValues[2][7] = dataValues_all[110];
dataValues[2][8] = dataValues_all[114];
dataValues[2][9] = dataValues_all[11开发者_运维问答8];

I tried this but it didnt work

/*for(int k=0; k<3; k++){

for(int u=0; u<10;u++){

for(int t=2; t<120; t=t+4){ 

dataValues[k][u] = dataValues_all[t]; }}}*/


for (int k = 0; k < 3; k++) {
    for (int u = 0; u < 10; u++) {
        dataValues[k][u] = dataValues_all[2 + (k * 40) + (u * 4)];
    }
}


int t = 2;
for (int k = 0; k < 3; k++)
    for (int u = 0; u < 10; u++) {
        dataValues[k][u] = dataValues_all[t];
        t = t + 4;
    }

Does it solve your problem?


One loop will also do:

for (int i = 0; i < 30; i++) 
    dataValues[i / 10][i%10] = dataValues_all[i*4+2];

Little clarification:

i/3 is a integer division by 3, so it generates 0,0,0,1,1,1,2,2,2,...

i%3 returns a rest resulting from division by 3, so it gives: 0,1,2,0,1,2,0,1,2


for(int k=0; k<3; k++)
  for(int u=0; u<10;u++)
    dataValues[k][u] = dataValues_all[k*40 + u*4 + 2];


If a nested loop like this isn't working correctly the single best thing you can do when debugging is print out the values of your loop variables and see if they are what you expect. For example:

for(int k=0; k<3; k++) {
    for(int u=0; u<10;u++) {
        for(int t=2; t<120; t=t+4) {
            dataValues[k][u] = dataValues_all[t];
            System.out.println("k = " + k + ", u = " + u + ", t = " + t);
        }
    }
}

See what it does. This will almost certainly show you what the problem is.

Alternatively, use a debugger and step through the loop watching those variables.

EDIT: Other people have given you answers that look correct; my intention here is to describe how to debug similar situations in the future rather than just giving you a specific answer to the problem at hand. I hope you find it useful!


In order for your solution to work, in a single loop two variables must be incremented (u and t). You are doing separate loops for each of these which causes your iterations to look something like this.

dataValues[0][0] = dataValues_all[2];
dataValues[0][0] = dataValues_all[6];
dataValues[0][0] = dataValues_all[10];
dataValues[0][0] = dataValues_all[14];
dataValues[0][0] = dataValues_all[18];
dataValues[0][0] = dataValues_all[22];

The code posted above (by Marc-Francois) looks like the correct solution to your problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消