ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ java.lang ํจํค์ง
: ์๋ฐ ํ๋ก๊ทธ๋๋ฐ์ ๊ฐ์ฅ ๊ธฐ๋ณธ์ด ๋๋ ํด๋์ค๋ค์ ํฌํจ
: import๋ฌธ ์์ด ์ฌ์ฉํ ์ ์์ (์ปดํ์ผ๋ฌ๊ฐ ์๋์ผ๋ก import๋ฌธ ์ถ๊ฐ)
https://docs.oracle.com/javase/8/docs/api/java/lang/package-summary.html
java.lang (Java Platform SE 8 )
Interface Summary Interface Description Appendable An object to which char sequences and values can be appended. AutoCloseable An object that may hold resources (such as file or socket handles) until it is closed. CharSequence A CharSequence is a readabl
docs.oracle.com
โ๏ธ Object ํด๋์ค
: ๋ชจ๋ ํด๋์ค์ ์ต๊ณ ์กฐ์
https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html
Object (Java Platform SE 8 )
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup. The general contract of fi
docs.oracle.com
โช๏ธ equls(Object obj)
: ์ฐธ์กฐ๋ณ์ ๊ฐ์ผ๋ก ํ๋จ (๊ฐ์ฒด์ ๊ฒฝ์ฐ, ์ธ์คํด์ค ์ฃผ์๊ฐ ๋น๊ต!)
: ๊ฐ์ฒด์ ์ฃผ์๊ฐ์ด ์๋ ์ ์ฅ๋ ๋ด์ฉ์ผ๋ก ํ๋จํ๊ณ ์ถ๋ค๋ฉด, ์ด ๋ฉ์๋๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉ!
โช๏ธ hashCode()
: ๊ฐ์ฒด์ ์ฃผ์๊ฐ์ผ๋ก ํด์์ฝ๋๋ฅผ ๋ง๋ค์ด ๋ฐํ
: ๋ค๋ฅธ ๊ฐ์ฒด๋ ๊ฐ์ ํด์์ฝ๋X in 32bit JVM, ๋ค๋ฅธ ๊ฐ์ฒด๋ ๊ฐ์ ํด์์ฝ๋๋ฅผ ๊ฐ์ง์๋ in 64bit JVM (32bit JVM์์ 4byte์ฃผ์๊ฐ์ ์ฐ์ง๋ง, 64bit JVM์์๋ 8byte ์ฃผ์๊ฐ์ ์ฐ๊ธฐ ๋๋ฌธ, ํด์์ฝ๋๋ 4byte!)
โ equls()๋ฅผ ๊น์ ๋ณต์ฌ๋ก ๋น๊ตํ๊ณ ์ถ์ด์ ์ค๋ฒ๋ผ์ด๋ฉ ํ๋ค๋ฉด, hashCode()๋ ์ค๋ฒ๋ผ์ด๋ฉ ํด์ฃผ์ด์ผ ํจ! (๊ฐ์ ์ธ์คํด์ค ๋ณ์๊ฐ์ ๊ฐ์ง๋ฉด ๊ฐ์ ๊ฐ์ฒด๋ก ๋ณด๊ฒ ๋ค๋ ์๋ฏธ์ด๊ธฐ ๋๋ฌธ์)
โช๏ธ toString()
: ์ธ์คํด์ค์ ๋ํ ์ ๋ณด๋ฅผ ๋ฌธ์์ด๋ก ์ ๊ณต (๋ณดํต ์ค๋ฒ๋ผ์ด๋ฉํด์ ์ฌ์ฉ!)
: ๊ธฐ๋ณธ์ ํด๋์ค๋ช @ํด์์ฝ๋
Card@19e0bfd
โ (์ฐธ๊ณ ) ์ค๋ฒ๋ผ์ด๋ฉํ ๋, ์กฐ์ํด๋์ค์ ์ ์๋ ์ ๊ทผ๋ฒ์๋ณด๋ค ๊ฐ๊ฑฐ๋ ๋์ด์ผ ํจ!
โช๏ธ clone()
: ์ธ์คํด์ค ๋ณ์์ ๊ฐ๋ง ๋ณต์ฌ (์์ ๋ณต์ฌ)
: ์ด ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด Cloneable ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํด์ผ ํจ (์ธ์คํด์ค ๋ณ์๋ฅผ ๋ณดํธํ๊ธฐ ์ํด์ ๊ฐ๋ฐ์๊ฐ clone์ ํ์ฉํ๋์ง ํ์ธํด์ผํจ!)
int[] arr = {1, 2, 3, 4};
int[] arrCopy = arr.clone(); // int[] arrCopy = new int[arr.length]; System.arraycopy(arr, 0, arrCopy, 0, arr.length);
โ๊ณต๋ณํ ํ์
: JDK1.5๋ถํฐ ์ค๋ฒ๋ผ์ด๋ฉํ ๋ ์กฐ์ ๋ฉ์๋์ ๋ฐํํ์ ์ ์์ ํด๋์ค์ ํ์ ์ผ๋ก ๋ณ๊ฒฝ ํ์ฉ! (๋ฒ๊ฑฐ๋ก์ด ํ๋ณํ↓)
๐๐ปโ๏ธ ๊น์ ๋ณต์ฌ? ์์ ๋ณต์ฌ?
๐ก ์์ ๋ณต์ฌ: ๋จ์ํ ๊ฐ์ฒด์ ์ ์ฅ๋ ๊ฐ(์ธ์คํด์ค ๋ณ์)์ ๊ทธ๋๋ก ๋ณต์ ํ ๋ฟ, ๊ฐ์ฒด๊ฐ ์ฐธ์กฐํ๊ณ ์๋ ๊ฐ์ฒด๊น์ง ๋ณต์ X (์๋ณธ๊ณผ ๋ณต์ ๋ณธ์ด ๊ฐ์ ๊ฐ์ฒด๋ฅผ ๊ณต์ )
๐ก ๊น์ ๋ณต์ฌ: ์๋ณธ์ด ์ฐธ์กฐํ๊ณ ์๋ ๊ฐ์ฒด๊น์ง ๋ณต์ (์๋ณธ๊ณผ ๋ณต์ ๋ณธ์ด ์๋ก ๋ค๋ฅธ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐ)
โช๏ธ getClass()
: ์์ ์ด ์ํ ํด๋์ค์ Class ๊ฐ์ฒด๋ฅผ ๋ฐํ
Class class = new Card().getClass(); // Card.class
๐๐ปโ๏ธ ํด๋์ค(Class) ๊ฐ์ฒด?
https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html
Class (Java Platform SE 8 )
Determines if the specified Class object represents a primitive type. There are nine predefined Class objects to represent the eight primitive types and void. These are created by the Java Virtual Machine, and have the same names as the primitive types tha
docs.oracle.com
- ํด๋์ค ๋น 1๊ฐ๋ง ์กด์ฌ
- ํด๋์ค ํ์ผ์ด ํด๋์ค ๋ก๋(์คํ ์ ํ์ํ ํด๋์ค๋ฅผ ๋์ ์ผ๋ก ๋ฉ๋ชจ๋ฆฌ์ ๋ก๋ํ๋ ์ญํ )์ ์ํด์ ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ผ๊ฐ ๋ ์๋์ผ๋ก ์์ฑ
- (1) ํด๋์ค ๊ฐ์ฒด๊ฐ ๋ฉ๋ชจ๋ฆฌ์ ์กด์ฌํ๋์ง ํ์ธ, (2) ์์ผ๋ฉด ๊ฐ์ฒด์ ์ฐธ์กฐ(์ฃผ์๊ฐ) ๋ฐํ (3) ์์ผ๋ฉด ํด๋์ค ํจ์ค์ ์ง์ ๋ ๊ฒฝ๋ก๋ฅผ ๋ฐ๋ผ์ ํด๋์ค ํ์ผ์ ์ฐพ์ Class ๊ฐ์ฒด(์ธ์คํด์ค)๋ก ๋ณํ (๋ชป์ฐพ์ผ๋ฉด ClassNotFoundException ๋ฐ์)
โ๏ธ String ํด๋์ค
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
String (Java Platform SE 8 )
Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argum
docs.oracle.com
โ String์ ๋ณ๊ฒฝ ๋ถ๊ฐ๋ฅํ(immutable) ํด๋์ค
: ํ๋ฒ ์์ฑ๋ String ์ธ์คํด์ค์ ๋ฌธ์์ด์ ์ฝ์ด ์ฌ ์๋ง ์๊ณ , ๋ณ๊ฒฝํ ์๋ ์์!
: '+' ์ฐ์ฐ์๋ฅผ ์ด์ฉํด์ ๋ฌธ์์ด์ ๊ฒฐํฉํ๋ ๊ฒฝ์ฐ, ์ธ์คํด์ค ๋ด์ ๋ฌธ์์ด์ด ๋ฐ๋๋ ๊ฒ์ด ์๋๋ผ ์๋ก์ด ๋ฌธ์์ด์ด ๋ด๊ธด String ์ธ์คํด์ค๊ฐ ์์ฑ๋๋ ๊ฒ!
: ๋ฐ๋ผ์, '+' ์ฐ์ฐ์ ํ ๋๋ง๋ค ๋ฉ๋ชจ๋ฆฌ์ ์๋ก์ด String ์ธ์คํด์ค๊ฐ ์์ฑ๋๋ ๊ฒ์ด๋ฏ๋ก ์๋↓, ๋ฉ๋ชจ๋ฆฌ ์ฐจ์ง↑
๐ก ๋ฌธ์์ด์ ๋ณ๊ฒฝํ๋ ์์ ์ด ๋ง์ด ํ์ํ๋ค๋ฉด, String ํด๋์ค ๋์ StringBuilder๋ StringBuffer ํด๋์ค ์ฌ์ฉ!
๐๐ปโ๏ธ ์์ฑ์? ๋ฌธ์ ๋ฆฌํฐ๋ด?
String s = new String("string"); //ํญ์ ์๋ก์ด ์ธ์คํด์ค ์์ฑ
String s = "string"; //์ด๋ฏธ ์กด์ฌํ๋ ๋ฌธ์์ด์ ์ฌ์ฌ์ฉ (String์ด immutable์ด๊ธฐ ๋๋ฌธ์ ๊ฐ๋ฅ!)
๐ก ๋ฌธ์์ด ๋ฆฌํฐ๋ด(ํด๋น ํด๋์ค์ ๋ฆฌํฐ๋ด ๋ชฉ๋ก์ ์๋)์ ํด๋์ค๊ฐ ํด๋์ค ๋ก๋์ ์ํด ๋ฉ๋ชจ๋ฆฌ์ ๋ก๋๋ ๋ JVM๋ด์ ์๋ ์์ ์ ์ฅ์(constant pool)์ ์ ์ฅ
๐๐ปโ๏ธ ๊ธฐ๋ณธ ์๋ฃํ → String
1๏ธโฃ ๊ธฐ๋ณธ์๋ฃํ + ""
2๏ธโฃ String.valueOf()
int i = 100;
String s1 = i + "";
String s2 = Strig.valueOf(i);
๐๐ปโ๏ธ String → ๊ธฐ๋ณธ ์๋ฃํ
1๏ธโฃ Integet.parseInt()
2๏ธโฃ Integer.valueOf()
int i = Integer.parseInt("100"); //int ๋ฆฌํด
int i = Integer.valueOf("100"); //Integer ๋ฆฌํด
: Integer ๋์ Boolean, Byte, Short, Long, Float, Double ๊ฐ๋ฅ!
โ๏ธ StringBuffer / StringBuilder ํด๋์ค
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
StringBuffer (Java Platform SE 8 )
Inserts the string into this character sequence. The characters of the String argument are inserted, in order, into this sequence at the indicated offset, moving up any characters originally above that position and increasing the length of this sequence by
docs.oracle.com
: ๋ด๋ถ ๋ฌธ์์ด์ ๋ณ๊ฒฝ์ด ๊ฐ๋ฅํ ํด๋์ค
: ๋ด๋ถ์ ์ผ๋ก ๋ฌธ์์ด ํธ์ง์ ์ํ ๋ฒํผ๋ฅผ ๊ฐ์ง๊ณ ์์!
: ๋ฌธ์์ด์ ๋ค๋ฃจ๋ ์์ ์ ํ ๋, ๋ฒํผ์ ํฌ๊ธฐ๊ฐ ์์ ํ๋ ค๋ ๋ฌธ์์ด์ ๊ธธ์ด๋ณด๋ค ์์ ๋๋ ๋ด๋ถ์ ์ผ๋ก ๋ฒํผ์ ํฌ๊ธฐ๋ฅผ ์ฆ๊ฐ์ํค๋ ์์ ์ด ์ํ! (์๋ก์ด ๊ธธ์ด์ ๋ฐฐ์ด ์์ฑ)
StringBuilder sb = new StringBuilder("string");
sb.append("Builder");
sb.toString(); //stringBuilder
๐ก StringBuffer์ StringBuilder๋ equals()๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉํ์ง ์์์ ๋ฌธ์์ด์ ๊ธฐ์ค์ผ๋ก ๋น๊ตX (์ฐธ์กฐ๊ฐ์ ๊ธฐ์ค์ผ๋ก ๋น๊ตO)
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
๐๐ปโ๏ธ StringBuffer VS StringBuilder
๐ก StringBuffer๋ ๋ฉํฐ์ค๋ ๋์ ์์ (Thread safe)ํ๋๋ก ๋๊ธฐํ๋์ด ์๊ณ , StringBuilder๋ ์ฐ๋ ๋ ๋๊ธฐํX. ๊ฑฐ์ ๋์ผํ์ง๋ง ๋ฉํฐ ์ฐ๋ ๋ ํ๊ฒฝ์ด ์๋๋ผ๋ฉด StringBuilder๊ฐ ๋ ์ฑ๋ฅ์ด ์ข๋ค!
โ๏ธ Math ํด๋์ค
โ๏ธ ๋ํผ(Wrapper) ํด๋์ค
: ๊ธฐ๋ณธ ์๋ฃํ์ ๊ฐ์ฒด๋ก ๋ค๋ฃฐ ์ ์๊ฒ ํด์ฃผ๋ ํด๋์ค
: Boolean, Character, Byte, Short, Integer, Long, Float, Double
โ ๊ธฐ๋ณธ ์๋ฃํ์ด ๊ฐ์ฒด๊ฐ ์๋ ๊ฒ์ด ์๋ฐ๊ฐ ์์ ํ ๊ฐ์ฒด์งํฅ ์ธ์ด๊ฐ ์๋ ์ด์ (๊ฐ์ฒด์งํฅ์์ ๋ชจ๋ ๊ฒ์ ๊ฐ์ฒด๋ก ๋ค๋ฃจ์ด์ง!) but ์ฑ๋ฅ์ ๋ ์ข์
- ๋ํผ ํด๋์ค์ ๋น๊ต๋ equals()์ compareTo()๋ฅผ ์ฌ์ฉ!
- ์ซ์์ ๊ด๋ จ๋ ๋ํผ ํด๋์ค๋ Numberํด๋์ค์ ์์!
๐๐ปโ๏ธ String → ๊ธฐ๋ณธ ์๋ฃํ
1๏ธโฃ ํ์ .parseํ์ () ← ๊ธฐ๋ณธ ์๋ฃํ ๋ฐํ
2๏ธโฃ ํ์ .valueOf() ← ๋ํผ ํด๋์ค ๋ฐํ (๊ธฐ๋ณธ ์๋ฃํ์ผ๋ก ๋ฐ์ผ๋ฉด ์คํ ๋ฐ์ฑ์ด ๋จ!)
๐๐ปโ๏ธ ์คํ ๋ฐ์ฑ? ์ธ๋ฐ์ฑ?
โช๏ธ ์คํ ๋ฐ์ฑ : ๊ธฐ๋ณธ ์๋ฃํ์ ๋ํผ ํด๋์ค์ ๊ฐ์ฒด๋ก ์๋ ๋ณํ
โช๏ธ ์ธ๋ฐ์ฑ : ๋ํผ ํด๋์ค → ๊ธฐ๋ณธ ์๋ฃํ
Integer integer = 2; // ์คํ ๋ฐ์ฑ Integer integer = Integer.valueOf(2)
int i = integer; // ์ธ๋ฐ์ฑ int i = integer.intValue()
โ ์ปดํ์ผ๋ฌ๊ฐ ์๋์ผ๋ก ๋ณํํ๋ ์ฝ๋๋ฅผ ๋ฃ์ด์ค
๐ java.util ํจํค์ง
โ๏ธ java.util.Objects ํด๋์ค
https://docs.oracle.com/javase/8/docs/api/java/util/Objects.html
Objects (Java Platform SE 8 )
Generates a hash code for a sequence of input values. The hash code is generated as if all the input values were placed into an array, and that array were hashed by calling Arrays.hashCode(Object[]). This method is useful for implementing Object.hashCode()
docs.oracle.com
: Math ํด๋์ค์ฒ๋ผ ๋ชจ๋ ๋ฉค๋ฒ๊ฐ static
: ๋ ์ฒดํฌ์ ์ ์ฉ
static boolean isNull(Object obj);
static boolean nonNull(Object obj);
static <T> requireNonNull(T obj) // ex. Objects.requireNonNull(name);
: Object ํด๋์ค์ ๋ฌ๋ฆฌ equals()๋ฟ๋ง์ด ์๋๋ผ deepEquals(), compare() ๋ฉ์๋๋ฅผ ๊ฐ์ง๊ณ ์์(Null ์ฒดํฌ๋ ์๋์ผ๋ก ํด์ค)
โ Object์ ์๋ ๋ฉ์๋์ ๊ฐ์ ์ด๋ฆ์ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ์ปดํ์ผ ์๋ฌ๊ฐ ๋จ! (Objects.toString()๊ณผ ๊ฐ์ด ํด๋์ค ์ด๋ฆ์ ๋ถ์ฌ์ค์ผํจ!)
โ๏ธ java.util.Random ํด๋์ค
https://docs.oracle.com/javase/8/docs/api/java/util/Random.html
Random (Java Platform SE 8 )
An instance of this class is used to generate a stream of pseudorandom numbers. The class uses a 48-bit seed, which is modified using a linear congruential formula. (See Donald Knuth, The Art of Computer Programming, Volume 2, Section 3.2.1.) If two instan
docs.oracle.com
: ๋์๋ฅผ ์ป์ ์ ์๋ ํด๋์ค
new Random().nextInt(6); // [0, 6) ๋์ ์์ฑ
๐๐ปโ๏ธ Math.random()๊ณผ util.Random์ ์ฐจ์ด?
๐ก ์ฒซ๋ฒ์งธ๋ก Math.random()์ ํจ์, util.Random์ ํด๋์ค์ด๋ฏ๋ก util.Random์ด ๋ ๋ง์ ๊ธฐ๋ฅ์ ์ ๊ณต!
๐ก Math.random()์ ๋ด๋ถ์ ์ผ๋ก Random ํด๋์ค์ ์ธ์คํด์ค๋ฅผ ์์ฑํด์ ์ฌ์ฉ
double randNum = Math.random();
double randNum = new Random().nextDouble();
๐ก Randomํด๋์ค๋ seed๊ฐ์ ์ค์ ํ ์ ์์
โSeed ๊ฐ
: ์ปดํจํฐ๋ ๋์๋ฅผ ์์ฑํ ์ ์์. ๋จ์ง ๋์ ์์ฑ ์๊ณ ๋ฆฌ์ฆ์ ํตํด ๋์๋ฅผ ์์ฑ!(์ฆ, ๊ฐ์ ์ ๋ ฅ์ ์ฃผ๋ฉด ๊ฐ์ ๊ฒฐ๊ณผ๊ฐ ๋์ด) ์ด๋, ์ ๋ ฅ๊ฐ์ผ๋ก ๋ฃ์ด์ฃผ๋ ๊ฒ์ด Seed๊ฐ! ๊ทธ๋ฆฌ๊ณ ๋์์ฒ๋ผ ๋ณด์ด๊ฒ ํ๊ธฐ ์ํด ๊ธฐ๋ณธ์ ์ผ๋ก seed๊ฐ์ผ๋ก ํ์ฌ์๊ฐ(System.currentTimeMillis())๋ฅผ ๋ฃ์ด์ค!
๐ from๋ถํฐ to ์ฌ์ด์ ๋๋ค ์ซ์๋ฅผ ๋ฐํํ๋ ๋ฉ์๋ ์ฝ๋ ์ค๋ํซ
public static int getRand(int from, int to) { //(์์) from: 10, to: 20
return new Random().nextInt(Math.abs(to-from) + 1) + Math.min(from, to);
}
// 0 <= x < 1
// 0 <= x < 11 <- Random().nextInt(11)
// 10 <= x < 21
โ๏ธ java.util.regex ํจํค์ง
https://docs.oracle.com/javase/8/docs/api/index.html?java/util/regex/package-summary.html
Java Platform SE 8
docs.oracle.com
: ์ ๊ท์(Regular Expression)๊ณผ ๊ด๋ จ๋ ํจํค์ง (โ์ ๊ท์ : ํน์ ํ ๊ท์น์ ๊ฐ์ง ๋ฌธ์์ด์ ์งํฉ)
: ํ ์คํธ ๋ฐ์ดํฐ(ex. String) ์ค์์ ์ํ๋ ์กฐ๊ฑด(pattern)๊ณผ ์ผ์นํ๋ ๋ฌธ์์ด์ ์ฐพ์๋ด๊ธฐ ์ํด ์ฌ์ฉํ๋ ๊ฒ!
๐ ์ฌ์ฉ๋ฒ
1๏ธโฃ ์ ๊ท์์ ๋งค๊ฐ๋ณ์๋ก Pattern์ ๋ง๋ค์!
import java.util.regex.*; //java.util.*; ์๋!
Pattern pattern = Pattern.compile("c[a-z]*"); //c๋ก ์์ํ๋ ์๋จ์ด
2๏ธโฃ ํจํด์ ์ํ๋ ํ ์คํธ ๋ฐ์ดํฐ์ ๋งค์นญ!
Matcher matcher = pattern.matcher("I love cats!");
3๏ธโฃ Matcher ์ธ์คํด์ค๋ฅผ ํ์ฉํด์ ์ ๊ท์์ ๋ถํฉํ๋์ง ํ์ธ!
if(matcher.matches()) System.out.println("์ ์ฒด ๋ฌธ์์ด์ด ํด๋น ํจํด์ ๋งค์นญ๋๋์ง ๊ฒ์ฌ!"); //false
if(matcher.find()) System.out.println(matcher.start() + " - " + matcher.end()); //true, 7 - 11
๐ก (์ฐธ๊ณ ) ์ ๊ท์ ํจํด ๋ง๋๋ ๋ฐฉ๋ฒ์ ์ฑ + ๊ณต์๋ฌธ์ + ๊ตฌ๊ธ๋ง ์ฐธ๊ณ
โ ์ ๊ท์ ์ผ๋ถ๋ฅผ ๊ทธ๋ฃนํ(group)ํ ์๋ ์์!
https://school.programmers.co.kr/learn/courses/30/lessons/17682
ํ๋ก๊ทธ๋๋จธ์ค
์ฝ๋ ์ค์ฌ์ ๊ฐ๋ฐ์ ์ฑ์ฉ. ์คํ ๊ธฐ๋ฐ์ ํฌ์ง์ ๋งค์นญ. ํ๋ก๊ทธ๋๋จธ์ค์ ๊ฐ๋ฐ์ ๋ง์ถคํ ํ๋กํ์ ๋ฑ๋กํ๊ณ , ๋์ ๊ธฐ์ ๊ถํฉ์ด ์ ๋ง๋ ๊ธฐ์ ๋ค์ ๋งค์นญ ๋ฐ์ผ์ธ์.
programmers.co.kr
Pattern pattern = Pattern.compile("([0-9]*)([SDT])([*|#]?)");
Matcher matcher = pattern.matcher("1D2S#10S");
while(matcher.find()) {
System.out.print(matcher.group(1) + matcher.group(2));
if(!matcher.group(3).isEmpty()) {
System.out.print(matcher.group(3));
}
System.out.print("\n");
}
// 1D
// 2S#
// 10S
โ๏ธ java.util.Scanner ํด๋์ค
https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html
Scanner (Java Platform SE 8 )
Scans the next token of the input as a float. This method will throw InputMismatchException if the next token cannot be translated into a valid float value as described below. If the translation is successful, the scanner advances past the input that match
docs.oracle.com
: ์ ๋ ฅ์์ค(ex. String, File, InputStream, Path ...)๋ก๋ถํฐ ๋ฌธ์๋ฐ์ดํฐ๋ฅผ ์ฝ์ด์ค๋ ์ค์บ๋ ๊ธฐ๋ฅ์ ํ๋ ํด๋์ค
Scanner s = new Scanner(System.in);
String input = s.nextLine();
: ์ ๋ ฅ ๋ฐ์ ๊ฐ์ด ์ซ์๋ผ๋ฉด nextInt(), nextDouble() ๋ฑ์ ํตํด ํ๋ณํ์ ์ค์ผ ์ ์์!
: ์ ๊ทํํ์์ ์ด์ฉํด์ ๋ผ์ธ ๋จ์ ๊ฒ์ ๊ฐ๋ฅ!
String line = "100, 200, 300";
Scanner s = new Scanner(line).useDelimiter(", ");
while(s.hasNextInt()) {
System.out.print(s.nextInt() + " ");
} //100 200 300
โ๏ธ java.util.StringTokenizer ํด๋์ค
๐ java.math ํจํค์ง
โ๏ธ java.math.BigInteger ํด๋์ค
โ๏ธ java.math.BigDecimal ํด๋์ค
'์ธ์ด > ์๋ฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์ง๋ค๋ฆญ์ค (0) | 2023.07.14 |
---|---|
์ปฌ๋ ์ ํ๋ ์์ (0) | 2023.07.13 |
์์ธ์ฒ๋ฆฌ (0) | 2023.06.25 |
๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐโ ก (1) | 2023.06.15 |
๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐโ - 2 (0) | 2023.04.27 |