전체 글(129)
-
배열 출력하기
배열을 출력하는 것에는 대표적으로 2가지 방법이 있다 1. 반복문 1 2 3 4 5 6 7 8 9 10 int[] arr1 = {1,2,3,4,5}; for(int i=0; i
2023.01.19 -
배열 순서대로 정렬하기
배열 안 숫자들을 크기대로 정렬해보자 1) for문 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 int[] arr1 = {5,3,4,2,1}; int temp; for(int i=0; i
2023.01.19 -
자릿수 더하기
문제 1) String으로 형 변환 1-1) 1 2 3 4 5 6 7 8 9 10 11 12 13 public class Solution { public int solution(int n) { String str = ""+n; String[] strarr = str.split(""); int temp = 0; for(String s : strarr){ temp += Integer.parseInt(s); } return temp; } } Colored by Color Scripter cs 1-2) valueOf()를 이용한 형 변환 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import java.util.*; import static java.lang.String.valueOf; pu..
2023.01.19 -
replace vs replaceAll
replace(찾을문자열, 바꿀문자열) 주어진 문자열에서 바꾸고 싶은 특정 문자열이 있을 때 사용하는 함수이다. 1 2 3 4 String str = "ababab"; String result1 = str.replace("ab", "0"); //000 Colored by Color Scripter cs 같은 문자열에 replaceAll을 적용해보겠다. 1 2 3 4 String str = "ababab"; String result2 = str.replaceAll("ab", "0"); //000 Colored by Color Scripter cs replaceAll을 사용했을 때도 replace와 같은 결과가 나왔다. 그렇다면 둘의 차이는 무엇일까? replaceAll(정규식 또는 찾을문자열, 바꿀문자열)..
2023.01.18 -
StringBuilder
대부분 문자열을 쓴다고 하면 String을 떠올릴 것이다. 예를 들어 여러 개의 문자열을 이어서 출력해야 하는 상황이 있다고 해보자. 1 2 3 4 5 6 String str = "홍노아의"; String str1 = "티스토리"; String str2 = "2023년"; String str3 = "1월"; String str4 = str + str1 + str2 + str3; System.out.println(str4); //홍노아의티스토리2023년1월 cs String은 immutable하다. 불변이라는 뜻이다. 한 번 String 객체가 생성되면 값을 바꿀 수 없다. 만약 String 클래스로 생성된 객체가 변경된다면 그건 참조하는 String 객체가 바뀐 것이고 이전 String 객체는 가비지 컬..
2023.01.18 -
숫자 문자열과 영단어
문제 결과 화면 1. if문으로 풀기 if문으로 해당 되는 문자열을 숫자로 바꿔주면 된다 다만 번거롭고 코드가 길어진다. 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 30 31 32 33 34 35 36 37 class Solution { public int solution(String s) { if (s.contains("zero")) { s = s.replace("zero", "0"); } if (s.contains("one")) { s = s.replace("one", "1"); } if (s.contains("two")) { s=s.replace("two", "2"); } if (s.contains("t..
2023.01.18