Jump to content

Having trouble using for-each loops. JAVA

CreepyPro

I'm trying to loop through an Array of ints(using a for-each loop) and checking if there's a number bigger than 0 at that index but I'm getting ArrayIndexOutOfBounds: 5 almost every time.

But if I use a normal for loop the method works fine.

 

	public boolean hallIsClear() {
		int c=0;
		/*
		for(int toys : hall) 
			c += hall[toys];
		*/
		
		for(int i=0;i<hall.length;i++)
			c += hall[i];
		return c == 0;
	}

 

Link to comment
Share on other sites

Link to post
Share on other sites

The way you are using 'toys' in the for-each loop is what is going wrong.

You are trying to use the for-each like it is a for-loop, while you can actually work with it quite a bit easier.

 

As GeeksForGeeks says:

for (type var : array) 
{ 
    statements using var;
}

https://www.geeksforgeeks.org/for-each-loop-in-java/

 

In your example, you can basically do this:

for(int toys : hall) 
	c += toys;

What the first line accomplishes:

"For each integer value in hall, that I will now call 'toys', do this:"

What the second line does: 
"Take value 'c' and add the current value I am checking (toys) to this variable".

 

What is probably happening in your first example, is that one of the values of 'toys' (so the value of one of the variables in the array) is larger than the (length-1) of your array. Causing the out-of-bound error.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, minibois said:

The way you are using 'toys' in the for-each loop is what is going wrong.

You are trying to use the for-each like it is a for-loop, while you can actually work with it quite a bit easier.

 

As GeeksForGeeks says:


for (type var : array) 
{ 
    statements using var;
}

https://www.geeksforgeeks.org/for-each-loop-in-java/

 

In your example, you can basically do this:


for(int toys : hall) 
	c += toys;

What the first line accomplishes:

"For each integer value in hall, that I will now call 'toys', do this:"

What the second line does: 
"Take value 'c' and add the current value I am checking (toys) to this variable".

 

What is probably happening in your first example, is that one of the values of 'toys' (so the value of one of the variables in the array) is larger than the (length-1) of your array. Causing the out-of-bound error.

Thanks for the explanation, everything works as it should now. 

Have a nice day

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×