java 알고리즘 강의를 듣고 알게 된 문법들을 정리해보겠습니다.
문자열에서 인자로 주어진 값(index)에 해당하는 문자(캐릭터변수)를 리턴한다.
// 예시
String str = "abcde";
char c = str.charAt(0);
System.out.println(c);
출력문 > a
*존재하지 않는 인덱스 번호를 넘길 시에 공백이 출력된다.
참고:생활코딩
String을 (문자요소하나씩)분리시켜서 문자배열객체를 생성시켜줌.
String str = "abcde";
char[] c = str.toCharArray();
for(int i=0;i<c.length;i++)
System.out.print(c[i]+ " ");
System.out.println(c); // 배열을 바로 출력할 수도 있다.
출력문 > a b c d e abcde
for(변수타입 변수명 : 배열,컬랙션클래스,arraylist명){조건문 or 문장}
//str에 t가 몇개 있는지 구하는 식
String str = "abcde";
char t = 'a';
int answer=o;
for(char x:str.toCharArray()) {if(x==t)answer++;}
문자가 소문자인지 확인해주는 함수. true, false로 반환 해준다.
char b = 'b';
System.out.println(Character.isLowerCase(b));
출력문 > true
코드는 패스 !...
자료형 타입의 범위 중 최소값을 반환해준다.
System.out.println(Integer.MIN_VALUE);
System.out.println(Double.MIN_VALUE);
System.out.println(Long.MIN_VALUE);
System.out.println(Byte.MIN_VALUE);
System.out.println(Float.MIN_VALUE);
출력문 >
-2147483648
4.9E-324
-9223372036854775808
-128
1.4E-45
찾고싶은 문자열의 위치(인덱스)를 알려준다.
String str = "abcde";
System.out.println(str.indexOf("a"));
System.out.println(str.indexOf('b'));
System.out.println(str.indexOf('d'));
System.out.println(str.indexOf('d',2)); // 2번부터 cde에서 'd'를 찾는다.
System.out.println(str.indexOf('d',4)); // -1를 반환
참고 :생활코딩
String str = "abcde";
String str2 = "abc def";
System.out.println(str.substring(2));
System.out.println(str.substring(1,2));
System.out.println(str2.substring(1,5));
출력문 >
cde
b
bc d
: 문자열을 거꾸로 뒤집어 변경해주는 메소드
String str = "Study";
// str를 stringbuilder 객체로 생성
StringBuilder sb = new StringBuilder(str);
// 새로운 string객체에 넣어주려면 tostring()으로 타입변경해준다.
String sbnew = sb.reverse().toString();
System.out.println(sb);
System.out.println(sbnew);
// append는 char(문자)를 추가해주는 메서드
System.out.println(sb.append('M'));
System.out.println(sb);
// 이미 string타입이 된 sbnew는 변경이 적용되지 않는 문자열임을 보여준다.
System.out.println(sbnew);
출력문 >
ydutS
ydutS
ydutSM
ydutSM
ydutS
static으로 선언된 클래스메서드이다. String 형변환에 사용된다.
String 형변환 방법에는 여러가지가 있지만 valueOf에 장점은 형변환 되는 자료형의 종류가 더 다양하다는 것이다.
char[] study = {'s','t','u','d','y'};
System.out.println(study); // 출력 : study
System.out.println(String.valueOf(study)); // 동일하게 study가 출력
System.out.println(study.getClass().getName()); // 타입을 확인하면 배열임을 알 수 있다.
nt n = 30;
System.out.println(String.valueOf(n)); // 정수 형변환
참고: 코딩 시그널 and https://juyeop.tistory.com/31
문자가 알파벳이며 true를 반환해준다.
boolean ret = Character.isAlphabetic('c');
System.out.println(ret);
System.out.println(Character.isAlphabetic('#'));
출력문 >
true
false
문자가 숫자로 된 문자형이면 true를 반환?
System.out.println(Character.isDigit('3')); //true를 출력
대문자 무시하고 문자열이 같은지 비교한다.
String str3 = "Goog";
boolean ret = str3.equalsIgnoreCase("goog");
System.out.println(ret);
출력문 >
true
(String regex,String replacement)
replaceAll(정규식or기존문자,대체문자)
는 문자열에서 특정문자(regex)를 다른 문자(replacement)로 전체 치환해주는 메서드 이다.
참고 :인사이드아웃
표현식
[abc] : a,b,c중 하나의 문자
[^abc] : a,b,c 이외 하나의 문자
[a-zA-z] : a-z,A-Z중 하나의 문자
\d : 한개의 숫자,[0-9]와 동일
^ : 문자열의 시작
. : 임의의 한 문자
등등 ... 많다.
참고 :주발2
String str = "abc.";
str = str.replaceAll(".", "hi");
System.out.println(str);
String str2 = "found7, time: study; Yduts; emit, 7Dnuof";
str2 = str2.replaceAll("[^a-zA-Z]", "");
System.out.println(str2);
출력문>
hihihihi
foundtimestudyYdutsemitDnuof
0 : 48
9 : 57
A : 65
Z : 90
a : 97
z : 122
이정도는 외워주는게 좋다.
두개의 값을 비교하여 더 작은 값을 반환 한다. Max()
은 더 큰 값을 반환한다.
정수와 실수 타입 모두 가능하다.
System.out.println(Math.min(2, 3));
System.out.println(Math.max(2, 30));
출력문>
2
30
문자열을 인자로 받은 진수값로 읽어서 다시 10진수로 반환해준다.
System.out.println(Integer.parseInt("1111",2)); // 2진수를 -> 10진수로 변환
System.out.println(Integer.parseInt("1111",8)); // 8진수를 -> 10진수로 변환
System.out.println(Integer.parseInt("1111",16)); // 16진수를 -> 10진수로 변환
출력문>
15
585
4369
문자열 다루는 함수들는 처음 접해봐서 모르는게 많아 많이 유익했던 강의였다.
한번에 많이 봐서 분명 까먹는게 생길 수 있으니 내가 써봤다는 걸 기억하기 위해 정리를 해보았다.
강의가 어려울까봐 걱정을 했는데 생각보다 난이도는 나랑 잘 맞아서 다행이다.
미리 백준을 풀어보고 있었더니 낯설지 않았고 알고리즘문제 풀 때 고민하는 시간(논리적인해결시간)을 훈련 시켜주는 것 같아 만족스럽다.
강의 : 인프런 자바 알고리즘 문제 풀이 입문