Error compiling code in eclipse

0

I have a small problem compiling this code, after I compile the following error appears:

  

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:   10

I looked in other places, and they said that this error occurs when you're overriding the size of a vector, but I do not understand what's wrong with my code.

public class Questao5Agenda {
    public static void main(String[] args) {

        Scanner inputNumerico = new Scanner(System.in);
        Scanner inputString = new Scanner(System.in);


        Contato [] c1 = new Contato[10];
        int opc;


        do{

             c1[10].Menu();

            opc = inputNumerico.nextInt();

            switch(opc){


            case 1:
                System.out.println("====== Criar Contato ======\n");
                boolean flag= true; 

                for(int i=0; i<c1.length; i++){
                    if(c1[i]==null){   
                        flag= false; 
                        break;      
                    }
                }



                if(flag==false){
                    Questao5Contato c = new Questao5Contato();

                    c.AdicionarContato(); 

                    for(int i=0; i<c1.length; i++){
                        if(c1[i]==null){ 
                            c1[i]= c; 
                        }

                    }

                    System.out.println("Contato adicionado com sucesso !");
                } else{
                    System.out.println("Lista de contatos cheia !");
                }
                break;


            case 2:
                System.out.println("====== Listar Contato ======");

                int i=0;
                if(c1[i]!=null){
                    c1[i].Listar();

                }   
            }



        }while(opc!=5);
    }

}
    
asked by anonymous 22.05.2017 / 23:18

1 answer

1

Your error is in this line:

c1[10].Menu();

The array c1 has size 10, that is, its valid positions are from 0 to 9, but you are trying to access the tenth position.

    
22.05.2017 / 23:21