Array with 2 of the same value

Hi,

Please, edit your post and format your code. It is all explained here: Guidelines—Tips on Asking Questions
EDIT: Thanks :slight_smile:

The problem with your code is that your are looping one increment at a time so even if inside your loop you assign the 2 same values you erase that value in the second increment. Since example speaks more than words:

  • First loop: i = 0
    • kaartSets[0] = 0
    • kaartSets[1] = 0

Everything is ok here but look what happens in the second loop:

  • Second loop: i = 1 -> Only one increment:
    • kaartSets[1] = 1
    • kaartSets[2] = 1

Now you start to mess things up because you are changing the value of the element [1].

Now, you have several way to achieve what you want to do, here’s one:

int [] cardSets = new int[amountOfSets];
int value = 0

void cardSetsArray()  {
  for(int i = 0; i < cardSets.length -1; i+=2) {
    cardSets[i] = value;
    cardSets[i + 1] = value;
    value++;
  }
}
2 Likes