I decided to play around with classes and method more in this module. I am using the same variable on the for loop, i. The for loop.java has sort or an array.
Lists
Blank Line
Arrays
Hash Maps
Linked Lists
For Loops
While Loops
sout: System.out.Println(” “);
Main.java
package com.michael.test;
public class Main {
public static void main(String[] args) {
For_Loop_Test.for_loop_test();
Blank_Line.blank_line();
List_Test.list_test();
Blank_Line.blank_line();
LinkedList_Test.linkedlist_test();
Blank_Line.blank_line();
Array_Test.array_test();
Blank_Line.blank_line();
HashMap_Test.hashmap_test();
Blank_Line.blank_line();
}
}
Array_Test.java
package com.michael.test;
public class Array_Test {
public static void array_test() {
int i = 0;
String[] array = new String[]{"Array Test1", "Array Test2", "Array Test3"};
for (i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
System.out.println();
array[0] = "New Value1";
for (i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
}
Blank_List.java
package com.michael.test;
public class Blank_Line {
public static void blank_line() {
System.out.println();
}
}
For_loop_Test
Count to 10
Create a 2D array and store: random array. sort in ascending order, sort in descending order
package com.michael.test;
import java.text.DecimalFormat;
import java.util.Random;
import java.util.Arrays;
public class For_Loop_Test {
public static void for_loop_test() {
int i = 0;
int length = 10;
int array_length = 10;
DecimalFormat ft = new DecimalFormat("##");
for (i = 0; i < length; i++) {;
System.out.println(i);
}
// Create random array of integers
int[] rnd_array = new int[array_length];
int[][] print_array = new int[array_length][3];
Random rand_array = new Random();
// Create the array and store it in column 0
for ( i = 0; i < array_length; i++) {
rnd_array[i] = rand_array.nextInt(1, array_length);
print_array[i][0] = rnd_array[i];
}
// Sort the array rnd_array
Arrays.sort(rnd_array);
// Store the sorted array in column 1 in Ascending Order
for ( i = 0; i < array_length; i++) {
print_array[i][1] = rnd_array[i];
}
Blank_Line.blank_line();
// Store the sorted array in column 2 in Descending Order
for ( i = array_length-1; i >= 0; i--) {
print_array[array_length - 1 - i][2] = rnd_array[i];
}
System.out.println("Rnd" + " " + "Asc" + " " + "Desc");
ft = new DecimalFormat("00");
for ( i = 0; i < array_length; i++) {
System.out.println(
ft.format(print_array[i][0]) +
" " + ft.format(print_array[i][1]) +
" " + ft.format(print_array[i][2]) );
}
}
}
HashMap.java
package com.michael.test;
import java.util.HashMap;
import java.util.UUID;
public class HashMap_Test {
public static void hashmap_test() {
// < Key, Value >
HashMap<Integer, String> map = new HashMap<>();
map.put(5, "Hash_Test");
System.out.println("Key: " + "5" + " Value: " + map.get(5));
HashMap<String, String> map_string = new HashMap<>();
map_string.put("key_5", "key_test_5");
System.out.println("Key: " + "key_5" + " Value: " + map_string.get("key_5"));
HashMap<UUID, Integer> map_points = new HashMap<>();
UUID uuid = UUID.randomUUID();
map_points.put(uuid, 100);
System.out.println("UUID: " + uuid + " Points: " + map_points.get(uuid));
System.out.println("For Loop for HashMap map");
Blank_Line.blank_line();
for (int map_count = 0; map_count < map.size(); map_count++) {
System.out.println("Key " + map.keySet().toArray()[map_count] + " Value: " + map.values().toArray()[map_count]);
}
}
}
LinkedList_Test.java
package com.michael.test;
import java.util.LinkedList;
import java.util.List;
public class LinkedList_Test {
public static void linkedlist_test() {
int i = 0;
LinkedList<String> list = new LinkedList<String>();
list.add("List Test1");
list.add("List test2");
list.add("List Test3");
System.out.println("LinkedList");
for (i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
Blank_Line.blank_line();
list.removeFirst();
System.out.println("Remove First from list");
for (i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
Blank_Line.blank_line();
System.out.println("Replace First");
list.addFirst("List Test4");
for (i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
List_Test.java
package com.michael.test;
import java.util.ArrayList;
import java.util.List;
public class List_Test {
public static void list_test() {
//set loop variable for first time only
int i = 0;
List<String> list = new ArrayList<>();
list.add("List Test1");
list.add("List test2");
list.add("List Test3");
for (i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
Blank_Line.blank_line();
list.removeFirst();
System.out.println("Remove first item");
for (i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
Blank_Line.blank_line();
list.addFirst("List Test4");
System.out.println("Add new first item");
for (i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
Blank_Line.blank_line();
System.out.println("While Loop list");
i = 0;
while (i < list.size()) {
System.out.println(list.get(i));
i++;
}
Blank_Line.blank_line();
}
}
Output
C:\Users\Win10x64i7\.jdks\openjdk-23.0.1\bin\java.exe "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2024.2.4\lib\idea_rt.jar=62051:D:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2024.2.4\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath C:\Users\Win10x64i7\IdeaProjects\Java\Third\out\production\Third com.michael.test.Main
0
1
2
3
4
5
6
7
8
9
Rnd Asc Desc
08 04 09
08 06 09
06 08 09
09 08 08
08 08 08
09 08 08
04 08 08
09 09 08
08 09 06
08 09 04
List Test1
List test2
List Test3
Remove first item
List test2
List Test3
Add new first item
List Test4
List test2
List Test3
While Loop list
List Test4
List test2
List Test3
LinkedList
List Test1
List test2
List Test3
Remove First from list
List test2
List Test3
Replace First
List Test4
List test2
List Test3
Array Test1
Array Test2
Array Test3
New Value1
Array Test2
Array Test3
Key: 5 Value: Hash_Test
Key: key_5 Value: key_test_5
UUID: feb7cbbb-1705-46a0-9de1-452ededadba0 Points: 100
For Loop for HashMap map
Key 5 Value: Hash_Test
Process finished with exit code 0