전체 글
-
진법 변환Java 2020. 12. 12. 10:16
n진수를 10진수로, 10진수를 n진수로 변환하는 소스코드이다. n진수는 String 타입으로 표현하고 10진수는 int 타입으로 표현한다. n의 범위는 2 = 0; i--) { int num = convertToInt(strNum.charAt(i)); result += num * multiplier; multiplier *= base; } return result; } public static int convertToInt(char c) { switch(c) { case 'A': case 'a': return 10; case 'B': case 'b': return 11; case 'C': case 'c': return 12; case 'D': case 'd': return 13; case 'E': ca..
-
문자열 뒤집기 예제Java 2020. 12. 10. 09:12
JAVA에서 String형 데이터를 뒤집어서 출력하는 예제이다. char[] 타입 배열변수에 역순의 데이터를 저장하고 저장된 String type으로 변환해서 뒤집는 방법 public class ReverseString { public static void main(String[] args) { String test = "Dave"; System.out.println("Origin string data: " + test); char[] charArr = new char[test.length()]; int j = 0; for(int i = test.length()-1; i >=0; i--) { charArr[j] = test.charAt(i); j++; } test = String.valueOf(charA..
-
IP주소 확인ETC 2020. 12. 8. 08:51
내부 IP와 외부 IP 1. 외부 IP (external IP, public IP) 인터넷 서비스 제공 업체(Internet Service Provider)에서 인터넷 사용자에게 할당하는 IP 주소 DNS에 의해 조회되는 대상 터미널을 통한 외부 IP 확인 방법 nslookup과 openDNS를 사용하여 확인: nslookup myip.opendns.com. resolver1.opendns.com curl을 사용하여 확인: curl ipecho.net/plain ; echo 또는 curl ifconfig.me; 2. 내부 IP (internal IP, local IP) ISP로 부터 할당받은 IP에 해당하는 회선을 라우터(또는 라우팅 기능을 가진 공유기)에 연결할 때 라우터가 사용자에게 할당하는 IP 주..
-
형변환 (type casting)Java 2020. 12. 4. 08:41
문자열 -> 숫자 String -> int, long, short String strNum = "255"; int intNum1 = Integer.parseInt(strNum); int intNum2 = Integer.valueOf(strNum); long longNum = Long.parseLong(strNum); short shortNum = Short.parseShort(strNum); String -> double, float String strNum = "255"; double doubleNum = Double.valueOf(strNum); float floatNum = Float.valueOf(strNum); 숫자 -> 문자열 int -> String int intNum = 10; String..
-
라이브러리를 사용한 배열 정렬Java 2020. 11. 30. 10:29
별도의 정렬 알고리즘 구현 없이 라이브러리를 사용하여 배열을 정렬한다. import java.util.*; public class Exam { public static void main(String[] args) { Integer[] intArr = {412, 284, 562, 774, 142}; Arrays.sort(intArr); //Sort array by ASC for(Integer num : intArr) { System.out.print(num + " ");} System.out.println(""); Arrays.sort(intArr, Collections.reverseOrder()); //Sort array by DESC for(Integer num : intArr) { System.out..
-
Map의 정렬Java 2020. 11. 28. 09:27
key 기준으로 자동정렬 되는 Map 타입들 HashMap, TreeMap은 entry를 추가할 때마다 key를 기준으로 오름차순 정렬되어 저장된다. public class Exam { public static void main(String[] args) { Map map = new HashMap(); //Map map = new TreeMap(); map.put("3", "Dave"); map.put("1", "Mike"); map.put("4", "John"); map.put("2", "Judy"); for(Map.Entry entry : map.entrySet()) { System.out.println("key: " + entry.getKey() + ", value: " + entry.getValu..
-
Collection FrameworkJava 2020. 11. 24. 08:36
Collection Famework의 개요 다수의 객체를 효율적으로 관리하기 위해 JAVA는 객체를 모아서 조작하는 기능들을 한데 모아 framework로 제공하는데 이를 Collection framewok라 한다. 배열을 사용하여 Collection framework를 대채할 수 있지만 다음과 같은 단점이 있다. 배열은 초기화 시 그 크기가 고정되어 확장하기 어렵다. 중간에 위치한 객체를 제거하는 경우 인덱스는 남아있으나 참조만 깨지게 되어 관리하기 어렵게 된다. (비어있는 배열 인덱스를 확인하는 성능비용이 발생된다.) Collection framework는 배열을 통해 객체를 관리할 때의 단점을 해결하고 보다 효율적인 기능을 제공한다. 객체들의 효율적인 추가, 삭제, 검색을위해 java.util 패키..
-
빌더 패턴ETC 2020. 10. 12. 18:02
빌더 패턴은 객체를 생성할 때 사용하는 패턴의 하나이다. 빌더 패턴의 장점 불필요한 생성자를 제거 데이터 순서와 상관없이 객체를 생성할 수 있게 해 줌 빌더 패턴의 예 먼저 빌더 클래스를 통해 생성하기 원하는 Info라는 클래스가 다음과 같이 선언되어 있다고 하자. public class Info { private String name; private int age; } 1. Info 객체 생성을 대행해줄 빌더 클래스인 InfoBuilder 작성 public InfoBuilder { private String name; private int age; public InfoBuilder setName(String name) { this.name = name; return this; } public InfoB..