Extended exception for loop

Created the code below when playing with loops. In the code below, the Fibonacci values ​​are stored in an array and then printed using loops.

int [] numbers; numbers = new int[25]; numbers[0] = 1; numbers[1] = 1; System.out.println("Initializing the array values"); for(int i = 2; i < numbers.length; i++) { numbers[i] = numbers[i-1] + numbers[i-2]; } System.out.println("Printing out Fibonacci values"); for(int i = 0; i < numbers.length; i++) { System.out.print(numbers[i] + ", "); } 

The above code is working fine. The first time I put it together, I used an extended loop to print values ​​(the second for a loop in code). This compiles fine, but when I run it, I get the following:

 Initializing the array values Printing out Fibonacci values 1, 1, 2, 3, 8, 34, 377, 17711, Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 34 at ArrayDemo.main(ArrayDemo.java:21) 

I don’t understand what went wrong. Changing the second cycle should not change the values ​​(you will notice that the fibonacci values ​​are incorrect (i.e. there are no values). And I don't understand why a simple extended loop will skip indexes. Now this is not a very big deal, because it is not a project or something else, it just bothers me that I can’t understand why it does it. Any clues?

Improved for the loop just looked like this:

 for(int i : numbers) { System.out.print(numbers[i] + ", "); } 
+6
source share
3 answers
 for(int i : numbers) { System.out.print(numbers[i] + ", "); } 

i here are the elements in the array, not the indices. It may be longer numbers.length .

For example, if numbers = {1,2,3,9} , then i will be 1 , 2 , 3 , 9 . But its length is 4, so when you loop on the elements inside it, you try to make numbers[9] , which exceeds its size.

You probably want System.out.print(i + ", ");

+5
source

for(int i = 0; i <= numbers.length; i++) should be

for(int i = 0; i < numbers.length; i++)

In java, arrays are based on indexes 0. This means that your first element must be available with index 0 and, obviously, the last one along the length of your array minus 1.

 int tab[] = new int[3]; //tab of length 3 tab[0] = 11; tab[1] = 24; tab[2] = 5; 

Here you access the last element by calling tab[2] or tab[tab.length-1] , which is equivalent.


Apologies, it was just a mistake in the code that I put in the question.

The problem is what you have to do: System.out.print(i + ", "); You should read this and this about loop reinforcement.

The for statement also has a different form for iterating through Collections and Arrays. This form is sometimes called advanced for the statement, and can be used to make your loops compact and easy to read. To demonstrate, consider the following array, which contains numbers from 1 to 10:

 int[] numbers = {1,2,3,4,5,6,7,8,9,10}; 

The following program EnhancedForDemo uses advanced to loop through an array:

 class EnhancedForDemo { public static void main(String[] args){ int[] numbers = {1,2,3,4,5,6,7,8,9,10}; for (int item : numbers) { System.out.println("Count is: " + item); } } } 

In this example, the variable element contains the current value from an array of numbers .

So item contains the current value from an array of numbers and not the current index. That's why you get IOOBE .

+5
source
 for(int i : numbers) { System.out.print(numbers[i] + ", "); } 

it should be

  for(int i : numbers) { System.out.print(i + ", "); } 

You do not need to use indexes in the extended for-loop.

+2
source

Source: https://habr.com/ru/post/945322/


All Articles