equals String str 1 new StringHello String str

  • Slides: 39
Download presentation

==과 equals() String str 1 = new String("Hello"); String str 2 = new String("Hello");

==과 equals() String str 1 = new String("Hello"); String str 2 = new String("Hello"); q == 연산자 변수에 저장된 값을 비교 str 1 == str 2 false q equals() 메소드 객체에 저장된 값을 비교 str 1. equals(str 2) true 4

레퍼런스 관계 1: 2: 3: 4: 5: 6: 7: 8: . . . public

레퍼런스 관계 1: 2: 3: 4: 5: 6: 7: 8: . . . public class Equal. Relation { public static void main(String[] args) { String str 1= "Hello"; String str 2= new String("Hello"); String str 3= str 1; String str 4= str 2; 5

예제 1 public class Equal. Relation { public static void main(String[] args) { String

예제 1 public class Equal. Relation { public static void main(String[] args) { String str 1= "Hello"; String str 2= new String("Hello"); String str 3= str 1; String str 4= str 2; System. out. println("== 관계"); System. out. println("str 1 == str 1 : "+(str 1==str 1)); System. out. println("str 1 == str 2 : "+(str 1==str 2)); System. out. println("str 1 == str 3 : "+(str 1==str 3)); System. out. println("str 1 == str 4 : "+(str 1==str 4)); System. out. println("Equal 관계"); System. out. println("str 1 equls str 1 : "+(str 1. equals(str 1))); System. out. println("str 1 equls str 2 : "+(str 1. equals(str 2))); System. out. println("str 1 equls str 3 : "+(str 1. equals(str 3))); System. out. println("str 1 equls str 4 : "+(str 1. equals(str 4))); } } 6

String 객체의 연산 String s = "Hello"; s = s + " World"; 8

String 객체의 연산 String s = "Hello"; s = s + " World"; 8

q 데이터가 다르면 n n q String 에서 new n n str 1 str

q 데이터가 다르면 n n q String 에서 new n n str 1 str 2 String str 1 = “Java 2”; String str 2 = “Hello”; String str 3 = new String(“Java 2”); String str 4 = new String(“Java 2”); “Java 2” String 객체 “Hello” String 객체 str 3 str 4 “Java 2” String 객체 10

String 클래스의 메소드 1 : public class String. Test 2: { 3 : public

String 클래스의 메소드 1 : public class String. Test 2: { 3 : public static void main(String[] args) 4: { 5: String str 1= "Sung. Yong is "; 6: String str 2= "Something Else. "; 7: String str 3= str 1 + str 2; 8: 9: System. out. println("str 1: "+str 1); 10 : System. out. println("str 2: "+str 2); 11 : System. out. println("str 3: "+str 3); 12 : System. out. println("str 1의 크기는 "+ str 1. length() +"입니다. "); 13 : System. out. println("str 2를 소문자로 바꾸면 "+ str 2. to. Lower. Case() +"입니다. "); 14 : System. out. println("str 3의 g을 Q로 바꾸면 "+ str 3. replace('g', 'Q') +"입니다. "); 15 : System. out. println("str 3의 5번째 문자는 "+ str 3. char. At(4) +"입니다. "); // char. At의 문자 위치는 0부터 시작 16 : } 17 : } 12

파일명 : String. Buffer. Test. java(p 139) public class String. Buffer. Test { public

파일명 : String. Buffer. Test. java(p 139) public class String. Buffer. Test { public static void main(String[] args) { String. Buffer sb= new String. Buffer(); // String. Buffer형 변수 생성 sb. append('D'); // char형 sb. append("oldol is a cute doggy. "); // String형 sb. append(2); // int형 sb. append(" years old. "); sb. append(0. 23); // double형 System. out. println(sb); sb. insert(10, "not "); // String형 sb. insert(30, '3'); // char형 System. out. println(sb); } } 14

파일명 : Reverse. String. java(p 140) import java. io. *; public class Reverse. String

파일명 : Reverse. String. java(p 140) import java. io. *; public class Reverse. String { public static void main(String[] args) throws IOException { System. out. println("거꾸로 뒤집을 문자열을 입력해주세요. "); Buffered. Reader in= new Buffered. Reader(new Input. Stream. Reader(System. in)); String str= in. read. Line(); // 키보드로부터 한 줄 입력 String. Buffer sb= new String. Buffer(str); System. out. println(sb. reverse()); // 거꾸로 뒤집은 문자열을 출력 } } 15

String. Tokenizer 클래스 String. Tokenizer my. ST = new String. Tokenizer("01/15/2004", "/"); 클래스이름 변수

String. Tokenizer 클래스 String. Tokenizer my. ST = new String. Tokenizer("01/15/2004", "/"); 클래스이름 변수 클래스이름 문자열 구분자 아직 토큰이 남아 있는지를 조사 while(my. ST. has. More. Tokens()){ System. out. println(my. ST. next. Token()); } 파싱해서 구한 다음 토큰을 반환 17

예제 파일명 : String. Tokenizer. Test. java(p 142) import java. io. *; import java.

예제 파일명 : String. Tokenizer. Test. java(p 142) import java. io. *; import java. util. *; public class String. Tokenizer. Test { public static void main(String[] args) { System. out. println("아무 문자도 입력하지 않고 엔터키를 치면 끝납니다. n"); Input. Stream. Reader is. Reader= new Input. Stream. Reader(System. in); Buffered. Reader in= new Buffered. Reader(is. Reader); int my. Tokens= 0; do{ System. out. print("문자열 입력: "); try{ String str= in. read. Line(); String. Tokenizer my. ST= new String. Tokenizer(str); my. Tokens= my. ST. count. Tokens(); System. out. println("토큰수: "+ my. Tokens); while(my. ST. has. More. Tokens()){ System. out. println(" "+ my. ST. next. Token()); } }catch(IOException e){ System. out. println(e. to. String()); } System. out. println(); }while(my. Tokens!=0); // 입력한 문자가 없을 때까지 반복 } } 18

다차원 배열 int[ ][ ] arr = new int[3][3]; 22

다차원 배열 int[ ][ ] arr = new int[3][3]; 22

예제 파일명 : Multi. Tab. java(p 146) public class Multi. Tab { public static

예제 파일명 : Multi. Tab. java(p 146) public class Multi. Tab { public static void main(String[] args) { int mt[][]= new int[9][9]; // 구구단 표 // 구구단을 배열에 저장 for(int i=0; i<9; i++) { for(int j=0; j<9; j++) { mt[j][i]= (i+1) * (j+1); } } // 구구단표를 출력 for(int i=0; i<9; i++) { for(int j=0; j<9; j++) { System. out. print((j+1) +"x"+ (i+1) +"="+ mt[j][i] +"t"); } System. out. println(); } } } 24

예제 파일명 : Array. Test. java(p 147) public class Array. Test { public static

예제 파일명 : Array. Test. java(p 147) public class Array. Test { public static void main(String[] args) { int[] array 1= { 10, 5, 6, 1, 3 }; // 배열의 초기화 int[] array 2; System. out. println("배열 array 1은 다음과 같습니다. "); for(int i=0; i<array 1. length; i++) // array 1의 크기 만큼 반복 { System. out. println("array 1["+i+"] = "+array 1[i]); } System. out. println("배열 array 2를 생성합니다. "); array 2= new int[array 1. length]; // array 1의 크기만큼 생성 System. out. println("배열 array 1을 배열 array 2로 복사합니다. "); for(int i=0; i<array 1. length; i++) // array 1의 크기 만큼 반복 { array 2[i]= array 1[i]; } System. out. println("배열 array 2는 다음과 같습니다. "); for(int i=0; i<array 2. length; i++) // array 2의 크기 만큼 반복 { System. out. println("array 2["+i+"] = "+array 2[i]); } } } 25

예제 파일명 : My. Array. java(p 150) public class My. Array { public static

예제 파일명 : My. Array. java(p 150) public class My. Array { public static void main(String[] args) { int[][] arr = { { 10, 20, 30, 40 }, { 50, 60 }, { 70, 80, 90 }, }; System. out. println("배열의 행수: "+ arr. length); for(int i=0; i<arr. length; i++) { System. out. println((i+1)+"행의 열수 : "+ arr[i]. length); for(int j=0; j<arr[i]. length; j++) { System. out. println("arr["+ i +"]["+ j +"] = "+ arr[i][j]); } } 26

Vector 클래스의 메소드. . . 7: . . . 10 : 11 : 12

Vector 클래스의 메소드. . . 7: . . . 10 : 11 : 12 : 13 : . . . 18 : . . . 23 : . . . 27 : . . . Vector my. Vector= new Vector(); my. Vector. add. Element("one"); my. Vector. add. Element("two"); my. Vector. add. Element(new Double(3. 3)); my. Vector. add. Element(new Boolean(true)); System. out. println("my. Vector의 "+ (i+1) +"번째 원소: "+ my. Vector. element. At(i)); my. Vector. remove. Element. At(1); my. Vector. insert. Element. At("둘", 1); 파일명 : Vector. Test. java(p 152) 28

Vector 클래스 생성자 설명 Vector( ) 아무 객체도 없는 빈 벡터 Vector( int initial.

Vector 클래스 생성자 설명 Vector( ) 아무 객체도 없는 빈 벡터 Vector( int initial. Capacity) 초기 크기가 initial. Capacity 인 벡터 Vector(int initial. Capacity, int capacity. Increment) 초기 크기가 initial. Capacity이고, 저장 공간 이 부족할 때마다 capacityincrement만큼씩 증가하는 벡터 Vector my. Vector = new Vector( ); Vector my. Vector = new Vector(10, 3); 29

Vector 클래스 메소드 대표 메소드 설명 void add. Element(Object obj) Obj 객체를 벡터의 마지막에

Vector 클래스 메소드 대표 메소드 설명 void add. Element(Object obj) Obj 객체를 벡터의 마지막에 추가 void insert. Element. At(Object obj, int index) Obj 객체를 index 위치에 삽입 void set. Element. At(Object obj, int index) Index 위치에 있는 객체를 obj로 변경 Object element. At(int index) Index 위치에 있는 객체를 반환 Object first. Element( ) 첫번째 객체를 반환 Object lastelement( ) 마지막 객체를 반환 int index. Of(Object obj) 저장된 첫번째 obj 객체의 위치를 반환 int last. Index. Of(Object obj) 저장된 마지막 obj 객체의 위치를 반환 void remove. All( ) 모든 객체를 삭제 void remove. Element(int index) Index 위치에 있는 객체를 삭제 boolean remove. Element(Object obj) 저장된 obj 객체를 모두 삭제 int size( ) 벡터의 크기를 반환 30

시간과 날짜를 다루는 클래스들 q Date 클래스 Date today= new Date(); System. out. println(today);

시간과 날짜를 다루는 클래스들 q Date 클래스 Date today= new Date(); System. out. println(today); q Simple. Date. Format 클래스 Simple. Date. Format date. Form=new Simple. Date. Format("hh시 mm분"); date. Form. format(today); q Calendar 클래스 Calendar now = Calendar. get. Instance(); year = now. get(Calendar. YEAR); 31

Simple. Date. Format 클래스 q 포맷에 사용되는 심볼 생성자 의미 예 y 년도(year) 1996

Simple. Date. Format 클래스 q 포맷에 사용되는 심볼 생성자 의미 예 y 년도(year) 1996 M 월(month) 07 d 일(day in month) 10 h 시(hour in am/pm ( 1 ~ 12)) 12 H 시(hour in day (0~23)) 0 m 분(minute in hour) 30 S 초(second in minute) 55 s 1000분의 1초(milisecond) 987 E 요일(day in week) Tuesday a 오전/오후 (am/pm marker) PM q 예제 Simple. Date. Format date. Form=new Simple. Date. Format("hh시 mm분"); date. Form. format(today); 33

Random 클래스 q 난수 n n 구하는 클래스 import java. util. Random; -2147483648 ~

Random 클래스 q 난수 n n 구하는 클래스 import java. util. Random; -2147483648 ~ 2147483647 q 사용 n 생성 n n Random r = new Random( ); 난수 발생 n int random. Num = r. next. Int( ) % 10; 35

Random 클래스 1 2 3 4 5 6 7 8 9 : : :

Random 클래스 1 2 3 4 5 6 7 8 9 : : : : : import java. util. Random; public class My. Random { public static void main(String[] args) { Random r= new Random(); System. out. println("1 ~ 100 범위의 난수: "+ (Math. abs(r. next. Int() % 100)+1)); 10 : } 11 : } Random r = new Random(); // 난수 발생 % 100 -99~99 사이 값 int random. Num = r. next. Int() % 100; // 음수 제거 + 1 1 ~ 100 사이 값 random. Num = Math. abs(random. Num) + 1; 36

예제 파일명 : Math. Test. java(p 162) public class Math. Test { public static

예제 파일명 : Math. Test. java(p 162) public class Math. Test { public static void main(String[] args) { System. out. println("-5의 절대값: "+ Math. abs(-5)); System. out. println("44. 0의 절대값: "+ Math. abs(44. 0)); System. out. println("20과 50 중 큰 값: "+ Math. max(20, 50)); System. out. println("20과 50 중 작은 값: "+ Math. min(20, 50)); } } 38