본문 바로가기

etc.15

[Java/Python] 문법 비교 정리 #9 Class Java에서의 Class // 실행 클래스 class Playground { public static void main(String[ ] args) { Sample sample = new Sample(); System.out.println(sample.a); // 1 sample.setA(5); System.out.println(sample.a); // 15 sample.setAA(19); System.out.println(sample.a); // 285 } } // 부모 클래스 선언 class TestClass { public TestClass() {} int a = 1; public void setA(int param) { this.a = param; } } // 부모 클래스 상속 class Samp.. 2022. 11. 12.
[Java/Python] 문법 비교 정리 #8 예외 처리 Java에서의 예외처리 String[] arr = new String[5]; int a = 9; try { // 1. 예외 던지기 if(a < 10) throw Exception("10 보다 큰 값이 필요 합니다."); // 2. 예상치 못한 Exception이 발생 String param = arr[a]; } catch(Exception e) { // e에 대한 로깅 및 예외 처리 구현 } finally { System.out.println("try or catch 구문의 종료 지점"); } 예시에서 보게 되면 a 보다 작은 값을 가진 변수가 try 구문에서 분기 처리를 하여 10 보다 작은 경우 catch 구문으로 바로 이동하게 되는 경우와 예상치 못한 예외가 발생 할 수 있는 로직에서 try cat.. 2022. 11. 10.
[Java/Python] 문법 비교 정리 #7 자료구조 - HashMap(Dictionary) Python에서 이야기하는 Dictionary 자료 구조는 타 언어에서는 HashMap이라는 명칭으로 사용되나 두개의 명칭 모두 동일한 자료 구조를 가지고 있습니다. 공통적인 특징으로는 key, value의 한쌍의 형태를 띄고 있으며 중복되는 key를 허용하지 않습니다. Java에서의 HashMap // HashMap 선언 Map map = new HashMap(); map.put("name", "a"); map.put("age", 100); map.put("name", "b"); // 처음 대임된 a -> b로 변경 됩니다. // 결과 // key : name, value : b // key : age, value : 100 for (Object o : map.keySet()) { System.out... 2022. 11. 9.
[Java/Python] 문법 비교 정리 #6 자료구조 - Set Java에서의 Set Set a = new HashSet(Arrays.asList(1, 3, 2, 4, 8, 9, 0)); Set b = new HashSet(Arrays.asList(1, 3, 7, 5, 4, 0, 7, 5)); // 합집합 Set union = new HashSet(a); union.addAll(b); System.out.println(union); // 0, 1, 2, 3, 4, 5, 7, 8, 9 // 교집합 Set intersection = new HashSet(a); intersection.retainAll(b); System.out.println(intersection); // 0, 1, 3, 4 // 차집합 Set difference = new HashSet(a); diff.. 2022. 11. 8.