Skip to content

Commit 8744c22

Browse files
authored
Initial upload new files
1 parent 6dd4fef commit 8744c22

File tree

4 files changed

+132
-0
lines changed

4 files changed

+132
-0
lines changed

EscapeNestedLoops.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Java - How to Break out of Nested For Loops
2+
3+
class EscapeNestedLoops {
4+
public static void main(String[] args) {
5+
6+
for(int x=0; x<8; x++) {
7+
for(int y=1; y<4; y++) {
8+
System.out.println(x + " " + y + " " + x*y);
9+
if(x * y > 5)
10+
break;
11+
}
12+
}
13+
14+
System.out.println(" ");
15+
16+
xLoop:
17+
for(int x=0; x<8; x++) {
18+
for(int y=1; y<4; y++) {
19+
System.out.println(x + " " + y + " " + x*y);
20+
if(x * y > 5)
21+
break xLoop;
22+
}
23+
}
24+
25+
}
26+
}

ListToArray.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Java: convert ArrayList<String> to String[]
2+
import java.util.ArrayList;
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.stream.*;
6+
7+
class ListToArray {
8+
public static void main(String[] args) {
9+
// create Array
10+
String[] array = {"Bugatti", "Ferarri", "Lamborghini", "Rolls Royce"};
11+
12+
// 3 ways to print Array
13+
System.out.println(Arrays.toString(array));
14+
15+
for(String s : array) {
16+
System.out.print(s + ", ");
17+
}
18+
System.out.println();
19+
20+
Stream.of(array)
21+
.forEach(System.out::println);
22+
23+
// 2 ways to convert Array to ArrayList
24+
List<String> arrayList = new ArrayList<>(Arrays.asList(array));
25+
System.out.println("arrayList: " + arrayList);
26+
27+
List<String> arrayList2 = new ArrayList<>(List.of(array));
28+
System.out.println("arrayList2: " + arrayList2);
29+
30+
// convert ArrayList to Array
31+
String[] array2 = arrayList.toArray(new String[0]);
32+
33+
// print Array
34+
Stream.of(array2)
35+
.forEach(System.out::println);
36+
37+
}
38+
}
39+
40+
41+
42+
43+
44+
45+

SplitString.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Java: Split a String at char
2+
import java.util.Arrays;
3+
4+
class SplitString {
5+
6+
public static void main(String[] args) {
7+
String s = "My dog ate my homework; Can I turn it in tomorrow?";
8+
9+
String[] ss = s.split(" ");
10+
System.out.println(Arrays.toString(ss));
11+
12+
ss = s.split(";");
13+
System.out.println(Arrays.toString(ss));
14+
15+
// you must escape special chars because the split parameter is a regex
16+
// special chars include \ . + ^ $ | ? * ( ) [ {
17+
String t = "54.25-128.17";
18+
String[] tt = t.split("\\.");
19+
System.out.println(Arrays.toString(tt));
20+
21+
// include multiple split chars inside brackets
22+
tt = t.split("[.-]");
23+
System.out.println(Arrays.toString(tt));
24+
25+
}
26+
}
27+
28+
29+
30+
31+
32+
33+
34+
35+

StringToInt.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Java - Convert String to an Int
2+
3+
class StringToInt {
4+
public static void main(String[] args) {
5+
String s = "222";
6+
int i;
7+
Integer integer;
8+
9+
// returns an int
10+
i = Integer.parseInt(s);
11+
System.out.println(i + 1);
12+
13+
// returns an Integer
14+
integer = Integer.valueOf(s);
15+
System.out.println(integer + 2);
16+
17+
// Best Practice: safely returns an Integer
18+
try {
19+
integer = Integer.valueOf(s);
20+
} catch (NumberFormatException e) {
21+
integer = 0;
22+
}
23+
System.out.println(integer + 3);
24+
}
25+
26+
}

0 commit comments

Comments
 (0)