java0116

 

final

  • class

    상속을 받을 수 없다.

    상속을 받는 이유 : 물려받아서 수정하고, 오버라이딩하고 ... --> 불가

    final class는 처음에 만들어 놓은 모양 그대로만 사용할 수 있다.

     

  • method

    overriding(재정의) 불가

    클래스 상속은 가능, 클래스 안의 다른 메소드는 가능.

    final이 붙은 메소드만 오버라이딩 불가.

 

클래스가 파이널이면, 클래스 상속 불가. 그안의 메소드도 건들일 수 없다. ==> final method 효과

일반 클래스에서 한 메소드만 final면, 그 메소드만 overriding 불가.

 

  • variable

    상수가 된다. 항상 변하지 않는 값.

     

    ex) math.class

    final class ==> 모든 메소드와 변수는 final. 즉, 고칠 수 없다.

     

     

 

**static

field variable, method, block 에 붙힐 수 있다.

 

생성이 되는 위치와 시점이 다르다.

 

소스파일 --컴파일--> 실행파일 ----> ~~~

.src .class

 

컴파일 하다가 static 을 만나면 인식하고,

실행버튼 누르면. main이 실행되기 전에 (컴파일 시점) static붙은 애들은 컴파일러가 미리 class area에 미리 만들어 놓는다. 1개만.

 

원래는 main이 실행되어야 객체가 만들어짐

count변수는 객체 생성을 해야 만들어진다.

static 변수를 사용할때는 객체 생성을 안해도 된다.

 

 

 

타 클래스에서 static변수를 사용하고 싶을때

==> 클래스이름.변수명

 

 

 

static method 안에서는 this, super 사용 불가능

main이 실행되기전에 생성되므로, this나 super가 없어서 error발생

'Computer Science > Languages' 카테고리의 다른 글

[java] 쓰레드(Thread)  (0) 2019.01.24
[java] 예외처리  (2) 2019.01.24
[java] Collection 활용  (0) 2019.01.17
[java] interface  (0) 2019.01.16
JAVA 입출력  (2) 2019.01.08

1. 개념

https://namu.wiki/w/%EB%8B%A4%EC%9D%B5%EC%8A%A4%ED%8A%B8%EB%9D%BC%20%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98



2. 기본 문제(BOJ)

https://www.acmicpc.net/problem/1753



3. 소스

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<stdio.h>
#include<iostream>
#include<vector>
#include<queue>
 
 
#define INF 987654321
#define MAX_E 300001
 
using namespace std;
 
typedef struct NODE {
    int end;
    int weight;
};
 
int dist[20000= { 0, };
vector<NODE> map[MAX_E];
 
int start;
 
 
void dijkstra() {
 
    priority_queue <pair<intint>> pq;
    pq.push({ 0,start }); //시작점 삽입
 
    while (!pq.empty()){//큐가 빌때까지 반복 
        int now_node = pq.top().second; 
        int cost = -1 * pq.top().first;
        pq.pop(); //제일 우선순위 높은 ... (?) 노드 삭제
 
        for (int k = 0; k < map[now_node].size(); k++) { //now_node에 연결된 정점 체크
        
            int new_val = dist[now_node] + map[now_node][k].weight; 
            int before_val = dist[map[now_node][k].end];
 
            if (new_val < before_val) {
                dist[map[now_node][k].end] = new_val;
                pq.push({ -1 * new_val , map[now_node][k].end });
            } // 간선 weight 갱신
            
 
        }
    }
 
}
 
int main() {
 
    int v, e; //v:정점 e:간선
    
    int from, to, w;
 
    int i, j;
 
    scanf("%d %d"&v, &e);
    scanf("%d"&start); //시작점
 
    for (i = 1;i <= e;i++) {
        scanf("%d %d %d"&from, &to, &w);
        map[from].push_back(NODE{to,w});
    }
 
    for (i = 1;i <= v;i++) {
        dist[i] = INF;
    }
 
    dist[start] = 0;
 
    dijkstra();
 
 
    for (i = 1;i <= v;i++) {
        if (dist[i] != INF) printf("%d\n", dist[i]);
        else printf("INF\n");
    }
 
    return 0;
}
 
cs




처음에 map 벡터에 연결된 정점들과 해당 weight를 저장해준다.

우선순위 큐는 min_heap ! (weight이 작은것부터 방문하는 이유)

* 우선순위 큐는 기본적으로 큰 원소가 pop의 우선순위를 가지기 때문에,

음의 가중치로 바꾸어 우선순위 큐에 넣는다. (-1을 곱하는 이유)

  ex) weight이 10, 9, 8 이라는 값이 들어왔다면 우선순위는 1 2 3 이어야함.

'Computer Science > Algorithms' 카테고리의 다른 글

[java] Dijkstra 다익스트라 알고리즘  (0) 2019.02.19
Kruskal 크루스칼 알고리즘  (0) 2019.02.18
DisjointSets  (0) 2019.02.18
[java] Insertion Sort 삽입정렬  (0) 2019.01.28
Permutation 순열  (0) 2019.01.17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    //입력
        
        Scanner sc = new Scanner(System.in); // jdk5 추가됨
        int num = sc.nextInt();
        
        //빠르다
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s = br.readLine(); // 문자열로만 리턴됨, 사용자가 입력한 한 줄을 통째로 문자열로 받아옴.
        s="20";
        int num = Integer.parseInt(s); //숫자로 변경
        //int num = Integer.valueOf(s); //내부적으로  parseInt를 다시 호출.
        s="1 2 3 4 5";
        String[] srr = s.split(" ");
        
        
        
        //출력
        System.out.println(); //느리다. 한번에 묶어서 출력하는 것이 좋다.
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        bw.write("abcdefg\n"); //줄 안바꿔줌, \n
cs


'Computer Science > Languages' 카테고리의 다른 글

[java] 쓰레드(Thread)  (0) 2019.01.24
[java] 예외처리  (2) 2019.01.24
[java] Collection 활용  (0) 2019.01.17
[java] interface  (0) 2019.01.16
[Java] final, static  (0) 2019.01.16

+ Recent posts