Skip to content

Commit

Permalink
Merge branch 'main' into practice01-stopmin
Browse files Browse the repository at this point in the history
  • Loading branch information
shkisme committed Jul 12, 2023
2 parents fc0eaa4 + 8c2aa0b commit 4d1a7b9
Show file tree
Hide file tree
Showing 43 changed files with 1,095 additions and 8 deletions.
94 changes: 94 additions & 0 deletions src/main/java/Practice/sunny/Chapter3/ArrayListPractice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package Practice.sunny.Chapter3;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

public class ArrayListPractice {

public static void main(String[] args) {

// Array
int[] odds = {1, 3, 5, 7, 9};
String[] weeks = {"월", "화", "수", "목", "금", "토", "일"};

// String[] weeks = new String[]; -> compile error
String[] weeks2 = new String[7];
weeks2[0] = "월";
weeks2[1] = "화";
weeks2[2] = "수";
weeks2[3] = "목";
weeks2[4] = "금";
weeks2[5] = "토";
weeks2[6] = "일";

// indexing
System.out.println(weeks[3]);

// length of array
for (int i = 0; i < weeks.length; i++) {
System.out.println(weeks[i]);
}

// ArrayIndexOutOfBoundsException
// System.out.println(weeks[7]);

// ArrayList
ArrayList pitches = new ArrayList();

// add
pitches.add("138");
pitches.add("129");
pitches.add("142");
pitches.add(0, "133");
pitches.add(2, "133");

// get
System.out.println(pitches.get(1));

// size
System.out.println(pitches.size());

// contains
System.out.println(pitches.contains("142"));

// remove
System.out.println(pitches.remove("129"));
System.out.println(pitches.remove(0));

// Generics
// ArrayList<String> pitches2 = new ArraList<String>();
ArrayList<String> pitches2 = new ArrayList<>();

ArrayList pitches3 = new ArrayList();
pitches3.add("138");
pitches3.add("129");
String one = (String) pitches3.get(0);
String two = (String) pitches3.get(1);

ArrayList<String> pitches4 = new ArrayList<>();
pitches4.add("138");
pitches4.add("129");
String three = pitches4.get(0);
String four = pitches4.get(1);

// asList
String[] data = {"138", "129", "142"};
ArrayList<String> pitches5 = new ArrayList<>(Arrays.asList(data));
System.out.println(pitches5);

ArrayList<String> pitches6 = new ArrayList<>(Arrays.asList("138", "129", "142"));
System.out.println(pitches6);

// String.join
String result = String.join(",", pitches6);
System.out.println(result);

String result2 = String.join(",", data);
System.out.println(result2);

// sort
pitches6.sort(Comparator.naturalOrder());
System.out.println(pitches6);
}
}
60 changes: 60 additions & 0 deletions src/main/java/Practice/sunny/Chapter3/CastingFinalPractice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package Practice.sunny.Chapter3;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CastingFinalPractice {

public static void main(String[] args) {

// String to Integer
String num1 = "123";
int n1 = Integer.parseInt(num1);
System.out.println(n1);

// Integer to String
int n2 = 123;
String num2 = "" + n2;
System.out.println(num2);

int n3 = 123;
String num3 = String.valueOf(n3);
String num4 = Integer.toString(n3);
System.out.println(num3);
System.out.println(num4);

// String to Float
String num5 = "123.456";
double d = Double.parseDouble(num5);
System.out.println(d);

// Integer to Float
int n4 = 123;
double d1 = n4;
System.out.println(d1);

// Float to Integer
double d2 = 123.456;
int n5 = (int) d2;
System.out.println(n5);

// NumberFormatException Error
/*
String num = "123.456;
int n = Integer.parseInt(num);
*/

// Final
final int n6 = 123;
// n6 = 456; -> Compile Error

// Final List
final ArrayList<String> a = new ArrayList<>(Arrays.asList("a", "b"));
// a = new ArrayList<>(Arrays.asList("c", "d")); -> Compile Error

// List.of
final List<String> b = List.of("a", "b");
a.add("c");
}
}
39 changes: 39 additions & 0 deletions src/main/java/Practice/sunny/Chapter3/EnumPractice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package Practice.sunny.Chapter3;

public class EnumPractice {

enum CoffeeType {
AMERICANO,
ICE_AMERICANO,
CAFE_LATE
}

;

public static void main(String[] args) {

// print
System.out.println(CoffeeType.AMERICANO);
System.out.println(CoffeeType.ICE_AMERICANO);
System.out.println(CoffeeType.CAFE_LATE);

// for loop
for (CoffeeType type : CoffeeType.values()) {
System.out.println(type);
}

// Why use Enum
/*
int countSellCoffee(int type){
...
}
int americano = countSellCoffee(1);
int result = countSellCoffee(99); -> error
int countSellCoffee(CoffeeType type){
...
}
int americano = countSellCoffee(CoffeeType.AMERICANO);
*/
}
}
35 changes: 35 additions & 0 deletions src/main/java/Practice/sunny/Chapter3/MapPractice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package Practice.sunny.Chapter3;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MapPractice {

public static void main(String[] args) {

// HashMap
// put
HashMap<String, String> map = new HashMap<>();
map.put("people", "사람");
map.put("baseball", "야구");

// get
System.out.println(map.get("people"));
System.out.println(map.get("java"));
System.out.println(map.getOrDefault("java", "자바"));

// containsKey
System.out.println(map.containsKey("people"));

// remove
System.out.println(map.remove("people"));

// size
System.out.println(map.size());

// keySet
System.out.println(map.keySet());
List<String> keyList = new ArrayList<>(map.keySet());
}
}
72 changes: 72 additions & 0 deletions src/main/java/Practice/sunny/Chapter3/NumBoolPractice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package Practice.sunny.Chapter3;

import java.nio.file.FileSystemNotFoundException;

public class NumBoolPractice {

public static void main(String[] args) {

// Integer
int age = 10;
long countOfstar = 8764827384923849L;

// Float
float pi = 3.14F;
double morePi = 3.14159265358979323846;
double d1 = 123.4;
double d2 = 1.234e2;

// Oct, Hex
int octal = 023; // Decimal : 19
int hex = 0xC; // Decimal : 12

// Add, Subtract, Multiply, Divide
int a = 10;
int b = 5;
System.out.println(a + b); // print 15
System.out.println(a - b); // print 5
System.out.println(a * b); // print 50
System.out.println(a / b); // print 2
System.out.println(7 % 3); // print 1
System.out.println(3 % 7); // print 3

// Increment, Decrement
int i = 1;
int j = 10;
i++;
j--;
System.out.println(i); // print 1
System.out.println(j); // print 9

i = 0;
System.out.println(i++); // print 0
System.out.println(i); // print 1

i = 0;
System.out.println(++i); // print 1
System.out.println(i); // print 1

// Boolean
boolean isSuccess = true;
boolean isTest = false;

// Boolean operation
// 2 > 1 -> true
// 1 == 2 -> false
// 3 % 2 == 1 -> true
// "3".equals("2") -> false

// Conditional
int base = 180;
int height = 185;
boolean isTall = height > base;

if (isTall) {
System.out.println("키가 큽니다."); // print 키가 큽니다.
}

i = 3;
boolean isOdd = i % 2 == 1;
System.out.println(isOdd); // print true
}
}
51 changes: 51 additions & 0 deletions src/main/java/Practice/sunny/Chapter3/SetPractice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package Practice.sunny.Chapter3;

import java.util.Arrays;
import java.util.HashSet;

public class SetPractice {

public static void main(String[] args) {

// HashSet
HashSet<String> set = new HashSet<>(Arrays.asList("H", "e", "l", "l", "o"));
System.out.println(set);

HashSet<Integer> s1 = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5, 6));
HashSet<Integer> s2 = new HashSet<>(Arrays.asList(4, 5, 6, 7, 8, 9));

// Intersection
HashSet<Integer> intersection = new HashSet<>(s1);
intersection.retainAll(s2);
System.out.println(intersection);

// Union
HashSet<Integer> union = new HashSet<>(s1);
union.addAll(s2);
System.out.println(union);

// Relative Complement
HashSet<Integer> subtract = new HashSet<>(s1);
subtract.removeAll(s2);
System.out.println(subtract);

// Add
HashSet<String> set2 = new HashSet<>();
set2.add("Jump");
set2.add("to");
set2.add("Java");
System.out.println(set2);

// AddAll
HashSet<String> set3 = new HashSet<>();
set3.add("Jump");
set3.addAll(Arrays.asList("To", "Java"));
System.out.println(set3);

// Remove
HashSet<String> set4 = new HashSet<>();
set4.remove("To");
System.out.println(set4);

}
}
Loading

0 comments on commit 4d1a7b9

Please sign in to comment.