[24.04.23] 내일배움캠프 7일차 JAVA TIL - 연산자, 제어문, 컬렉션
2024. 4. 23. 20:50ㆍT.I.L
오늘 한 일
- 오후 2시 특강 듣기
- 2주차 강의 수강
- 2주차 1주차 HW 컬렉션 사용하여 리팩토링
수기를 남기기에 앞서 교육 내용이 많아, 평소 헷갈렸던 개념 위주로 T.I.L을 작성했다.
연산자
산술 연산자 | + - * / % << >> | 사칙, 비트 연산 |
비교 연산자 | > < >= <= == != | 크기 비교 |
논리 연산자 | && || ! | AND, OR 등 논리 연산 |
대입 연산자 | = ++ -- | 값 저장 등 |
기타 연산 | (type) ? : instance of | 삼항 연산, 자료형 변경 |
연산자의 우선 순위는 산술 > 비교 > 논리 > 대입
Collection
컬렉션은 List / Queue / Set / Map 이 있다.
List (LIFO)
: ArrayList, LinkedList, Stack
함수
add, set, remove, toString([] 형태로 출력),clear
package week02.collection;
import java.util.ArrayList;
public class col1 {
public static void main(String[] args) {
/*
List : 순서, 중복 0
Array -> 정적
List(ArrayList) -> 동적
*/
ArrayList<Integer> intL = new ArrayList<Integer>();
intL.add(1);
intL.add(2);
intL.add(3);
System.out.println(intL.get(2));
intL.set(1,10);
System.out.println(intL.get(1));
intL.remove(0); // index는 자동 완성이라 0만 쓰면 됨
System.out.println(intL.get(0)); // 0번째 인덱스 자리에 1번째 인덱스가 옴
}
}
LinkedList
package week02.collection;
import java.util.LinkedList;
public class col2 {
public static void main(String[] args) {
LinkedList<Integer> link = new LinkedList<Integer>();
link.add(5);
link.add(10);
link.add(3);
System.out.println(link.get(0));
System.out.println(link.get(1));
System.out.println(link.get(2));
System.out.println(link.toString()); // 조회는 느리지만 수정은 빠름
link.add(200);
}
}
Stack
package week02.collection;
import java.util.Stack;
public class col3 {
public static void main(String[] args) {
/*
Stack(LIFO) -> push.peek, pop
*/
Stack<Integer> stack = new Stack<Integer>();
stack.push(1);
stack.push(15);
stack.push(3);
while(!stack.empty())
System.out.println(stack.pop());
stack.push(1);
stack.push(15);
stack.push(3);
System.out.println(stack.size());
}
}
Queue(FIFO)
함수
add, peek, poll
package week02.collection;
import java.util.LinkedList;
import java.util.Queue;
public class col4 {
public static void main(String[] args) {
/*
queue (FIFO)
add peak poll(꺼낸다)
queue : 생성자가 없는 interface
-> 보통 LinkedList 사용
*/
Queue<Integer> queue = new LinkedList<Integer>();
queue.add(1);
queue.add(2);
queue.add(3);
while(!queue.isEmpty())
System.out.println(queue.poll());
}
}
Set
순서가 없고, 중복을 허용하지 않는다.
중복값이 있을 경우 가장 최신값으로 update
함수
add, remove, contains
package week02.collection;
import java.util.HashSet;
import java.util.Set;
public class col5 {
public static void main(String[] args) {
/*
Set(집합) : 생성자가 없는 interface
HashSet, TreeSet과 사용
*/
Set<Integer> set = new HashSet<Integer>();
set.add(1);
set.add(2);
set.add(3);
set.add(4);
for(Integer i : set)
System.out.println(i);
System.out.println(set.contains(2));
}
}
Map
: key-value 구조로 구성되어 있음
함수
put, get, keySet, values
package week02.collection;
import java.util.HashMap;
import java.util.Map;
public class col6 {
public static void main(String[] args) {
/*
Map : key - value pair
key : 중복 X unique
Map -> HasMap, TreeMap 응용
*/
Map<String ,Integer> map = new HashMap<>();
map.put("일",1);
map.put("이",2);
map.put("삼",3);
map.put("사",4);
map.put("사",5); //중복일 경우 가장 최신걸로 Update
for(String s : map.keySet())
System.out.println(s);
for(Integer i : map.values())
System.out.println(i);
System.out.println(map.get("사"));
}
}
HW 2주차 숙제는 배운 내용을 활용하여
switch문, for문의 다양한 형태를 사용하여 구현했다
'T.I.L' 카테고리의 다른 글
[24.04.25] 내일배움캠프 9일차 JAVA TIL - 객체지향 프로그래밍 (1) | 2024.04.25 |
---|---|
[24.04.24] 내일배움캠프 8일차 JAVA TIL - 객체지향 프로그래밍 (0) | 2024.04.24 |
[24.04.22] 내일배움캠프 6일차 JAVA TIL - JVM, 변수 (0) | 2024.04.22 |
[24.04.19] 내일배움캠프 5일차 JAVA TIL - Web 특강 (1) | 2024.04.19 |
[24.04.19] 내일배움캠프 5일차 JAVA TIL - Start ☆ (0) | 2024.04.19 |