Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Iterator;
- import java.util.Random;
- public class DemoList3 {
- public static void main(String[] args) {
- System.out.println("Demo de iterable e iterator listas simples");
- Random random = new Random();
- List<Integer> lista1 = new List<Integer>();
- int num;
- System.out.printf("\nNumeros: ");
- for (int i = 0; i < 6; ++i) {
- num = random.nextInt(1000);
- System.out.printf("%d ", num);
- lista1.AddFirst(num);
- }
- System.out.printf("\n\nLista1 con Mostrar(): ");
- lista1.Mostrar();
- System.out.printf("\n\nLista1 con for-each : ");
- int total = 0;
- for (int n : lista1) {
- System.out.printf("%d ", n);
- total += n;
- }
- System.out.printf("\nTotal %d", total);
- System.out.printf("\n\nLista1 con Iterator : ");
- Iterator<Integer> iter = lista1.iterator();
- while(iter.hasNext()) {
- System.out.printf("%d ", iter.next());
- }
- }
- }
Add Comment
Please, Sign In to add comment