본문 바로가기

java4

String 클래스의 생성자와 메서드 1. String(char[ ] value) 주어진 문자열(value)을 갖는 String 인스턴스를 생성 char[ ] c = {'H', 'e', 'l', 'l', 'o}; String s = new String(c); → s = "Hello" 2. String(StringBuffer buf) StringBuffer 인스턴스가 갖고 있는 문자열과 같은 내용의 String 인스턴스를 생성 StringBuffer sb = new StringBuffer("Hello"); String s = new String(sb); → s = "Hello" 3. char charAt(int i) 지정된 위치(i)에 있는 문자를 알려준다.(i는 0부터) String s = "Hello"; char c = s.charAt(1.. 2023. 8. 5.
순열(Permutation)과 조합(Combination) C++에는 permutation함수가 있는데 자바에는 없다...ㅜ 1. 순열 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 package _1week; public class Permutation01 { public static void swap(int[] list, int i, int j) {// 자리 바꾸기 int temp = list[i]; list[i] = list[j]; list[j] = temp; } public static void print1(int[] arr) { for (int e : arr) { System.out.print(e + " "); } System.out.pri.. 2023. 8. 2.
조합(Combination) 2.2 백트래킹 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 public class Combination02 {// 백트래킹 public static void main(String[] args) { int[] arr = { 1, 2, 3 };// 조합을 만들 배열 boolean[] visited = new boolean[arr.length];// 조합에 뽑혔는지를 확인하기 위한 배열 comb1(arr, visited, 0, 1);// 1개 고르기 } static void comb1(int[] arr, boolean[] visited, int start, int r) {// start : 기준 if (r =.. 2023. 8. 2.
static 키워드 1. 클래스를 설계할 때, 멤버변수 중 모든 인스턴스에 공통으로 사용하는 것에 static 붙임 2. static 변수는 인스턴스 생성하지 않아도 사용 가능 ★3. static 메서드는 인스턴스 변수 사용 불가능 - 이유 : static 메서드가 호출될 때 인스턴스가 존재하지 않을 수 있다. 그래서 클래스 메서드에서 인스턴스변수 사용 금함. 1. 멤버변수 : 클래스 영역에 선언된 변수 1) 클래스변수 : static 붙음 2) 인스턴스변수 : static 안 붙음 2. 지역변수 : 클래스 영역 이외의 영역(메서드, 생성자, 초기화 블럭 내부) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Test{ public void iMethod(){} public static sMethod(.. 2023. 7. 24.