Eu tenho esse método Crescer para aumentar o tamanho do vetor, só que ele acusa como Dead Code a Expressão "if (novoVet == null) return false ". É um trabalho e o professor pediu para usar Exception nesse método. Se alguém puder me ajudar agradeço!
public boolean crescer(int quantidade) throws ListaException
{
Object novoVet[] = new Object[quantidade + vet.length];
if (novoVet == null)
return false;
for(int i = 0; i < vet.length; i++)
novoVet[i] = vet[i];
vet = novoVet;
return true;
}
Aqui as Classes que estou trabalhado "Lista" e "ListaException"
LISTApublic class Lista
{
private static int Limite = 20;
protected int tamanho;
protected Object vet[];
private Object aux[];
public Lista(int quantidade)
{
tamanho = 0;
vet = new Object[quantidade];
}
public void adicionar(int pos, Object obj) throws ListaException
{
if(pos > tamanho)
pos = tamanho;
if(pos < 0)
pos = 0;
if(tamanho == vet.length)
{
ListaException e = new ListaException
(ListaException.INSERIR, tamanho, pos, obj);
throw e;
}
int i = tamanho;
while(i > pos)
{
vet[i] = vet[i-1];
i--;
}
vet[pos] = obj;
tamanho++;
}
public Object remover(int pos) throws ListaException
{
if(tamanho == 0)
{
ListaException e = new ListaException
(ListaException.REMOVER, tamanho, pos, null);
throw e;
}
Object res;
if(pos >= tamanho)
{
ListaException e = new ListaException
(ListaException.REMOVER, tamanho, pos, null,
"Fora dos limites: " + pos + " >= " + tamanho + ".");
throw e;
}
if(pos < 0)
pos = 0;
res = vet[pos];
while(pos < tamanho)
{
if(pos+1 >= vet.length)
vet[pos] = null;
else
vet[pos] = vet[pos+1];
pos++;
}
tamanho--;
return res;
}
public Object crescer(int newSize) {
aux = new Object[newSize];
System.arraycopy(vet, 0, aux, 0, Math.min(vet.length, aux.length));
vet = aux;
aux = null;
return vet;
}
/*
public boolean crescer(int quantidade) throws ListaException
{
Object novoVet[] = new Object[quantidade + vet.length];
if (novoVet == null) {
return false;
}
for(int i = 0; i < vet.length; i++)
novoVet[i] = vet[i];
vet = novoVet;
return true;
}*/
public String toString()
{
String str = new String();
for(int i = 0; i < tamanho; i++)
str += String.format("[%s] ", vet[i]);
return str;
}
}
public class ListaException extends Exception
{
private static final long serialVersionUID = 1L;
public static final int INDETERMINADO = 0;
public static final int INSERIR = 1;
public static final int REMOVER = 2;
private int tipo;
private int numElementos;
private int pos;
private Object elemento;
ListaException(int Op, int num, int p, Object el)
{
super(Op == 1 ? "Lista cheia." : "Lista vazia.");
tipo = Op;
numElementos = num;
pos = p;
elemento = el;
}
ListaException(int Op, int num, int p, Object el, String msg)
{
super(Op == 1 ? "Lista cheia - " + msg : "Lista vazia - " + msg);
tipo = Op;
numElementos = num;
pos = p;
elemento = el;
}
public int getPos()
{
return pos;
}
public String getTipo()
{
return tipo == 1 ? "Lista cheia" : "Lista vazia";
}
public int getNumElementos()
{
return numElementos;
}
public Object getElemento()
{
return elemento;
}
}