Advertisement
cesarnascimento

Arvore TreeSet

Nov 17th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.71 KB | None | 0 0
  1.  
  2. public class Arvore implements Comparable<Arvore>{
  3.    
  4.     int altura;
  5.     public Arvore(int alt) {
  6.         altura = alt;
  7.     }
  8.    
  9.     @Override
  10.     public String toString() {
  11.         return altura + "";
  12.     }
  13.  
  14.     @Override
  15.     public int compareTo(Arvore a) {
  16.         // TODO Auto-generated method stub
  17.         return a.altura;
  18.     }
  19.  
  20. }
  21.  
  22. import java.util.Iterator;
  23. import java.util.TreeSet;
  24.  
  25. public class UsandoTreeSetComObjeto {
  26.  
  27.     public static void main(String[] args) {
  28.         TreeSet<Arvore> arvore = new TreeSet<Arvore>();
  29.         arvore.add(new Arvore(2));
  30.         arvore.add(new Arvore(1));
  31.         arvore.add(new Arvore(3));
  32.         Iterator<Arvore> iterator = arvore.iterator();
  33.         while(iterator.hasNext()) {
  34.             System.out.println(iterator.next()+" ");
  35.         }
  36.  
  37.     }
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement