Skip to content

Added Java examples #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
node_modules
*.log
.DS_Store
/out/
/.idea
*.iml



8 changes: 8 additions & 0 deletions Java/src/1-for.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class For {
public static void main(String[] args)
{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code style.

for (int i = 0; i < 10; i++) {
System.out.println(i);
}
}
}
9 changes: 9 additions & 0 deletions Java/src/2-while.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class While {
public static void main(String[] args) {
int i = 0;
while (i < 10)
{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code style.

System.out.println(i++);
}
}
}
8 changes: 8 additions & 0 deletions Java/src/3-do-while.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class DoWhile {
public static void main(String[] args) {
int i = 0;
do {
System.out.println(i++);
} while (i < 10);
}
}
8 changes: 8 additions & 0 deletions Java/src/5-for-of.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class ForOf {
public static void main(String[] args) {
for (int element : new int[]{7, 10, 1, 5, 2})
{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code style.

System.out.println(element);
}
}
}
9 changes: 9 additions & 0 deletions Java/src/6-break.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Break {
public static void main(String[] args) {
label1: for (int i = 0; i < 10; i++)
{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code style.

if (i == 7) break label1;
System.out.println(i);
}
}
}
12 changes: 12 additions & 0 deletions Java/src/7-continue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Continue {
public static void main(String[] args) {
int i = 0;
label1:
while (i < 10) {
i++;
System.out.println("Hello");
if (i == 5) continue label1;
System.out.println("World");
}
}
}
13 changes: 13 additions & 0 deletions Java/src/8-forEach.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import java.util.Arrays;
import java.util.List;

class ForEach {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(7, 10, 1, 5, 2);
list.forEach(item -> System.out.println(String.format("%s, %s, %s",item,list.indexOf(item),list.toString())));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move a part of this line to the next to fit 100 symbols in line, please? This is a code style mistake.

System.out.println();
list.forEach(System.out::print);
System.out.println();
list.forEach(x -> System.out.print(x));
}
}
9 changes: 9 additions & 0 deletions Java/src/9-map.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import java.util.Arrays;
import java.util.function.Function;

class Map {
public static void main(String[] args) {
Function<Integer,Void> log = s -> {System.out.println(s); return null;};
Arrays.stream(new Integer[]{7, 10, 1, 5, 2}).map(x -> x * 2).map(log);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this example you should better use forEach(System.out::println) instead of map(log).

}
}