Skip to content

Commit e2c19f2

Browse files
#17 java
1 parent b6d13d9 commit e2c19f2

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import java.util.*;
2+
3+
/**
4+
* #17 ITERACIONES
5+
*
6+
* @author martinbohorquez
7+
*/
8+
public class martinbohorquez {
9+
public static void main(String[] args) {
10+
// 1. for
11+
System.out.println("1. Iteración usando 'for':");
12+
for (int i = 1; i <= 10; i++) System.out.println(i);
13+
14+
// 2. while
15+
System.out.println("2. Iteración usando 'while':");
16+
int i = 1;
17+
while (i <= 10) System.out.println(i++);
18+
19+
// 3. recursividad
20+
System.out.println("3. Iteración usando 'función recursiva':");
21+
count10(1);
22+
23+
/*
24+
* DIFICULTAD EXTRA
25+
*/
26+
//4. Array y for
27+
System.out.println("4. Iteración usando 'array con fori'");
28+
int[] numbers = {6, 5, 4, 3, 2, 1};
29+
for (int number : numbers) System.out.println(number);
30+
31+
//5. Stream y forEach
32+
System.out.println("5. Iteración usando 'stream con forEach'");
33+
Arrays.stream(numbers).forEach(System.out::println);
34+
35+
// 6. Lista, reversed y forEach
36+
System.out.println("6. Iteración usando 'lista con forEach(reversed)'");
37+
Arrays.asList(1, 2, 3, 4).reversed().forEach(System.out::println);
38+
39+
//7 String, split, stream y forEach
40+
System.out.println("7. Iteración para una 'string' (sorted) usando 'split, stream y forEach'");
41+
String word = "strawberry";
42+
Arrays.stream(word.split("")).sorted().forEach(System.out::println);
43+
44+
// 8. Map, keySet, values, forEach
45+
System.out.println("8. Iteración para una 'map' por keys/values usando 'forEach'");
46+
Map<Integer, String> map = new HashMap<>();
47+
map.put(1, "a");
48+
map.put(2, "b");
49+
map.put(3, "c");
50+
map.put(4, "d");
51+
map.put(5, "e");
52+
map.keySet().forEach(System.out::println);
53+
map.values().forEach(System.out::println);
54+
map.forEach((key, value) -> System.out.println(key + ": " + value));
55+
56+
// 9. do-while
57+
System.out.println("9. Iteración usando 'do-while':");
58+
i = 10;
59+
do {
60+
System.out.println(i--);
61+
} while (i >= 1);
62+
63+
// 10. set, iterator
64+
System.out.println("10. Iteración de un 'set' usando 'iterator':");
65+
Set<String> set = new HashSet<>(Arrays.asList("a", "b", "c", "d", "e"));
66+
Iterator<String> iterator = set.iterator();
67+
iterator.forEachRemaining(System.out::println);
68+
69+
}
70+
71+
private static void count10(int i) {
72+
if (i <= 10) {
73+
System.out.println(i);
74+
count10(++i);
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)