public class ListaCF {
private int numN;
private Object elemento[];
public ListaCF(int capacidade) {
elemento = new Object[capacidade];
numN=0;
}
public ListaCF(){
elemento = new Object[10];
numN = 0;
}
public boolean estavazia(){
if(numN==0)
return true;
else
return false;
}
public boolean estacheia(){
if(numN==elemento.length)
return true;
else
return false;
}
public void exibe(){
//for(int i=0; i<numN; i++)
System.out.println(toString());
}
public void incluiNoFim(Object e){
if(estacheia()==true)
throw new ListaCheiaException();
else{
elemento[numN]=e;
numN++;
}
}
public Object excluiFim(){
if(estavazia()==true)
throw new ListaVaziaException();
else{
Object ultimo;
ultimo = elemento[numN-1];
elemento[numN-1] = 0;
numN--;
return ultimo;
}
}
public int numeroNodos(){
return numN;
}
public String toString(){
String s="";
if(estavazia()==true){
return "[]";
}else{
if(numN==1)
return "["+elemento[0]+"]";
else{
for(int i=0;i<numN;i++){
s= s + elemento[i] +",";
}
s=s.substring(0,s.length()-1);
}
return "["+s+"]";
}
}
public void incluiInicio(Object e){
if(estacheia()==true)
throw new ListaCheiaException();
else{
for(int i=numN;i>=1;i--){
elemento[i]=elemento[i-1];
}
elemento[0]=e;
numN++;
}
}
public Object excluiInicio(){
if(estavazia()==true)
throw new ListaVaziaException();
else{
Object e;
e=elemento[0];
for(int i=0;i<numN-1;i++){
elemento[i]=elemento[i+1];
}
numN--;
return e;
}
}
public Object altera(Object e, int ind){
if(ind<0 || ind>numN-1){
throw new IndiceInvalidoException(ind);
}else{
Object novo;
novo=elemento[ind];
elemento[ind]=e;
return novo;
}
}
public boolean existe(Object e){
for(int i=0;i<numN;i++){
if(elemento[i]==e){
return true;
}
}
return false;
}
public Object consulta(int ind){
if(ind<0 || ind>numN-1){
throw new IndiceInvalidoException(ind);
}else{
return elemento[ind];
}
}
public Object exclui(int ind){
Object objeto;
if(estavazia()==true){
throw new ListaVaziaException();
}else{
if(ind<0 || ind>numN-1){
throw new IndiceInvalidoException(ind);
}else{
objeto=elemento[ind];
elemento[ind]=null;
}
}
return objeto;
}
public void inclui(Object e,int ind){
if(ind<0 || ind>numN-1){
throw new IndiceInvalidoException(ind);
}else{
if(estacheia()==true){
throw new ListaCheiaException();
}
else{
for(int i=ind;i<numN;i++){
elemento[i+1]=elemento[i];
}
elemento[ind]=e;
numN++;
}
}
}
}
public class Teste {
public static void main(String args[]) {
ListaCF lista = new ListaCF(4);
lista.incluiNoFim("teste1");
lista.incluiNoFim("teste3");
lista.incluiNoFim("teste4");
// lista.incluiNoFim("teste");
// lista.incluiNoFim("teste");
lista.exibe();
// lista.altera("teste40",1);
// lista.exibe();
// System.out.println(lista.existe("teste40"));
// lista.exclui(0);
// System.out.println(lista.consulta(0));
System.out.println(lista.numeroNodos());
lista.inclui("teste2",1);
lista.exibe();
System.out.println(lista.numeroNodos());
}
}
Minha duvida é no método inclui, que recebe como parâmetro um objeto, e o índice no qual você quer incluir o objeto, ele verifica se o índice passado como parâmetro é válido, verifica se a lista não está cheia, após isso, ele deveria pelo menos na minha cabeça, adicionar o objeto no índice especificado, e alocar todos os objetos uma posição a frente, mas no caso, até consigo, mas quando chega no ultimo elemento, ele copia o penúltimo.