목록분류 전체보기 (83)
천천히 빛나는
이번 포스팅에서는 [Ladder1, 달팽이 숫자, 파리퇴치]를 다룹니다 1210. Ladder1 import java.io.*; import java.util.*; public class Solution { static int[] dx = { 1, 0, 0 }; static int[] dy = { 0, -1, 1 }; static int[][] map = new int[100][100]; static boolean[][] visited; static class Pair { int first; int second; Pair(int first, int second) { this.first = first; this.second = second; } } static boolean ladder(int r, int..
Day 1에서는 [연구소, 연산자 끼워넣기, 감시, 스타트와 링크, 치킨배달, 톱니바퀴, 인구이동, 퇴사]를 다룹니다. 14502. (골드4) 연구소 import java.io.*; import java.util.*; class Pair{ int first; int second; public Pair(int first, int second) { this.first = first; this.second = second; } public int first() { return first; } public int second() { return second; } } public class Main { static int N, M; static int[][] map = new int[8][8]; static fi..
Day 1에서는 [원재의 메모리 복구하기, 농작물 수확하기]를 다룹니다. 1289. 원재의 메모리 복구하기 (D3) import java.io.*; class Solution { static int initCount(String m) { int cnt = m.charAt(0) - '0'; for(int i = 0; i < m.length()-1; i++) { if(m.charAt(i) != m.charAt(i+1)) cnt++; } return cnt; } public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBu..
ArrayList import java.util.ArrayList; public class ArrayListDemo { public static void main(String[] args) { ArrayList al = new ArrayList(); // Object 타입 al.add("one"); // 넣은건 String이지만 Object 타입으로 저장이 되어짐 => String이 아님! al.add("two"); al.add("three"); } } 연관된 데이터를 관리하는 배열 -> 배열의 크기를 변경할 수 없음 이러한 문제를 해결할 수 있는 것을 Collections Framework가 제공하게 된다. 이 중 ArrayList가 Collections Framework의 기능 중 하나이다. for(..
다형성 (Polymorphism) class A{ public String x(){return "x";} } class B extends A{ public String y(){return "y";} } public class PolymorphismDemo1 { public static void main(String[] args) { A obj = new B(); obj.x(); obj.y(); // 오류 발생 } } 부모 클래스 A, 자식 클래스 B가 있을 때 클래스 B의 객체를 A에 담을 수가 있다. 하지만 클래스 B의 메소드 y를 실행할 수는 없다. 클래스 B의 데이터 형을 클래스 A로 하면 클래스 B는 마치 클래스 A인것처럼 동작하게 된다. class A{ public String x(){retur..
인터페이스 (interface) interface I { public void z(); } public class interfaceTest implements I{ public void z(){}; } 어떤 객체가 있고 그 객체가 특정한 인터페이스를 사용한다면 그 객체는 반드시 인터페이스의 메소드들을 구현해야 한다. interface에 명시 되어 있는대로 클래스의 멤버를 구현해야 컴파일이 된다. public interface Calculatable { public void setOprands(int first, int second, int third) ; public int sum(); public int avg(); } // 개발자 A class Calculator implements Calculata..
접근 제어자 class A{ public String y(){ return "public void y()"; } private String z(){ return "private void z()"; } public String x(){ return z(); } } public class AccessDemo1 { public static void main(String[] args) { A a = new A(); System.out.println(a.y()); System.out.println(a.z()); // 호출불가 System.out.println(a.x()); } } 접근 제어자는 클래스의 멤버(변수와 메소드)들의 접근 권한을 지정한다. 객체를 보호하기 위해서 외부의 접근을 허용하거나 차단해야할 필요..
System.out.println(1); System의 out 이라는 변수에 담겨 있는 객체가 가진 메소드 println을 호출하는 코드이다. System을 인스턴스화한 적이 없음에도 out에 접근할 수 있는 것은 out이 static이기 때문이다. import java.lang.*; public class LibraryDemo1 { public static void main(String[] args) { System.out.println(1); } } import java.lang.*;은 자바 프로그래밍을 위한 필수적인 클래스들을 모아둔 패키지이다. 이를 명시적으로 적어주지 않아도 자바는 자동으로 java.lang.*을 import하게 된다. API (Application Programming Inte..
클래스 패스 class Item{ } class ClasspathDemo { } 이와 같이 ClasspathDemo.java 라는 파일을 만들고 컴파일을 하면 ClasspathDemo.class와 Item.class 파일이 생성되게 된다. 이 중에서 Item.class 파일을 lib이라는 하위 폴더를 생성하고 이동시킨 후 ClasspathDemo를 실행시키면 경로 오류가 난다. I tem.class 파일이 현재 디렉터리에 존재하지 않기 때문에 찾을 수 없다는 오류 메세지를 볼 수 있을 것이다. java -classpath ".;lib" ClasspathDemo2 옵션 classpath로 사용할 클래스의 위치를 가상머신에게 알려줄 수 있다. .은 현재 디렉터리에서 클래스를 찾는다는 뜻이고 ;은 경로와 경로..
전에 다룬 내용이지만 복습 중 자세히 작성하는 것이 좋을 것 같다고 판단되어 static에 대해 다루기로 하였다. package javatutorials.numberstring; import java.util.Scanner; // 입력관련 import java.io.*; // File 관련 class Calculator { static double PI = 3.14; // 클래스 변수 static int base = 0; int left, right; // 인스턴스 변수 public void setOprands(int left, int right){ this.left = left; this.right = right; } public void sum(){ System.out.println(this.left..