본문 바로가기

분류 전체보기

(229)
[자바] 재귀함수와 증감연산자 package com.devjones.web.javaAlgo.tree; public class RecursionTest { public static void main(String[] args) { hello(0); } static void hello(int i) { if(i > 10)return; System.out.println(i + " 번째 hello 호출"); hello(i++); } } 위와 같이 재귀함수를 통해 hello()를 10번 실행하려했다. 실행결과로 "0 번째 hello 호출"이 6221번 뜨더니 java.lang.StackOverflowError가 걸려버렸다. 다음과 같이 수정한다. package com.devjones.web.javaAlgo.tree; public class Rec..
자바와 비트마스크, BitSet 자바에는 비트마스크에 관한 라이브러리가 있는데,java.util.BitSet 클래스가 바로 그것이다. 이전 글까지만 해도 직접 구현하였다.만약 실무에 써먹을 일이 있다면(적어도나는) 요 라이브러리를 써먹을 것이다.하나씩 알아보자. /* * BitSets are packed into arrays of "words." Currently a word is * a long, which consists of 64 bits, requiring 6 address bits. * The choice of word size is determined purely by performance concerns. */ private final static int ADDRESS_BITS_PER_WORD = 6; private fin..
비트마스크 (2) 비트마스크 정수의 이진수 표현을 자료구조 기법으로 쓰는 테크닉 집합의 구현 비트마스크의 사용사례로 집합의 구현이 있다. -> N비트 정수 변수는 0부터 N-1까지의 정수 원소를 가질 수 있는 집합이 된다. 이때 원소 i가 집합에 속해 있는지 여부는 2^i을 나타내는 비트가 켜져 있는지 여부로 나타낸다. 예를 들어 여섯 개의 원소를 갖는 집합 {1, 4, 5, 6, 7, 9}을 표현하는 정수는 754임을 다음과 같이 알 수 있다. 2^1 + 2^4 + 2^5 + 2^6 + 2^7 + 2^9 = 10 1111 0010 = 754 피자집 예제 피자집 주문 시스템 0부터 19까지의 번호를 갖는 스무 가지의 토핑이 있으며, 주문시 토핑을 넣기/넣지 않기를 선택할 수 있다. 공집합과 꽉 찬 집합 토핑을 올리지 않은..
비트마스크 (1) 컴퓨터는 모든 정수형 변수를 이진수로 표현한다. 이때 이진수의 한 자리를 비트(bit)라고 부른다. 비트는 0 혹은 1의 값을 가질 수 있다. 예를 들어 부호 없는 8비트 정수형은 여덟 자리 이진수로 표시할 수 있는 모든 정수를 표현할 수 있다. 따라서 8비트 정수형이 가질 수 있는 최소값은 0, 최대값은 255이다. 부호 없는 N비트 정수형 변수는 N자리의 이진수로 쓸 수 있다. 이때 각 비트가 표현하는 값은 2^0부터 2^N-1까지이다. 2^N-1에 해당하는 비트를 최상위 비트(most siginficant bit)라고 부르고, 2^0을 나타내는 비트를 최하위 비트(last significant bit)라고 부른다.
생성자 주입 왜? 1. 불변 2. 누락 3. final
옵션 처리 주입할 스프링 빈이 없어도 동작해야 할 때가 있다. 이때 @Autowired만 사용하면 required 옵션의 기본값이 true로 되어 있어서 자동 주입할 대상이 없으면 오류가 발생한다. 자동주입 대상을 옵션으로 처리하는 방법을 하나씩 살펴보자. package com.devjones.web.autowired; import java.util.Optional; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.annotatio..
람다 package main03; import java.util.function.Consumer; import java.util.function.IntConsumer; public class Main { public static void main(String[] args) { Main m = new Main(); m.run(); } private void run() { int baseNum = 10; // 로컬 클래스 class LocalClass { int baseNum = 11; void printBaseNum() { System.out.println(baseNum);//11 } } // 익명 클래스 Consumer integerConsumer = new Consumer() { @Override publi..
자바에서 제공하는 함수형 인터페이스 docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html java.util.function (Java Platform SE 8 ) Interface Summary Interface Description BiConsumer Represents an operation that accepts two input arguments and returns no result. BiFunction Represents a function that accepts two arguments and produces a result. BinaryOperator Represents an operation u docs.oracle.com 자바에서 기본적으로 제..
함수형 인터페이스 public interface MyInterface { void doIt(); // 추상메소드 하나만 존재해야 함수형 인터페이스로 인정! // 아래 메소드와는 관계x static void printName() { System.out.println("devjones"); } default void printAge() { System.out.println("11"); } } @FunctionalInterface public interface MyInterface { void doIt(); } 이걸 어떻게 쓰는지? package main03; public class Main { public static void main(String[] args) { // 익명 내부 클래스. MyInterface mi = ne..
LinkedList 구현예제 package com.devjones.web.javaAlgo.dynamicArray; public class Main_Linked { public static void main(String[] args) { Node head = new Node(1); head.append(2); head.append(3); head.append(4); //head.retrieve(); //head.delete(2); //head.retrieve(); //head.delete(2); //head.retrieve(); head.delete(1); head.retrieve(); } } class Node { int data;// 보통은 Object 지금은 간단하게 정수값만 취급한다. Node next = null;// 다음 ..