Preencher Matriz

4 respostas
A

eu to tentando digitar o um numero qualquer e atraves dele criar a matriz e depois preencher digitando os numero, eu to fazendo assim mas nao esta dando, será que alguem pode me ajudar???

public class Floyd2 {
	 public static void main(String[] args) { 
		 Scanner input = new Scanner(System.in);
		 
		 System.out.println("Informe os vertices: ");  
		 	int size = input.nextInt();
		 	int [][] matriz  = new int [size][size];
		 	System.out.println(size);
		 	
		 	for(int l=0; l<=matriz.length;l++){
		 		for(int c=0; c<=matriz.length;c++){
		 			System.out.println("Digite o elemento " + l + " " + c + " da matriz :");
		 			matriz[l][c]= input.nextInt();
		 		}
		 	}
		}
}

4 Respostas

InicianteJavaHenriqu

Acontece algum erro :?: O que vc não está entendo :?:

flw :thumbup:

elitebest

qual erro está dando? Qual o problema exatamente?

A

esta dando esse erro :

Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 3
at Floyd2.main(Floyd2.java:16)

InicianteJavaHenriqu

Olá alvaroconceicao :smiley:

alvaroconceicao :
esta dando esse erro :

Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 3
at Floyd2.main(Floyd2.java:16)

Este erro é devido você está tentando acessar um endereço do array que não existe (no caso o endereço 4):

Veja, para resolver isto você pode retirar o operador =:

...              
   for(int l=0; l&lt;matriz.length;l++){  
      for(int c=0; c&gt;&lt;matriz.length;c++){ 
...

Ou iniciar seu contador em 1:

...              
   for(int l=1; l&gt;&lt;=matriz.length;l++){  
      for(int c=1; c&lt;=matriz.length;c++){ 
...

flw :thumbup:

Criado 27 de fevereiro de 2012
Ultima resposta 28 de fev. de 2012
Respostas 4
Participantes 3