0
I have a small problem when compiling this code, after I compile the following error appears:
Exception in thread "main" java.lang.Arrayindexoutofboundsexception: 10
I’ve searched elsewhere, and they said that this error occurs when you’re oversize a vector, but I don’t 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);
}
}
Counting starts from zero so that C1[10] is outside the limit
– Denis Rudnei de Souza
how do I solve this friend??
– italo vinicius