Ola galera!
Toh com o seguinte problema:
Tenho que criar uma classe que inverta as possições de um vetor.
Mas o problema é q esta dando o seguinte erro:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
Segue abaixo o codigo da classe inversora e do programa principal.
Desde de ja Obrigado ^^
package teste3;
/**
*
* @author K4MU1
*/
public class Inversora {
public void inverter(int [] vetor)
{
int Aux;
for (int i = vetor.length; i > (vetor.length / 2) + 1; i--)
{
Aux = vetor[i];
vetor[i] = vetor[vetor.length - i + 1];
vetor[vetor.length - i + 1] = Aux;
}
}
public void escreverVetor(int [] vetor)
{
for (int i = 0; i < 8;i++)
System.out.println(vetor[i]);
}
}
package teste3;
/**
*
* @author K4MU1
*/
import java.util.Scanner;
public class Teste3 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Inversora vet = new Inversora();
int x = 10;
int [] vetor = new int [x];
for (int i = 0; i < x;i++)
{
vetor[i] = sc.nextInt();
}
vet.inverter(vetor);
vet.escreverVetor(vetor);
}
}