Método com Exception[RESOLVIDO]

4 respostas
G

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"

LISTA
public 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;
	}
}
LISTA EXCEPTION
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;
	}
}

4 Respostas

S

Ele acusa dead code porque é impossível novoVet[] estar null.

Se eu entendi bem, você quer que ele lance um ListaException ao invés de retornar false certo? Se for isso:

if (novoVet == null) {  //arrume esse if
            throw new ListaException(); //use o construtor que você quer usar. 
        }
fredericomaia10

É o que o colega acima já disse mesmo.

Object novoVet[] = new Object[quantidade + vet.length];  
          
 if (novoVet == null)   
    return false;

É impossível novoVet ser null sendo que você acabou de instanciá-lo.

fredericomaia10

Se a itenção é usar a exceção você poderia verificar a quantidade.

public boolean crescer(int quantidade) throws ListaException  
    {  
        if (quantidade <= 0)   
            throw new ListaException(); //valor inválido  

        Object novoVet[] = new Object[quantidade + vet.length];           
                      
        for(int i = 0; i < vet.length; i++)  
            novoVet[i] = vet[i];  
          
        vet = novoVet;  
          
        return true;  
    }
G

Vlw… Muito Obrigado!!

Criado 9 de abril de 2013
Ultima resposta 9 de abr. de 2013
Respostas 4
Participantes 3