반응형

# 간단 알고리즘

## 홀, 짝 출력

int a = 20;

if (a % 2 == 0) {
	System.out.println("짝수 입니다.");
} else {
	System.out.println("홀수 입니다.");
}

## Switch

Switch (조건식) {
	case 10:
    case 9:
    	System.out.println("999");
        break;
    default:
    	System.out.println("else");
}

## 버블정렬

  • 인접한 두 원소 검사하여 정렬
int[] arr = {50, 11, 48, 99, 120, 3};

int temp;

for (int i = 0; i < arr.length; i++) {
	for (int j = 0; j < arr.length - 1; j++) {
		if (a[j] > a[j+1]) {
			temp = a[j];
			a[j] = a[j+1];
			a[j+1] = temp;
		}
	}
}

for (int i = 0; i < arr.length; i++) {
	System.out.println(arr[i]);
}

## Map

HashMap<String, Integer> hm = new HashMap<>();

// 값 추가.
hm.put("key1", 1);
hm.put("key2", 5);


// key, value 포함여부 확인
if (hm.containskey("key1") && hm.containsValue(1)) {
	System.out.println("포함");
}


// 값 가져오기
hm.get("key1");

## List

List<String> list = new ArrayList<>();

// 값 추가. add(넣을값);
list.add(15);


// 값 삭제. remove(삭제할인덱스);
list.remove(list.size()-1);


// 크기
list.size();

## Array

int[] arr = new int[5];

// 정렬.
Arrays.sort(arr);


// 크기
arr.length;
반응형

'기타' 카테고리의 다른 글

오라클 ORDER BY절 NULL 정렬, ORDER BY 절 CASE  (0) 2022.05.06
타임리프(Thymeleaf)  (0) 2022.05.05
디지털 트윈  (0) 2022.04.28
Collection에서 Map타입과 Set타입의 정의와 둘의 차이점  (0) 2022.04.28
SI, SM  (0) 2022.04.28

+ Recent posts