Error trying to scan an array

0

I'm trying to scan the array inside the loop, as per the code below:

import java.util.Scanner;

public class RapidoPora {


    public static void main(String[] args) {

        int[] nota = new int[10];



        for(int c = 0; c < 11; c++) {


            System.out.println(nota[c]);

        }




}


}

But this exception occurs:

 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
        at RapidoPora.main(RapidoPora.java:15)
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0

What can it be?

    
asked by anonymous 04.03.2018 / 20:32

2 answers

3

If the size of the vector is 10 positions, you only have 10 positions to go through, not 11 as it is in your loop. Change to:

int[] nota = new int[10];


 for(int c = 0; c < 10; c++) {


     System.out.println(nota[c]);

 }

See working at ideone: link

    
04.03.2018 / 20:35
-2

The error refers to the overflow of the vector index. The correct would be:

for(int c = 0; c < 10; c++) {

            System.out.println(nota[c]);

        }
    
05.03.2018 / 01:24