0

I am trying to fill an array in two nested loops however for each second pPiece[] i want to give it a k attribute of 0 or 1 every second pPiece[] respectively

For example -

  pPieces[0] = new Piece(0,pcName,1);
  pPieces[1] = new Piece(1,pcName,1);
  pPieces[2] = new Piece(0,pcName,1);
  pPieces[3] = new Piece(1,pcName,1);
  etc....

What i have

private Piece pPieces[] = new Piece[8];

for(int j=0; j<pCount; j++) //pCount = 4
  {
      for(int k=0; k<pcCount; k++) //pcCount = 2
      {
          String pcName = "Piece " + (allocation());
          pPieces[j+k] = new Piece(k,pcName,1);
      }
  }

Doing it this way results in pPieces[] index being over written 4 times, i think. Is it possible to properly fill this array which should have 8 objects stored in it with every second 'k' equaling 0 or 1 respectively?

4 Answers 4

2

The issue in your current solution is that j+k will get the same value multiple times during the two loops:

for(int j=0; j<pCount; j++) //pCount = 4
{
    for(int k=0; k<pcCount; k++) //pcCount = 2
    {
        String pcName = "Piece " + (allocation());
        pPieces[j+k] = new Piece(k,pcName,1);
    }
}

For example, when j = 0 and k = 1, you will have j + k = 1. But you will also have that when j = 1 and k = 0.

The problem comes from the fact that you're incrementing the variable j by steps of 1 when you should increment it by steps of pcCount; and the related issue is that j should go to pCount*pcCount and not pCount only.

for(int j=0; j<pCount*pcCount; j+=pcCount) //<--- j+=pcCount here, not j++
{
    for(int k=0; k<pcCount; k++)
    {
        String pcName = "Piece " + allocation();
        pPieces[j+k] = new Piece(k,pcName,1);
    }
}

As a side-note, consider using more descriptive variable names instead of pCount and pcCount.

Sign up to request clarification or add additional context in comments.

2 Comments

i've done this and it's only filling my array with 2 Piece objects instead of 4. I made a loop to go through pPieces.length and print and only 2 spots were filled. I changed "(allocation());" to "(j+k));" btw
yes that got it to add 4 thank you. Both this answer and @Geeth Lochana's answers are correct. Thanks
2

Refactor your code as follows, The issues is pPieces[j+k] = new Piece(k,pcName,1);

Your j+k => 0,1,1,2,2,3,3,4

Corrected Code

private Piece pPieces[] = new Piece[8];
int count=0;

for(int j=0; j<pCount; j++) //pCount = 4
  {
      for(int k=0; k<pcCount; k++) //pcCount = 2
      {
          String pcName = "Piece " + (allocation());
          pPieces[count++] = new Piece(k,pcName,1);
      }
  }

Comments

0

Indeed, j+k will be overlapping. But 2j+k does not. You need to replace by :

private Piece pPieces[] = new Piece[8];

for(int j=0; j<pCount; j++) //pCount = 4
  {
      for(int k=0; k<pcCount; k++) //pcCount = 2
      {
          String pcName = "Piece " + (allocation());
          pPieces[2*j+k] = new Piece(k,pcName,1);
      }
  }
}

Comments

0

The % operator can be of use here:

private Piece pPieces[] = new Piece[8];

for(int j = 0; j < pPieces.length; j++) {
    String pcName = "Piece " + (allocation());
    pPieces[j] = new Piece(j % 2,pcName,1);
}

The % (modulus) operator returns the remainder of integer division.

e.g.

0 % 2 = 0
1 % 2 = 1
2 % 2 = 0
3 % 2 = 1

Creating an alternation of 1 and 0.

4 Comments

this is designed to always fill my pPieces[] array. I don't want that to happen. But good idea!
@danielb Wasn't that the goal? "Is it possible to properly fill this array which should have 8 objects stored in it..."
yes i would like it to be filled, but not unconditionally. Your idea is correct in the instance that I would always want it filled. Sorry, I dont think i explained it clearly enough
@danielb Oh!, well this is just an example. You could put the for loop inside a method, and call that method when the condition applies.

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.