문서의 선택한 두 판 사이의 차이를 보여줍니다.
다음 판 | 이전 판 | ||
프로그램:java:ㄴhomework:book_source:ch12 [2022/01/04 11:15] clayeryan@gmail.com 만듦 |
프로그램:java:ㄴhomework:book_source:ch12 [2023/07/13 17:29] (현재) clayeryan@gmail.com ↷ 문서가 programmer:java:ㄴhomework:book_source:ch12에서 프로그램:java:ㄴhomework:book_source:ch12(으)로 이동되었습니다 |
||
---|---|---|---|
줄 1: | 줄 1: | ||
- | ====== | + | ====== |
+ | ===== java.lang 패키지 ===== | ||
+ | |||
+ | ==== Object 클래스 ==== | ||
+ | |||
+ | : | ||
+ | |||
+ | public boolean equals(Object obj){ | ||
+ | return (this == obj); | ||
+ | } | ||
+ | | ||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | public class EqualsEx { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | Obj obj1 = new Obj(100); | ||
+ | Obj obj2 = new Obj(100); | ||
+ | |||
+ | if (obj1.equals(obj2)) { | ||
+ | System.out.println(" | ||
+ | } else { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | |||
+ | Obj obj3 = obj1; | ||
+ | |||
+ | if (obj1.equals(obj3)) { | ||
+ | System.out.println(" | ||
+ | } else { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | |||
+ | ObjOverride objo1 = new ObjOverride(100); | ||
+ | ObjOverride objo2 = new ObjOverride(100); | ||
+ | |||
+ | if (objo1.equals(objo2)) { | ||
+ | System.out.println(" | ||
+ | } else { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | class Obj { | ||
+ | int obj_var; | ||
+ | |||
+ | Obj(int obj_var) { | ||
+ | this.obj_var = obj_var; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | class ObjOverride { | ||
+ | int obj_var; | ||
+ | |||
+ | ObjOverride(int obj_var) { | ||
+ | this.obj_var = obj_var; | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public boolean equals(Object obj) { | ||
+ | if (obj instanceof ObjOverride) { | ||
+ | return true; | ||
+ | } else { | ||
+ | return false; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | public class EqualsEx2 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | String str1 = new String(" | ||
+ | String str2 = new String(" | ||
+ | |||
+ | if(str1 == str2) { | ||
+ | System.out.println(" | ||
+ | } else { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | |||
+ | if(str1.equals(str2)) { | ||
+ | System.out.println(" | ||
+ | } else { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | public class HahsCodeEx { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | String str1 = new String( " | ||
+ | String str2 = new String( " | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.identityHashCode(str1)) ; | ||
+ | System.out.println(" | ||
+ | System.identityHashCode(str2)) ; | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | public class HashCodeEx2 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | Hash v1 = new Hash(20); | ||
+ | Hash v2 = new Hash(20); | ||
+ | System.out.println(v1.hashCode()); | ||
+ | System.out.println(v2.hashCode()); | ||
+ | System.out.println ("v1 객체 진짜 해쉬값 :" | ||
+ | System.out.println ("v2 객체 진짜 해쉬값 :" | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | class Hash { | ||
+ | int value; | ||
+ | Hash(int value) { | ||
+ | this.value = value; | ||
+ | } | ||
+ | @Override | ||
+ | public boolean equals(Object obj) { | ||
+ | if(obj instanceof Hash) { | ||
+ | Hash v = (Hash)obj; | ||
+ | return value == v.value; | ||
+ | } else { | ||
+ | return false; | ||
+ | } | ||
+ | } | ||
+ | @Override | ||
+ | public int hashCode() { | ||
+ | return value; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | public String toString(){ | ||
+ | | ||
+ | } | ||
+ | |||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | public class ToStringEx { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | Fruit f = new Fruit(" | ||
+ | System.out.println(f); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | class Fruit { | ||
+ | String name; | ||
+ | String color; | ||
+ | public Fruit(String name, String color) { | ||
+ | this.name = name; | ||
+ | this.color = color; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | public class ToStringEx2 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | Fruit2 f = new Fruit2(" | ||
+ | System.out.println(f); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | class Fruit2 { | ||
+ | String name; | ||
+ | String color; | ||
+ | public Fruit2(String name, String color) { | ||
+ | this.name = name; | ||
+ | this.color = color; | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public String toString() { | ||
+ | return " | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | import java.util.Date; | ||
+ | |||
+ | public class ToStringDateEx { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | Date now = new Date(); | ||
+ | System.out.println(now); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | : | ||
+ | |||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | public class ClonEx { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | String[] arr = {" | ||
+ | String[] arr2 = arr.clone(); | ||
+ | |||
+ | System.out.println(arr == arr2); | ||
+ | for (String v : arr2) { | ||
+ | System.out.println(v); | ||
+ | } | ||
+ | |||
+ | String[] arr3 = new String[arr.length]; | ||
+ | System.arraycopy(arr, | ||
+ | |||
+ | System.out.println(arr == arr3); | ||
+ | for (String v : arr3) { | ||
+ | System.out.println(v); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | ==== System 클래스 ==== | ||
+ | |||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | public class PropertyEx { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | for (String var : System.getenv().keySet()) { | ||
+ | System.out.println(var + " | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Class 클래스 ==== | ||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | public class ClassEx { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | EnvEx env = new EnvEx(); | ||
+ | |||
+ | // 객체를 이용해서 생성 | ||
+ | Class c1 = env.getClass(); | ||
+ | System.out.println(c1.getName()); | ||
+ | |||
+ | // 문자열 주소로 생성 | ||
+ | try { | ||
+ | Class c2 = Class.forName(" | ||
+ | System.out.println(c2.getName()); | ||
+ | } catch (ClassNotFoundException e) { | ||
+ | System.out.println(e.getMessage()); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== String 클래스 ==== | ||
+ | <code java> | ||
+ | package chapter12.string; | ||
+ | |||
+ | public class StringEx { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | int score = 90; | ||
+ | System.out.println(" | ||
+ | |||
+ | //String s = score; // 데이터타입이 달라 에러 | ||
+ | String s = String.valueOf(score); | ||
+ | |||
+ | String s2 = score + ""; | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter12.string; | ||
+ | |||
+ | public class StringEx3 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | String sum = ""; | ||
+ | |||
+ | for (int i=1; i<=5; i++) { | ||
+ | sum += i; | ||
+ | } | ||
+ | System.out.println(sum); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter12.string; | ||
+ | |||
+ | public class StringEx2 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | String name1 = " | ||
+ | String name2 = " | ||
+ | |||
+ | if (name1 == name2) { | ||
+ | System.out.println(" | ||
+ | } else { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | |||
+ | if (name1.equals(name2)) { | ||
+ | System.out.println(" | ||
+ | } else { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | |||
+ | String name3 = new String(" | ||
+ | String name4 = new String(" | ||
+ | |||
+ | if (name3 == name4) { | ||
+ | System.out.println(" | ||
+ | } else { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | |||
+ | if (name3.equals(name4)) { | ||
+ | System.out.println(" | ||
+ | } else { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter12.string; | ||
+ | |||
+ | public class StringEx4 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | String text = "Hello My Name is Hong Gil Dong"; | ||
+ | |||
+ | System.out.println(" | ||
+ | |||
+ | for (int i=0; i< | ||
+ | System.out.println(text.charAt(i)); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter12.string; | ||
+ | |||
+ | public class StringEx5 { | ||
+ | |||
+ | static String s1; | ||
+ | static String s2 = ""; | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | System.out.println(s1); | ||
+ | System.out.println(s2); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter12.string; | ||
+ | |||
+ | public class StringEx6 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | String s1 = ""; | ||
+ | |||
+ | System.out.println(" | ||
+ | System.out.println("" | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter12.string; | ||
+ | |||
+ | public class StringEx7 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | String str = "Hello My Name is Hong Gil Dong"; | ||
+ | |||
+ | System.out.println(str.charAt(6)); | ||
+ | System.out.println(str.equals(" | ||
+ | System.out.println(str.indexOf(" | ||
+ | System.out.println(str.indexOf(' | ||
+ | System.out.println(str.substring(17)); | ||
+ | System.out.println(str.substring(6, | ||
+ | System.out.println(str.toLowerCase()); | ||
+ | System.out.println(str.toUpperCase()); | ||
+ | System.out.println(str.length()); | ||
+ | System.out.println(str.startsWith(" | ||
+ | System.out.println(str.endsWith(" | ||
+ | System.out.println(str.replace(" | ||
+ | System.out.println(str.replaceAll(" | ||
+ | System.out.println(str.toString()); | ||
+ | |||
+ | str = " | ||
+ | System.out.println(str.trim()); | ||
+ | // 모든 공백을 제거하는 방법 | ||
+ | System.out.println(str.replace(" | ||
+ | |||
+ | str = String.valueOf(10); | ||
+ | str = String.valueOf(10.5); | ||
+ | |||
+ | str = " | ||
+ | String[] arr = str.split("," | ||
+ | for (int i=0; i< | ||
+ | System.out.println(i+" | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter12.string; | ||
+ | |||
+ | public class StringEx8 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | String[] str = {" | ||
+ | |||
+ | int sum1 = 0; | ||
+ | for (int i=0; i< | ||
+ | sum1 += Integer.parseInt(str[i]); | ||
+ | } | ||
+ | System.out.println(" | ||
+ | |||
+ | long sum2 = 0; | ||
+ | for (int i=0; i< | ||
+ | sum2 += Long.parseLong(str[i]); | ||
+ | } | ||
+ | System.out.println(" | ||
+ | |||
+ | double sum3 = 0; | ||
+ | for (int i=0; i< | ||
+ | sum3 += Double.parseDouble(str[i]); | ||
+ | } | ||
+ | System.out.println(" | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | ==== String Buffer, StringBuilder 클래스 ==== | ||
+ | |||
+ | <code java> | ||
+ | package chapter12.stringbuffer; | ||
+ | |||
+ | public class StringEx9 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | String str1 = " | ||
+ | String str2 = " | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | |||
+ | // 기본 객체에 + 연산 후 다시 대입 | ||
+ | str1 = str1 + " | ||
+ | System.out.println(" | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter12.stringbuffer; | ||
+ | |||
+ | public class StringBufferEx { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | StringBuffer sb1 = new StringBuffer(" | ||
+ | System.out.println(" | ||
+ | sb1.append(" | ||
+ | System.out.println(" | ||
+ | |||
+ | System.out.println(sb1.toString().equals(" | ||
+ | |||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter12.stringbuffer; | ||
+ | |||
+ | public class StringBufferEx2 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | StringBuffer sb = new StringBuffer(); | ||
+ | |||
+ | // 메서드 체이닝으로 여러 타입의 매개변수값을 StringBuffer 객체의 문자열값에 추가 | ||
+ | sb.append(" | ||
+ | System.out.println(sb); | ||
+ | |||
+ | // 2~3번 인덱스값 삭제 | ||
+ | sb.delete(2, | ||
+ | System.out.println(sb); | ||
+ | |||
+ | // 4번 인덱스값 삭제 | ||
+ | sb.deleteCharAt(4); | ||
+ | System.out.println(sb); | ||
+ | |||
+ | // 5번 인덱스에 == 추가 | ||
+ | sb.insert(5," | ||
+ | System.out.println(sb); | ||
+ | |||
+ | // 6번 인덱스에 1.23 추가(문자열로 변환) | ||
+ | sb.insert(6, | ||
+ | System. out.println(sb); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter12.stringbuffer; | ||
+ | |||
+ | public class StringBufferEx3 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | // 시작 | ||
+ | long start = System.currentTimeMillis(); | ||
+ | |||
+ | String str = ""; | ||
+ | for (int i=0; i< | ||
+ | str += i; | ||
+ | } | ||
+ | |||
+ | // 끝 | ||
+ | long end = System.currentTimeMillis(); | ||
+ | System.out.println( " | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter12.stringbuffer; | ||
+ | |||
+ | public class StringBufferEx4 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | // 시작 | ||
+ | long start = System.currentTimeMillis(); | ||
+ | |||
+ | |||
+ | StringBuffer sb = new StringBuffer(); | ||
+ | for (int i=0; i< | ||
+ | sb.append(i); | ||
+ | } | ||
+ | |||
+ | |||
+ | // 끝 | ||
+ | long end = System.currentTimeMillis(); | ||
+ | System.out.println( " | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | ==== Math 클래스 ==== | ||
+ | |||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | public class MathEx { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | public class RoundEx { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | // 원주율을 소수점 3자리로 반올림 | ||
+ | double v1 = Math.PI * 1000; | ||
+ | double v2 = Math.round(v1); | ||
+ | double v3 = v2 / 1000.0; | ||
+ | System.out.println(v3); | ||
+ | |||
+ | // 한줄로 출력 | ||
+ | System.out.println(Math.round(Math.PI * 1000)/ | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | ==== Wrapper 클래스 ==== | ||
+ | |||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | public class WrapperEx { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | // 정수 10이 Integer 클래스 객체로 변환 (boxing) | ||
+ | Integer i1 = new Integer(10); | ||
+ | Integer i2 = new Integer(10); | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | |||
+ | //i1 객체가 100 정수로 변환 (unboxing) | ||
+ | System.out.println(" | ||
+ | int i3 = 10; | ||
+ | System.out.println(" | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | public class WrapperEx2 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | **문자열을 숫자로 변환** | ||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | public class WrapperEx3 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | String number = " | ||
+ | |||
+ | int i1 = Integer.parseInt(number); | ||
+ | int i2 = new Integer(number).intValue(); | ||
+ | int i3 = Integer.valueOf(number); | ||
+ | |||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | public class WrapperEx4 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | int i = 10; | ||
+ | |||
+ | // 기본형을 참조형으로 형변환(형변환 생략가능) | ||
+ | Integer intg = (Integer)i; | ||
+ | // Integer intg = Integer.valueOf(i); | ||
+ | |||
+ | Long lng = 10L; // Long lng = new Long (100L); | ||
+ | int i2 = intg + 10; // 참조형과 기본형간의 연산 가능 | ||
+ | long l = intg + lng ; // 참조형 간의 덧셈 가능 | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | |||
+ | Integer intg2 = new Integer(30); | ||
+ | int i3 = (int) intg2; // 참조형을 기본형으로 형변환 (형변환 생략가능) | ||
+ | System.out.println ("i3 : "+i3); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | ===== java.util 패키지 ===== | ||
+ | |||
+ | ==== Random 클래스 ==== | ||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | import java.util.Random; | ||
+ | |||
+ | public class RandomEx { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | Random r1 = new Random(42); | ||
+ | Random r2 = new Random(42); | ||
+ | |||
+ | System.out.println(" | ||
+ | for (int i=0; i<5; i++) { | ||
+ | System.out.println(i + " | ||
+ | } | ||
+ | System.out.println(" | ||
+ | for (int i=0; i<5; i++) { | ||
+ | System.out.println(i + " | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | import java.util.Random; | ||
+ | |||
+ | public class RandomEx2 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | Random rand = new Random(); | ||
+ | |||
+ | for (int i=0; i<5; i++) { | ||
+ | System.out.println(rand.nextInt(6)+1); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | ==== Scanner 클래스 ==== | ||
+ | |||
+ | Scanner scan = new Scanner(System.in); | ||
+ | String input = scan.nextLine(); | ||
+ | | ||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | import java.util.Scanner; | ||
+ | |||
+ | public class ScannerEx { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | Scanner scan = new Scanner(System.in); | ||
+ | |||
+ | int cnt = 0; | ||
+ | while(true) { | ||
+ | System.out.println(" | ||
+ | String name = scan.nextLine(); | ||
+ | if("" | ||
+ | System.out.println(name+" | ||
+ | cnt++; | ||
+ | } | ||
+ | System.out.println(" | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | ==== Date 클래스 ==== | ||
+ | |||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | import java.text.SimpleDateFormat; | ||
+ | import java.util.Calendar; | ||
+ | import java.util.Date; | ||
+ | |||
+ | public class DateEx { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | Date now = new Date(); | ||
+ | System.out.println(now); | ||
+ | SimpleDateFormat sf = new SimpleDateFormat(" | ||
+ | System.out.println(sf.format(now)); | ||
+ | |||
+ | Calendar cal = Calendar.getInstance(); | ||
+ | Date d = new Date(cal.getTimeInMillis()); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | ==== Calendar 클래스 ==== | ||
+ | |||
+ | **Calendar 객체를 Date로 변환** | ||
+ | Calendar cal = Calendar.getInstance(); | ||
+ | Date d = new Date(cal.getTimeInMillis()); | ||
+ | |||
+ | Date 객체를 Calendar로 변환 | ||
+ | Date d = new Date(); | ||
+ | Calendar cal = Calendar.getInstance(); | ||
+ | cal.setTime(d); | ||
+ | | ||
+ | Calendar cal = Calendar.getInstance(); | ||
+ | | ||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | import java.util.Calendar; | ||
+ | |||
+ | public class CalendarEx { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | Calendar today = Calendar.getInstance(); | ||
+ | |||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | import java.util.Calendar; | ||
+ | |||
+ | public class CalendarEx2 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | Calendar today = Calendar.getInstance(); | ||
+ | |||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | |||
+ | Calendar cal = Calendar.getInstance(); | ||
+ | cal.set(2020, | ||
+ | System.out.println(" | ||
+ | cal.get(Calendar.YEAR) + "년 " + | ||
+ | (cal.get(Calendar.MONTH) + 1) + "월 " + | ||
+ | cal.get(Calendar.DATE) + " | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | **Date 클래스 사용** | ||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | import java.util.Date; | ||
+ | |||
+ | public class CalendarEx3 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | |||
+ | // 현재일 | ||
+ | int sYear = 2020; | ||
+ | int sMonth = 6; | ||
+ | int sDay = 12; | ||
+ | |||
+ | // 이전일 | ||
+ | int eYear = 2020; | ||
+ | int eMonth = 6; | ||
+ | int eDay = 1; | ||
+ | |||
+ | Date sd = new Date(); | ||
+ | Date ed = new Date(); | ||
+ | |||
+ | sd.setYear(sYear); | ||
+ | sd.setMonth(sMonth-1); | ||
+ | sd.setDate(sDay); | ||
+ | |||
+ | ed.setYear(eYear); | ||
+ | ed.setMonth(eMonth-1); | ||
+ | ed.setDate(eDay); | ||
+ | |||
+ | long temp = (sd.getTime() - ed.getTime()) / (1000L*60L*60L*24L); | ||
+ | int diff = (int)temp; | ||
+ | |||
+ | System.out.println(diff + "일 경과" | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | **Calendar 클래스 사용** | ||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | import java.util.Calendar; | ||
+ | |||
+ | public class CalendarEx4 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | |||
+ | // 현재일 | ||
+ | int sYear = 2020; | ||
+ | int sMonth = 6; | ||
+ | int sDay = 12; | ||
+ | |||
+ | // 이전일 | ||
+ | int eYear = 2020; | ||
+ | int eMonth = 6; | ||
+ | int eDay = 1; | ||
+ | |||
+ | Calendar sCal = Calendar.getInstance(); | ||
+ | Calendar eCal = Calendar.getInstance(); | ||
+ | sCal.set(sYear, | ||
+ | eCal.set(eYear, | ||
+ | |||
+ | long diffSec = (sCal.getTimeInMillis() - eCal.getTimeInMillis()) / 1000; | ||
+ | |||
+ | long diffDay = diffSec / (24*60*60); | ||
+ | |||
+ | System.out.println(diffDay + "일 경과" | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | **GregorianCalendar 클래스 사용** | ||
+ | |||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | import java.util.Calendar; | ||
+ | import java.util.GregorianCalendar; | ||
+ | |||
+ | public class CalendarEx5 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | |||
+ | // 현재일 | ||
+ | int sYear = 2020; | ||
+ | int sMonth = 6; | ||
+ | int sDay = 12; | ||
+ | |||
+ | // 이전일 | ||
+ | int eYear = 2020; | ||
+ | int eMonth = 6; | ||
+ | int eDay = 1; | ||
+ | |||
+ | // | ||
+ | // | ||
+ | // | ||
+ | // | ||
+ | Calendar sCal = new GregorianCalendar(sYear, | ||
+ | Calendar eCal = new GregorianCalendar(eYear, | ||
+ | |||
+ | long diffSec = (sCal.getTimeInMillis() - eCal.getTimeInMillis()) / 1000; | ||
+ | |||
+ | long diffDay = diffSec / (24*60*60); | ||
+ | |||
+ | System.out.println(diffDay + "일 경과" | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | ==== Array 클래스 ==== | ||
+ | |||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | import java.util.Arrays; | ||
+ | |||
+ | public class ArraysEx { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | String[] arr = {" | ||
+ | Arrays.fill(arr, | ||
+ | for (String n : arr) System.out.print(n + "," | ||
+ | System.out.println(); | ||
+ | Arrays.fill(arr, | ||
+ | for (String n : arr) System.out.print(n + "," | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | ===== java.text 패키지 ===== | ||
+ | |||
+ | ==== DecimalFormat 클래스 ==== | ||
+ | |||
+ | **숫자 포맷을 지정할 수 있는 클래스** | ||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | import java.text.DecimalFormat; | ||
+ | |||
+ | public class DecimalFormatEx { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | double[] scores = {90.555, 80.6666, 70.77777, 60.666666, 50.5}; | ||
+ | |||
+ | DecimalFormat df = new DecimalFormat("#,### | ||
+ | |||
+ | for (int i=0; i< | ||
+ | System.out.println(df.format(scores[i])); | ||
+ | } | ||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | import java.text.DecimalFormat; | ||
+ | |||
+ | public class DecimalFormatEx2 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | DecimalFormat df1 = new DecimalFormat("#,### | ||
+ | DecimalFormat df2 = new DecimalFormat(" | ||
+ | |||
+ | System.out.println(df1.format(5500)); | ||
+ | System.out.println(df2.format(5500)); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | ==== Simple date format ==== | ||
+ | |||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | import java.text.SimpleDateFormat; | ||
+ | import java.util.Date; | ||
+ | |||
+ | public class SimpleDateFormatEx { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | Date now = new Date(); | ||
+ | System.out.println(now); | ||
+ | SimpleDateFormat sf = new SimpleDateFormat(" | ||
+ | System.out.println(sf.format(now)); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | import java.text.ParseException; | ||
+ | import java.text.SimpleDateFormat; | ||
+ | import java.util.Date; | ||
+ | |||
+ | public class SimpleDateFormatEx3 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | SimpleDateFormat sdf = new SimpleDateFormat(" | ||
+ | String strDate = " | ||
+ | |||
+ | Date d = null; | ||
+ | try { | ||
+ | d = sdf.parse(strDate); | ||
+ | } catch (ParseException e) { | ||
+ | e.printStackTrace(); | ||
+ | } | ||
+ | System.out.println(d); | ||
+ | |||
+ | SimpleDateFormat sf2 = new SimpleDateFormat(" | ||
+ | System.out.println(sf2.format(d)); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | import java.text.SimpleDateFormat; | ||
+ | import java.util.Calendar; | ||
+ | import java.util.Date; | ||
+ | |||
+ | public class SimpleDateFormatEx2 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | // Calendar 와 Date 간의 변환은 다 음과 같이 한다 • | ||
+ | Calendar cal = Calendar.getInstance() ; | ||
+ | cal.set(2020, | ||
+ | Date day = cal.getTime() ; | ||
+ | SimpleDateFormat sdf1 , sdf2 , sdf3 , sdf4 ; | ||
+ | sdf1 = new SimpleDateFormat (" | ||
+ | sdf2 = new SimpleDateFormat (" | ||
+ | sdf3 = new SimpleDateFormat (" | ||
+ | sdf4 = new SimpleDateFormat (" | ||
+ | System.out.println(sdf1.format(day)); | ||
+ | System.out.println(sdf2.format(day)); | ||
+ | System.out.println(sdf3.format(day)) ; | ||
+ | System.out.println(sdf4.format(day)); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | ===== java.util.regex 패키지 ===== | ||
+ | |||
+ | **정규표현식을 사용하기 위한 패키지** | ||
+ | |||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | import java.util.regex.Matcher; | ||
+ | import java.util.regex.Pattern; | ||
+ | |||
+ | public class ReEx { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | // 소문자 b로 시작하는 | ||
+ | Pattern p = Pattern.compile(" | ||
+ | Matcher m; | ||
+ | |||
+ | // 문자열 bat 확인 | ||
+ | m = p.matcher(" | ||
+ | System.out.println(" | ||
+ | |||
+ | // 문자열 cat 확인 | ||
+ | m = p.matcher(" | ||
+ | System.out.println(" | ||
+ | |||
+ | // 문자열 bed 확인 | ||
+ | m = p.matcher(" | ||
+ | System.out.println(" | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | |1. Pattern 클래스의 static 메서드 copile에 정규표현식을 매개변수로 넣고 객체 생성 Pattern p = Pattern.copile(" | ||
+ | |2. Matcher 클래스를 이용해 생성한 패턴 책체의 matcher() 메서드의 매개변수로 비교할 대상 문자열을 넣어 Matcher 객체 생성| | ||
+ | | | ||
+ | Matcher m; | ||
+ | m= p.matcher(" | ||
+ | | | ||
+ | |3. Matcher 객체에 amtchers() 메서드를 호출해 매칭이 되었는지(true) 안 되었는지(false) 판단 if (m.matchers()) {...}| | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ==== 정규 표현식에 사용되는 문자 ==== | ||
+ | |||
+ | ==== 정규 표현식으로 문자열검증 ==== | ||
+ | **Pattern 클래스의 static 메서드 matches() 메서드 사용** | ||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | import java.util.regex.Matcher; | ||
+ | import java.util.regex.Pattern; | ||
+ | |||
+ | public class ReEx2 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | String[] patterns = {" | ||
+ | " | ||
+ | String[] datas = {" | ||
+ | |||
+ | for(String d : datas) { | ||
+ | System.out.print(d+" | ||
+ | for(String p : patterns) { | ||
+ | Pattern pattern = Pattern.compile(p); | ||
+ | Matcher m = pattern.matcher(d); | ||
+ | if(m.matches()) { | ||
+ | System.out.print(p+", | ||
+ | } | ||
+ | } | ||
+ | System.out.println(); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter12; | ||
+ | |||
+ | import java.util.regex.Matcher; | ||
+ | import java.util.regex.Pattern; | ||
+ | |||
+ | public class ReEx3 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | String source = " | ||
+ | + " | ||
+ | String telpattern = " | ||
+ | String emailpattern = " | ||
+ | String accountpattern = " | ||
+ | |||
+ | Pattern p = Pattern.compile(telpattern); | ||
+ | Matcher m = p.matcher(source); | ||
+ | |||
+ | System.out.println(" | ||
+ | while(m.find()) { // 지정된 패턴 맞는 문자열을 검색 | ||
+ | System.out.println(m.group() + " : " | ||
+ | + m.group(1) + "," | ||
+ | } | ||
+ | |||
+ | p = Pattern.compile(emailpattern); | ||
+ | m = p.matcher(source); | ||
+ | System.out.println(" | ||
+ | while(m.find()) { // 지정된 패턴 맞는 문자열을 검색 | ||
+ | System.out.println(m.group() + " : " | ||
+ | + m.group(1) + "," | ||
+ | } | ||
+ | |||
+ | p = Pattern.compile(accountpattern); | ||
+ | m = p.matcher(source); | ||
+ | System.out.println(" | ||
+ | while(m.find()) { // 지정된 패턴 맞는 문자열을 검색 | ||
+ | System.out.println(m.group() + " : " | ||
+ | + m.group(1) + "," | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ |