jtentor

DemoList3.java [iterable e iterator]

Oct 22nd, 2016
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1. import java.util.Iterator;
  2. import java.util.Random;
  3.  
  4. public class DemoList3 {
  5.  
  6.     public static void main(String[] args) {
  7.         System.out.println("Demo de iterable e iterator listas simples");
  8.        
  9.        
  10.         Random random = new Random();
  11.  
  12.         List<Integer> lista1 = new List<Integer>();
  13.        
  14.         int num;
  15.         System.out.printf("\nNumeros: ");
  16.         for (int i = 0; i < 6; ++i) {
  17.             num = random.nextInt(1000);
  18.             System.out.printf("%d ", num);
  19.             lista1.AddFirst(num);
  20.         }
  21.        
  22.         System.out.printf("\n\nLista1 con Mostrar(): ");
  23.         lista1.Mostrar();
  24.  
  25.         System.out.printf("\n\nLista1 con for-each : ");
  26.         int total = 0;
  27.         for (int n : lista1) {
  28.             System.out.printf("%d ", n);
  29.             total += n;
  30.         }
  31.         System.out.printf("\nTotal %d",  total);
  32.  
  33.         System.out.printf("\n\nLista1 con Iterator : ");
  34.         Iterator<Integer> iter = lista1.iterator();
  35.         while(iter.hasNext()) {
  36.             System.out.printf("%d ", iter.next());
  37.         }
  38.  
  39.        
  40.     }
  41.  
  42. }
Add Comment
Please, Sign In to add comment