# 02 썸네일형 리스트형 [윤성우 열혈자바] 26-2. 람다의 소개 람다의 이해 1 interface Printable {void print(String s);} class Printer implements Printable {public void print(String s) {System.out.println(s);}} class Lambda1 {public static void main(String[] args) {Printable prn = new Printer();prn.print("What is Lambda?");}} -> 익명클래스로 변경 interface Printable {void print(String s);} class Lambda2 {public static void main(String[] args) {Printable prn = new Printa.. [윤성우 열혈자바] 26-1. 네스티드 클래스와 이너 클래스 Static 네스티드 클래스 class Outer {private static int num = 0;static class Nested1 { // Static 네스티드 클래스void add(int n) { num += n; } Outer 클래스의 static 변수 공유! - 직접 접근 허용} static class Nested2 { // Static 네스티드 클래스int get() { return num; }} } Static 네스티드 클래스는 static 선언이 갖는 특성이 반영된 클래스이다. 따라서 자신을 감싸는 외부 클래스의 인스턴스와 상관없이 Static 네스티드 클래스의 인스턴스 생성이 가능하다. class StaticNested {public static void main(String[] ar.. [윤성우 열혈자바] 25-3. 어노테이션 어노테이션의 설명 범위 @Override @Deprecated @SuppressWarnings 어노테이션 관련 문서 - 봐도 되고... 안봐도 됨!! JSR 175 "A Metadata Facility for the Java Programming Language." JSR 250 "Common Annotations for the Java Platform" @Override interface Viewable { public void showIt(String str);} class Viewer implements Viewable { @Overridepublic void showIt(String str) {System.out.println(str);}} 매개변수 타입을 다르게 해서 오버로딩이 되지 않도록 주의.. [윤성우 열혈자바] 25-2. 매개변수의 가변 인자 선언 매개변수의 가변 인자 선언과 호출 class Varargs {public static void showAll(String ... vargs) {System.out.println("LEN : " + vargs.length); for(String s : vargs)System.out.print(s + '\t');System.out.println();} public static void main(String[] args) {showAll("Box");showAll("Box", "Toy");showAll("Box", "Toy", "Apple");}} 가변 인자 선언에 대한 컴파일러 처리 public static void showAll(Stirng...vargs) { . . . }vargs를 배열의 참조변수로 간.. [윤성우 열혈자바] 25-1. 열거형 인터페이스 기반 상수의 정의 : 자바 5 이전의 방식 interface Scale { int DO = 0; int RE = 1; int MI = 2; int FA = 3;int SO = 4; int RA = 5; int TI = 6; } 인터페이스 내에 선언된 변수는 public, static, final이 선언된 것으로 간주 이전 방식의 문제점 interface Animal {int DOG = 1;int CAT = 2;} interface Person {int MAN = 1;int WOMAN = 2;} class NonSafeConst {public static void main(String[] args) {who(Person.MAN); // 정상적인 메소드 호출who(Animal.DOG); // 비정.. [윤성우 열혈자바] 24-1. 컬렉션 기반 알고리즘 정렬 List를 구현한 컬렉션 클래스들은 저장된 인스턴스를 정렬된 상태로 유지하지 않는다.대신에 정렬을 해야 한다면 다음 메소드를 사용할 수 있다. public static void sort(List list)-> Collections 클래스에 정의되어 있는 제네릭 메소드-> 인자로 List의 인스턴스는 모두 전달 가능-> 단, T는 Comparable 인터페이스를 구현한 상태이어야 한다. 리스트 대상 정렬의 예 public static void main(String[] args) {List list = Arrays.asList("Toy", "Box", "Robot", "Weapon");list = new ArrayList(list); // 정렬 이전 출력for (Iterator itr = list.it.. [윤성우 열혈자바] 23-5. Map<E> 인터페이스를 구현하는 컬렉션 클래스들 Key-Value 방식의 데이터 저장과 HashMap 클래스 public static void main(String[] args) {HashMap map = new HashMap(); // Key-Value 기반 데이터 저장map.put(45, "Brown");map.put(37, "James");map.put(23, "Martin"); // 데이터 탐색System.out.println("23번 : " + map.get(23));System.out.println("37번 : " + map.get(37));System.out.println("45번 : " + map.get(45)); // 데이터 삭제map.remove(37); // 데이터 삭제 확인System.out.println("37번 : " + ma.. [윤성우 열혈자바] 23-4. Queue<E> 인터페이스를 구현하는 컬렉션 클래스들 큐 인터페이스 Queue 인터페이스의 메소드들 boolean add(E e) 넣기E remove() 꺼내기E element() 확인하기 boolean offer(E e) 넣기, 넣을 공간이 부족하면 false 반환E poll() 꺼내기, 꺼낼 대상 없으면 null 반환E peek() 확인하기, 확인할 대상이 없으면 null 큐의 구현 public static void main(String[] args) {Queue que = new LinkedList(); // LinkedList 인스턴스 생성!que.offer("Box");que.offer("Toy");que.offer("Robot"); LinkedList는 List와 동시에 Queue를 구현하는 컬렉션 클래스이다.따라서 어떠한 타입의 참조변수로 참조.. 이전 1 ··· 45 46 47 48 49 50 51 ··· 86 다음