-책 자료내 notion 링크
문제 ~Eureka는 java파일로 업로드
https://github.com/nanandive/algorithm
int[] arr ={5, 26, 1, 74,59,38}
Arrays.sort(arr)[;
for (int i =0; i< arr.length; i++){
System.out.print(arr[i] + "");
Integer[] arr = {5, 26, 1, 74, 59, 38};
ArrayList list = new ArrayList(Arrays.asList(arr));
for (int i : list) {
System.out.print(i + " ");
}
컬렉션 계층 구조의 루트 인터페이스로, 하위 인터페이스에는 List, Map, Set 등 다양한 컬렉션들이 존재한다. Collection에 포함된 메소드는 하위 인터페이스에서도 사용이 가능하다.
size() // 컬렉션의 크기 반환
equals(Object o) // 컬렉션 비교
isEmpty() // 컬렉션이 비어있는지 확인
contains(Object o) // 해당 값이 컬렉션에 있는지 확인
clear() // 컬렉션에 있는 모든 요소 제거
remove(Object o) // 컬렉션 내에서 해당 객체 삭제
removeIf(Predicate<? super E> filter) // 해당 조건에 만족하는 객체 삭제
addAll(Collection<? extends E> c) // 지정된 컬렉션의 모든 요소를 컬렉션에 추가
containsAll(Collection<?> c) // 컬렉션에 지정된 컬렉션의 모든 요소가 포함되어 있는지 여부를 반환
removeAll(Collection<?> c) // 지정된 컬렉션에 공통으로 포함된 컬렉션의 모든 요소를 제거
retainAll(Collection<?> c) // 지정된 컬렉션에 공통으로 포함된 컬렉션의 요소만 유지
Integer[] arr = {5, 26, 1, 74, 59, 38};
ArrayList<Integer> list = new ArrayList<Integer>(Arrays.asList(arr));
Collections.sort(list);
for (int i : list) {
System.out.print(i + " ");
}
max / min : 컬렉션 내의 최댓값 / 최솟값 반환
System.out.println(Collections.max(list));
System.out.println(Collections.min(list));
- add : 리스트 끝에 원소 추가
// List 인터페이스를 구현한 ArrayList를 사용
ArrayList<String> list = new ArrayList<String>();
list.add("one");
list.add("two");
list.add("three");
for (String str : list) {
System.out.print(str + " ");
}
```java
get : 특정 인덱스 값 조회
// 현재 list : one two three
System.out.println(list.get(2));
- remove : 특정 값 삭제
✅ remove(int index) : 인덱스에 위치하는 값 삭제
✅ remove(Object o) : 특정 객체 삭제
```java
// 같은 일을 하는 코드 -> 아래의 코드 중 하나만 실행해야 함
list.remove("three");
list.remove(2);
contains : 특정 원소가 리스트 내에 있는지 판별
// 현재 list : one two
System.out.println(list.contains("one"));
System.out.println(list.contains("three"));
size : 리스트 크기 반환
// 현재 list : one two
System.out.println(list.size());
map.put(1, "one");
map.put(2, "two");
map.put(3, "three");
System.out.println(map.get(1));
System.out.println(map.get(5));
containsKey : 특정 key가 map에 있는지 판별
System.out.println(map.containsKey(1));
System.out.println(map.containsKey(5));
containsValue : 특정 value가 map에 있는지 판별
System.out.println(map.containsValue("one"));
System.out.println(map.containsValue("five"));
remove : 특정 key에 해당하는 값을 삭제
map.remove(1);
// map의 값 출력
for(int key : map.keySet()){
String value = map.get(key);
System.out.println(key + " : " + value);
}
// Set 인터페이스를 구현한 HashSet 사용
HashSet<String> set = new HashSet<String>();
set.add("one");
set.add("two");
set.add("three");
set.add("two");
Iterator<String> iter = set.iterator();
while(iter.hasNext()) {
System.out.println(iter.next());
}
ontainsAll : 한 Set에 있는 원소들이 다른 Set에 전부 포함되는지 확인. 즉, 부분집합인지 판별.
// setA : 2, 5, 9, 12
// setB : 3, 6, 9, 12
System.out.println(setA.containsAll(setB));
addAll : 한 Set에 있는 원소들과 다른 Set 원소들을 합침. 즉, 합집합을 의미함.
// setA : 2, 5, 9, 12
// setB : 3, 6, 9, 12
setA.addAll(setB);
System.out.println(setA.toString());
removeAll : 다른 Set에 포함된 공통적인 원소를 제거. 즉, 차집합을 의미함.
// setA : 2, 5, 9, 12
// setB : 3, 6, 9, 12
setA.removeAll(setB);
System.out.println(setA.toString());
retainAll : 다른 Set에 포함된 공통적인 원소들만 포함. 즉, 교집합을 의미함.
// setA : 2, 5, 9, 12
// setB : 3, 6, 9, 12
setA.retainAll(setB);
System.out.println(setA.toString());
Stack<String> stack = new Stack<String>();
stack.push("one");
stack.push("two");
stack.push("three");
System.out.println(stack.toString());
pop : 스택의 top 값을 반환한 뒤에 삭제
System.out.println(stack.pop());
System.out.println(stack.toString());
peek : 스택의 top 값 조회
// 현재 스택 값 : [one, two]
System.out.println(stack.peek());
System.out.println(stack.toString());
Queue
offer:큐의 뒤에 데이터를 삽입, add도 같은 역할을 하지만, 큐의 크기가 꽉 찼을 경우에 add는
예외를 발생시키지만 offer false를 반환
Queue<String> queue = new LinkedList<String>();
queue.offer("one");
queue.offer("two");
queue.offer("three");
System.out.println(queue.toString());
poll : 큐의 맨 앞에 위치한 값을 반환한 뒤에 삭제
System.out.println(queue.poll());
System.out.println(queue.toString());
peek : 큐의 맨 앞에 위치한 값을 반환
System.out.println(queue.peek());
System.out.println(queue.toString());
max : 두 인자 중 더 큰 값을 반환
System.out.println(Math.max(15, 24));
min : 두 인자 중 더 작은 값을 반환
System.out.println(Math.min(15, 24));
abs : 절댓값을 반환
System.out.println(Math.abs(15));
System.out.println(Math.abs(-8));
pow : 제곱 값을 반환. 앞의 인자는 밑, 뒤의 인자는 지수를 뜻함.
System.out.println(Math.pow(5, 2));
System.out.println(Math.pow(5, 3));
sqrt : 제곱근 반환
System.out.println(Math.sqrt(25));
System.out.println(Math.sqrt(16));
parseint:string을 int 타입 변환 다, 변환할 문자열이 숫자로 이루어져 있어야한다. 또한 진수법 변환에도사용된다
String str = "55";
System.out.println(str.getClass().getName());
int num = Integer.parseInt(str);
// 연산은 숫자형 타입만 가능
System.out.println("연산결과 = " + num + 5);
// 2진수를 10진수로 변환
System.out.println(Integer.parseInt("1010", 2));
valueOf : String을 Integer 객체로 반환. 단, 변환할 문자열이 숫자로 이루어져 있어야 한다. 또는 int 타입을 Integer로 변환할 때도 사용한다.
String str = "55";
System.out.println(Integer.valueOf(str));
int num = 90;
System.out.println(Integer.valueOf(num).getClass().getName());
String str = "Hello World";
System.out.println(str.startsWith("H"));
System.out.println(str.startsWith("h"));
endswith : 문자열이 특정 문자로 끝나는지 판별
equals : String 문자열 값 비교
indexOf : 특정 문자가 대상 문자열의 몇 번째 인덱스에 위치하는지 반환
replace : 특정 문자열을 새로운 문자열로 치환
toLowerCase : 문자열을 소문자로 변환
toUpperCase : 문자열을 대문자로 변환
trim : 문자열 앞뒤의 공백 제거. 단, 문자열 내부의 공백은 replace를 사용해야 함.
charAt : 문자열 특정 위치에 있는 문자 반환
valueOf : 지정된 값을 String으로 변환
contains : 특정 문자열이 포함되어있는지 확인
split : 문자열을 특정 문자열을 기준으로 나눈 후, 배열을 반환