Java
/
Basics
- 1 Basics 9
-
Classes
-
Objects
-
Arrays
-
Variables
-
Loops
-
Numbers
-
Strings
-
Exceptions
-
Regexp
- 2 OOP 9
-
Inheritance
-
Polymorphism
-
Static
-
Abstract
-
Interfaces
-
Constructors
-
Packages
-
Nested Classes
-
Final
- 3 Compiler 2
-
Sublime Text
-
Apache Ant
- 4 Collections 8
-
Lists
-
Comparable
-
Sets
-
Maps
-
Generics
-
Properties
-
Streams
-
Json
- 5 Threads 4
-
Create Thread
-
Sleep
-
Lock
-
Scheduler
- 6 Design patterns 4
-
Singleton
-
Observer
-
Strategy
-
Mediator
- 7 Swing 12
-
Frame
-
Panel
-
Listener
-
Combo Box
-
Label
-
Image
-
Menu
-
Table
-
Layout
-
Drawing
-
Timer
-
Designer
- 8 I/O 7
-
Streams IO
-
Socket
-
Watching Files
-
Mail
-
Logger
-
Clipboard
-
Encrypt
- 9 Effective 7
-
Constructors
-
Dependency Injection
-
Composition
-
Interfaces Default
-
Import Static
-
Enums
-
Lambdas
- 10 Junit 5
-
About Junit
-
Test Case
-
Suite Test
-
Annotations
-
Exceptions
- 11 Lambdas 7
-
Expressions
-
Functional Interfaces
-
Streams
-
Common Operations
-
Default Methods
-
Static Methods
-
Single Responsibility
- 12 JavaFX 6
-
Openjfx
-
Scene Builder
-
First App
-
Jar Archive
-
On Action
-
Change Listener
- 13 Maven 4
-
Demo
-
Spring Boot
-
Junit
-
Guava
- 14 Spring Boot 8
-
Quick start
-
Rest service
-
Consuming Rest
-
Templates
-
Security
-
Command Line
-
Scheduling Tasks
-
Ajax
/
Loops
➟
➟
Last update: 29-10-2021
For loop
p114 You can use normal for-loop when you know how many times you want to loop.
/**
* For loop can be used when you know ...
* how many times you want to loop.
*
* Those three expressions in the for loop are optional ...
* but this form is not recommended.
*/
package com.minte9.basics.loops;
public class ForLoop {
public static void main(String[] args) {
for (int i=0; i<10; i++) {
System.out.println(i);
// 0 1 2 .. 9
}
int i=0; for ( ;; ) { // infinite loop
if (i >= 10) break;
System.out.println(i++);
// 0 1 2 .. 9
}
}
}
Enhanced
Allows each you to visit each element without tracking the index.
/**
* With Enhanced For Loop you don't need a loop index
*/
package com.minte9.basics.loops;
public class EnhancedFor {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
for (int no : numbers) {
System.out.println(no); // 1 2 .. 5
}
}
}
For Each
With Java 8 we can use forEach to loop a Map, List, Set or Stream.
/**
* Starting with Java 8 we can use forEach ...
* to loop over a Collection
*/
package com.minte9.basics.loops;
import java.util.Arrays;
import java.util.List;
public class ForEach {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3);
// lambda
numbers.forEach(x -> System.out.println(x)); // 1, 2, 3
// method reference
numbers.forEach(System.out::println); // 1, 2, 3
}
}
While
A while loop has only the boolean test.
/**
* While loop is going ...
* as long as a boolean condition is true
*/
package com.minte9.basics.loops;
public class While {
public static void main(String[] args) {
int i = 0;
while(i<10) {
System.out.println(i);
// 0 1 2 ... 9
i++;
}
}
}
Break
The break statement can be labeled.
/**
* To exit a for loop use break statement.
*
* For multiple loops use labels
*/
package com.minte9.basics.loops;
public class Break {
public static void main(String[] args) {
int[][] numbers = {
{1,2,8},
{4,5,6},
{7,8,9}
};
a: for (int i=0; i<numbers.length; i++) {
b: for (int j=0; j<numbers[i].length; j++) {
System.out.println("i=" + i + " / j=" + j);
if (numbers[i][j] == 4) {
System.out.println("Found 4 at " + i + ":" + j);
System.out.println("Break from b loop");
break b;
}
if (numbers[i][j] == 7) {
System.out.println("Found 7 at " + i + ":" + j);
System.out.println("Break from a loop");
break a;
}
}
}
}
}
/*
i=0 / j=0
i=0 / j=1
i=0 / j=2
i=1 / j=0
Found 4 at 1:0
Break from b loop
i=2 / j=0
Found 7 at 2:0
Break from a loop
*/
➥ Questions github Basics