JAVA 06 배열/스트링/콜렉션
반응형
String을 사용 할 때는 == 사용금지
- 대신에 equals()를 사용
1 2 3 4 5 6 7 8 9 10 11 12 13 | String str1 = "abcde"; String str2 = "abcde"; if (str1 == str2) { //주소 System.out.println("같다"); } else { System.out.println("다르다"); } if (str1.equals(str2)) { //값 System.out.println("같다"); } else { System.out.println("다르다"); } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 | String str3 = new String("abcde"); String str4 = new String("abcde"); if (str3 == str4) { //주소 System.out.println("같다"); } else { System.out.println("다르다"); } if (str3.equals(str4)) { //값 System.out.println("같다"); } else { System.out.println("다르다"); } | cs |
1 2 3 4 | StringBuffer sb = new StringBuffer("abc"); System.out.println(sb); sb.append("d"); System.out.println(sb); | cs |
1 2 3 | System.out.println(str1.charAt(0)); System.out.println(str1.length()); System.out.println(str1.substring(0, 2)); | cs |
1 2 3 4 5 6 7 8 | String phon = "010-0000-7777"; String[] phonArr = phon.split("-"); for (int i = 0; i < phonArr.length; i++) { System.out.println(phonArr[i]); } System.out.println(); | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | String phon1 = "021571577"; String[] phonArr1 = phon1.split("-"); for (int i = 0; i < phonArr1.length; i++) { System.out.println(phonArr1[i]); } String ch = phon1.replace("-", ""); System.out.println(ch); String end = ch.substring(0, ch.length()-4); System.out.println(end); if(ch.substring(0, 2).equals("02")) { System.out.println(ch.substring(0, 2) + " " + end.substring(2) + " " + ch.substring(ch.length()-4, ch.length())); } else { System.out.println(ch.substring(0, 3) + " " + end.substring(3) + " " + ch.substring(ch.length()-4, ch.length())); } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | int[][] score2 = new int[][] { {100, 90, 80}, {80, 20, 30}, {70, 90, 100}, {30, 90, 60}, {40, 100, 100} }; System.out.println("번호\t총점" ); System.out.println("================="); for (int i = 0; i < score2.length; i++) { int sum2 = 0; System.out.print((i+1) + "\t"); for (int j = 0; j < score2[i].length; j++) { sum2 += score2[i][j]; } System.out.println(sum2); } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | int[][] score3 = new int[][] { {100, 90}, {80}, {70, 90, 100}, {30, 90, 60}, {40, 100} }; System.out.println("번호\t평균" ); System.out.println("================="); for (int i = 0; i < score3.length; i++) { int sum2 = 0; double avg = 0.0; System.out.print((i+1) + "\t"); for (int j = 0; j < score3[i].length; j++) { sum2 += score3[i][j]; } avg = sum2/(double)score3[i].length; System.out.println(avg); } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import java.util.*; ArrayList list = new ArrayList(); list.add(1); list.add("2"); list.add("abd"); list.add(4); list.add(4, "다섯번째"); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } list.add("666"); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } HashMap map = new HashMap(); map.put(1, "일"); map.put(2222, "이"); map.put("삼", 33333); map.put("weight", 107.4); System.out.println(map.get(1)); | cs |
반응형
'development' 카테고리의 다른 글
JAVA 08 객체지향 프로그래밍(2) (0) | 2017.12.27 |
---|---|
JAVA 07 객체지향 프로그래밍(1) (0) | 2017.12.27 |
JAVA 05 배열 (0) | 2017.12.27 |
JAVA 04 반복문(While) (0) | 2017.12.27 |
JAVA 03 반복문(for) (0) | 2017.12.27 |
댓글
이 글 공유하기
다른 글
-
JAVA 08 객체지향 프로그래밍(2)
JAVA 08 객체지향 프로그래밍(2)
2017.12.27 -
JAVA 07 객체지향 프로그래밍(1)
JAVA 07 객체지향 프로그래밍(1)
2017.12.27 -
JAVA 05 배열
JAVA 05 배열
2017.12.27 -
JAVA 04 반복문(While)
JAVA 04 반복문(While)
2017.12.27