0

I have a problem I'm not sure how to solve. I need to change the name of the array being used in the loop to the next consecutive one. ie array xor1[] in first iteration then xor2[] in next and so on.

int xor1[] = {0,1,1,0,1,1};
int xor2[] = {0,1,1,1,0,1};

for(int ii = 0; ii < 2; ii++)
{
    int[] row = new int[2];

    //xor1 in next iteration should be xor2???
    row[0] = xor1[0];
    row[1] = xor1[5];
}

Note: there is far more than 2 iterations this is just for simplicity.

2
  • You can"t dynamiclly create variable, that's why we use arrays. How will you use ii, where will be stored value of xor2 you want ? Commented Apr 13, 2020 at 7:38
  • 1
    It seems like you need a 2D array. int[][] xor = {{0,1,1,0,1,1}, {0,1,1,1,0,1}}; Commented Apr 13, 2020 at 7:38

1 Answer 1

3

Create an array of the arrays, then iterate that.

int[] xor1 = {0,1,1,0,1,1};
int[] xor2 = {0,1,1,1,0,1};

int[][] xors = {xor1,xor2};
for (int[] xor : xors) {

    int[] row = new int[2];
    row[0] = xor[0];
    row[1] = xor[5];
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.