본문 바로가기

분류 전체보기

(229)
8.3 Field Declarations (1) Example 8.3-1. Multiply Inherited Fields A class may inherit two or more fields with the same name, either from two interfaces or from its superclass and an interface. A compile-time error occurs on any attempt to refer to any ambiguously inherited field by its simple name. A qualified name or a field access expression that contains the keyword super (§15.11.2) may be used to access such fields ..
[자바] 8.2 클래스 멤버 Example 8.2-1. Use of Class Members package classmember; /** * 8.2-1 클래스 멤버의 사용 * @author DELL * */ public class Test { public static void main(String[] args) { ColoredPoint c = new ColoredPoint(); // error c.reset(); // error } } class Point { int x, y; private Point() { reset(); } Point(int x, int y) { this.x = x; this.y = y; } private void reset() { this.x = 0; this.y = 0; } } class ColoredPo..
람다 표현식과 this 키워드에 관하여 @FunctionalInterface interface MyFunction { R apply(T t); } 위와 같은 함수형 인터페이스가 있을 때 익명클래스를 작성하여 인터페이스의 인스턴스를 얻을 수 있다. MyFunction plus30 = new MyFunction() { @Override public Integer apply(Integer t) { return t + 30; } }; 물론 람다표현식으로도 바꿀 수 있다. MyFunction plus30 = i -> i + 30; 보통의 많은 자료에서, 익명클래스를 람다표현식으로 바꿔가며 설명을 이어간다. 어그런데 혹시, 혹시. 람다를 익명클래스로 바꿀 수는...? 그럴 수 있다. 물론. 하지만 함수형 인터페이스 내에는 default 메소드 또한 다수..
자바 Stream API 자바 1.8에 도입된 Stream API에 대해 알아보자. 다음은 Stream 인터페이스에 적힌 주석의 첫 문장으로, 스트림의 정의를 확인할 수 있다. A sequence of elements supporting sequential and parallel aggregate operations. 번역(야매)을 하자면 원소의 순차적이고 병렬적인 연산을 종합지원하는 시퀀스다. 여기서 시퀀스는 사전적 의미를 따르는거 같고, 원소라 함은 컬렉션, 배열, 파일, 정규표현식 패턴 패처, 난수 생성기, 다른스트림을 예로 들수 있다.(이펙티브자바3) 다음은 우리 팀 멤버의 나이를 담은 ArrayList이다. List teamMemberAge = new ArrayList(); teamMemberAge.add(48); te..
[자바] 옵저버패턴 : java.util.Observer, Observable 이전 포스팅 https://dev-jones.tistory.com/154 [자바] Observer 패턴 '관찰자' 관찰하는 객체를 'Observer' 인터페이스를 구현하도록 하고, 관찰하려는 객체는 'Observable'을 상속한다. 다음예제는 난수를 만들고 이를 'Observer'가 관찰하고, 그 수를 출력하도록 한다. packa dev-jones.tistory.com 에서는 Observer 인터페이스를 직접 구현해 사용했었다. 이번에 소개할 내용은 이미 존재하는 java.util.Observer 인터페이스와 java.util.Observable 클래스를 직접 이용하여 똑같은 예제를 다른 코드로 작성하였다. Observable클래스의 setChanged메소드와 발생한 난수가 notifyObservers..
문자열 검색 전체 알고리즘 package main; import java.util.ArrayList; import java.util.Arrays; /** * 짚더미 문자열(H)와 바늘문자열(N)이 주어졌을 때, * H에 N이 몇번째 자리에서 속하는지 알아내기 * @param args */ public class Main { public static void main(String[] args) { String H = "avava"; String N = "ava"; //ArrayList result = searchNaive(H, N); ArrayList result = kmpSearch(H, N); for(Integer begin : result) { System.out.println("begin : " + begin); } //Ar..
[자바] Chain of Responsibility 패턴 다수의 객체를 사슬(chain)처럼 연결해두고, 그 객체의 사슬을 차례로 돌아다니며 목적한 객체를 결정하는 방법을 생각할 수 있다. 이를 Chain of Responsibility 패턴이라 한다. 예제는 '트러블이 발생해서 누군가 처리해야하는 상황'이다. package chainofresponsibility; /** * 발생한 트러블을 표현하는 클래스 * @author dev-jones * */ public class Trouble { private int number;// 발생한 트러블 번호 public Trouble(int number) { this.number = number; } public int getNumber() { return number; } @Override public String ..
[자바] Template Method 패턴 package templateomethod; public abstract class AbstractDisplay { public abstract void open(); public abstract void print(); public abstract void close(); public final void display() { open(); for (int i = 0; i < 5; i++) { print(); } close(); } } package templateomethod; public class CharDisplay extends AbstractDisplay { private char ch; public CharDisplay(char ch) { this.ch = ch; } @Override pub..
InputStream과 InputStreamReader package main06; import java.io.IOException; import java.io.InputStream; public class Main { public static void main(String[] args) { InputStream in = System.in; while(true) { try { int i = in.read(); System.out.println(i); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } 결과: 다음은 InputStreamReader의 예제이다. package main06; import java.io.IOException; import j..
문자열 검색 기본 용어 정리. 문자열 S가 주어졌을 때 문자열 S의 길이를 |S| 라 하자. S = "rompeprop"일 때, |S| = 9이다. S의 i(0≤i