Pq obrigar coloque <E>?

1 resposta
G

Ola Pessoal,
Pq obirgar colocar ?, eu n entendi,tem erro, por favor me explique,certo.
Outro como fazer "Inserir: Os elementos são inseridos em ordem ascedente", vc tem idéia?

public class Node<E> {
	private E data;
	Node<E> previous;
	Node<E> next;
	
	public Node() {
		this ( null, null, null );
	}

	public Node( Node<E> previous, E data, Node<E> next ) {
		setPrevious ( previous );
		setData     ( data );
		setNext     ( next );
	}

	public void setPrevious( Node<E> previous ) {
		this.previous = previous;
	}

	public void setData( E data ) {
		this.data = data;
	}

	public void setNext( Node<E> next ) {
		this.next = next;
	}

	public Node<E> getPrevious() {
		return ( previous );
	}

	public E getData () {
		return ( data );
	}

	public Node<E> getNext() {
		return ( next );
	}	
}

public class ListaDEncadeada<E>{
	private Node<E> header;
	private Node<E> tailer;
	private int nelemCounter;
	
	public ListaDEncadeada(){
		this.header = null;
		this.tailer  = null;
		this.nelemCounter = 0;
	}
	public void insereNoInicio(E x){
		Node<E> noAux;
		noAux = new Node<E>(x, null, header);
		if(header ==null){
			tailer=noAux;
		}else{
			header.setPrevious(noAux);
		}
		tailer=noAux;
		nelemCounter++;
	}
	public void insereNoFinal(E x){
		Node<E> noAux;
		noAux = new Node<E>(x, tailer, null);
		if (header==null) 
			header = noAux;
		else {
			tailer.setNext(noAux);
		}
		tailer = noAux;
		nelemCounter++;
	}
	public void imprimeED(){
	   Node<E> pt;
	   pt = header;
	   if (pt != null){
		   System.out.println("===============================");
		   System.out.println("Numero de elementos = " + nelemCounter);
		   System.out.println("Esquerda => Direita");
		   System.out.println();
		   while(pt!=null){
			   System.out.print(pt.getData() + " ");
			   pt= pt.getNext();
		   }
		   System.out.println("\n===============================");
	   }
	   else{
	   	System.out.println("A Lista esta vazia");
	   }
	   System.out.println();
	}
	public void imprimeDE(){
	   Node<E> pt;
	   pt = tailer;
	   if (pt != null){
		   System.out.println("===============================");
		   System.out.println("Numero de elementos = " + nelemCounter);
		   System.out.println("Direita => Esquerda");
		   System.out.println();
		   while(pt!=null){
			   System.out.print(pt.getData() + " ");
			   pt= pt.getPrevious();
		   }
		   System.out.println("\n===============================");
	   }
	   else{
	   	System.out.println("A Lista esta vazia");
	   }
	   System.out.println();
	}
	public void insereDepois(E x,E y){
		Node<E> pt, aux, ptprox;
		pt = header;
		while(pt != null && pt.getData()!= x){
			pt = pt.getNext();
		}
		if(pt == null){
			System.out.println("O Elemento com a informacao " + x + " nao esta na Lista\n");
			System.exit(0);
		}
		aux = new Node<E>(y, pt, pt.getNext());
	    ptprox = pt.getNext();
	    pt.setNext(aux);
	    if (ptprox !=null){ 
	    	ptprox.setPrevious(aux); 
	    }else {
	    	tailer = aux;
	    }
	    nelemCounter++;
	}
	public void insereAntes(E x,E y){
		Node<E> pt, aux, ptante;
		pt = header;
		while(pt != null && pt.getData()!= x){
			pt = pt.getNext();
		}
		if(pt == null){
			System.out.println("O Elemento com a informacao " + x + " nao esta na Lista\n");
			System.exit(0);
		}
		aux = new Node<E>(y, pt.getPrevious(), pt);
	    ptante = pt.getPrevious();
	    pt.setPrevious(aux);
	    if (ptante !=null){ 
	    	ptante.setNext(aux); 
	    }else {
	    	header = aux;
	    }
	    nelemCounter++;
	}
	public ListaDEncadeada obterListaInv(){
	   ListaDEncadeada ListaInv = null;
	   Node<E> pt;
	   pt = header;
       if (pt == null){
       	System.out.println("A Lista ainda esta Vazia\n"); 
       	System.exit(0);
       }
       ListaInv = new ListaDEncadeada();
	   while(pt != null){
	 	 ListaInv.insereNoInicio(pt.getData());
		 pt = pt.getNext();
	   }
	   return ListaInv;
   }
}
[color=#444444] [/color] ERRO - ListaDEncadeada

[color=red]noAux = new Node(x, null, header);
cannot find symbol constructor Node(E,,Node )

noAux = new Node(x, tailer, null);
cannot find symbol constructor Node(E,Node, )

aux = new Node(y, pt, pt.getNext());
cannot find symbol constructor Node(E,Node,Node )

aux = new Node(y, pt.getPrevious(), pt);
cannot find symbol constructor Node(E,Node,Node )[/color]

1 Resposta

bombbr

Este é uma sintaxe de generics do java.
Este deve ser substituido por um nome de uma classe (Exemplo List), quanto for utilizar a classe genérica.

Veja o tutorial abaixo.

http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf

Criado 23 de setembro de 2010
Ultima resposta 23 de set. de 2010
Respostas 1
Participantes 2