알고리즘/이론과 문법 15

Integer 객체 비교

Integer객체 비교public class Test { public static void main(String[] args){ Integer a = new Integer(1); Integer b = new Integer(1); System.out.println(a==1); //true System.out.println(a==b); //false } } int가 아닌 Integer에서는 두 객체의 레퍼런스가 같은지를 비교한다. 위와 같은 상황 정도에서는 헷갈릴 리가 없었겠지만, List 형태에서는 처음에 발견을 못했었음. static List sumLine(List line){ for(int i=0; i

[Java] 배열 clone() 매소드 deep copy

1차원배열public class Test { public static void main(String[] args){ int[] a = {1,2,3,4,5}; int[] b = a; a[0] = 100; for(int num: b){ System.out.println(num); } } } 결과 : 100, 2, 3 int[] b = a.clone();이렇게 바꾸면 결과가 1,2,3으로 나온다.1차원 배열에서는 clone매소드를 사용하면 deep copy가 된다. 2차원배열public class Test { static int[][] map; public static void main(String[] args){ map = new int[3][3]; for(int i=1; i

Union & Find

Union & Find static void union(int a, int b) { int aRoot = find(a); int bRoot = find(b); parent[bRoot] = aRoot; //b는 자기자신을 가리키다가 a를 가리키게 된다 } static int find(int a) { if(parent[a]==a) { return a; } else { parent[a] = find(parent[a]); return parent[a]; } }네트워크연결문제가 이걸 이용한 것. parent[a] = find(parent[a]);이걸로 인해 탐색성능이 향상된다. 하나씩 타고가는게 아니라 모든 하위노드들이 바로 Root에 연결된다.찾는 과정에서 연결해줌.

String, StringBuffer, StringBuilder

String클래스는 불변 객체이기 때문에 문자열 연산이 많은 프로그래밍이 필요할 때 계속해서 인스턴스를 생성하므로 성능이 떨어지지만 조회가 많은 환경, 멀티쓰레드 환경에서 성능적으로 유리. StringBuffer클래스와 StringBuilder클래스는 문자열 연산이 자주 발생할 때 문자열이 변경가능한 객체기 때문에 성능적으로 유리. StringBuffer와 StringBuilder의 차이점은 동기화지원의 유무이고 동기화를 고려하지 않는 환경에서 StringBuilder가 성능이 더 좋고, 동기화가 필요한 멀티쓰레드 환경에서는 StringBuffer를 사용하는 것이 유리. 출처: https://jeong-pro.tistory.com/85 [기본기를 쌓는 정아마추어 코딩블로그]

객체(Object) 정렬

방법1. Comparable 인터페이스 구현package com.company; import java.util.Arrays; import java.util.Collections; public class Main { public static void main(String[] args) { People[] people = new People[5]; people[0]=new People("할배", 50); people[1]=new People("민경", 25); people[2]=new People("초딩", 11); people[3]=new People("아재", 40); people[4]=new People("용수", 25); //정렬 Arrays.sort(people); for(People p:peopl..

Huffman Decoding 코드분석

Hoffman 개념출처: http://wooyaggo.tistory.com/95 Comparable 인터페이스Comparable 인터페이스는 객체간의 비교를 가능하게 해주는 인터페이스이다. 해당 인터페이스를 구현(implements)한 클래스는 반드시 compareTo 메소드를 정의해야 한다.출처: http://wooyaggo.tistory.com/95compareTo 메소드란?코드를 통해 확인해보자. 아래 코드의 -5가 출력 된다. (b를 기준으로 삼았을때 a 는 -5만큼 앞쪽)Integer a = new Integer(5);Integer b = new Integer(10);int result = a.compareTo(b);System.out.println(result);파라메터를 기준으로 삼아 앞쪽에..