2

I have an array called blockHeights, which contains 3 values inside of it, namely 1,2,3. So blockHeights[0] is equal to 1.

I also have a loop:

for (int i = 1; i <= blockHeights.length; i++)

In the first time around the loop, I want to create a variable called totalBlockHeights where it is

int totalBlockHeights = blockHeights[0] + blockHeights [1] + blockHeights [2];

However, in the next loop I want that variable to change, so that it only adds blockHeights[1] and blockHeights[2] together, ignoring blockHeights[0].

How would I go about doing this?

1
  • Do you also want the third loop to assign only blockHeights[2] to totalBlockHeights? Commented Sep 14, 2014 at 10:51

5 Answers 5

3

Try the following (I'm assuming the third iteration should only include blockHeights[2], following the pattern):

for (int i = 1; i <= blockHeights.length; i++) {
    int totalBlockHeights;
    for (int j = i - 1; j < blockHeights.length; j++) { // all block heights from here onwards
        totalBlockHeights += blockHeights[j];
    }
    // do whatever
}
Sign up to request clarification or add additional context in comments.

1 Comment

That's what I was thinking :)
2

Well, if you want the sum of your array, and the sum of the array without first value

int totalBlockHeights = 0;
for(int i = 0; i < blockHeights.length; i++){
    totalBlockHeights += blockHeights[i];
}

System.out.println(totalBlockHeights);
System.out.println("totalBlockHeights without first value = " + (totalBlockHeights - blockHeights[0]));

this way you only loop once

Comments

0

Try following code:

public class Loop {

    public static void main(String[] argv) {

        int[] blockHeights = new int[] {1, 2, 3};
        int totalBlockHeights = 0;

        for(int i = 0; i < blockHeights.length; i++) {
            totalBlockHeights = 0;
            for(int j = i; j < blockHeights.length; j++) {
                totalBlockHeights += blockHeights[j];
            }
            System.out.println(totalBlockHeights);
        }
    }

}

Comments

0
    int[] blockHeights = new int[] { 1, 2, 3 };
    int totalBlockHeights = 0;
    int customBlockHeights = 0;

    for (int i = 0; i < blockHeights.length; i++) {
        totalBlockHeights += blockHeights[i];
        if (i == 0) {
            continue;
        }
        customBlockHeights += blockHeights[i];
    }
    System.out.println(totalBlockHeights);
    System.out.println(customBlockHeights);

This will print:

6
5

You dont need two for to achieve that.

Comments

0

you can perform this on two for loop outer loop for (int i = 1; i <= blockHeights.length; i++), and in inner loop (take a variable j) you can do like int totalBlockHeights = totalBlockHeights + blockHeights[j], and for i<j, you can just continue the for loop.

as answered by btrs20

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.