자료구조
- 데이터 단위와 데이터 자체 사이의 물리적 또는 논리적인 관계
데이터 단위는 데이터를 구성하는 한 덩어리라고 생학하면 된다.
자료구조는 쉽게 말해 자료를 효율적으로 이용할 수 있도록 컴퓨터에 저장하는 벙법을 말한다.
배열
배열은 같은 자료형의 변수로 이루어진 구성요소(component)가 모인 것
- int [] a; // a는 자료형이 int형인 배열 : 형식 A
- int a []; // a는 자료형이 int형인 배열 : 형식 B
* 단순한 int 형이 아니라 int 형인 배열임을 명확하게 나타내는 형식 A쪽이 훨씬 많이 사용된다.
구성요소의 개수가 5개인 배열은 아래와 같이 선언된다.
- a = new int [5]; // 배열 본체를 생성하는 본체에 대한 참조를 생성 (int [5])
- new 연산자가 생성하는 것은 배열 본체에 대한 참조이다. 참조하는 곳이 a에 대입되고 그 결과 배열 변수 a가 배열
본체를 참조하게 된다.
배열의 요솟값을 초기화하며 배열 선언하기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package algorithm;
public class IntArrayInit {
public static void main(String[] args) {
int[] a = { 1, 2, 3, 4, 5 }; // 배열 a의 요소 각각 처음부터 순서대로 초기화 된다.
// 다음과 같이 new 연산자를 사용하여 좀 더 명확하게 선언할 수 있다.
// int [] a = new int [] { 1, 2, 3, 4, 5 };
for (int i = 0; i < a.length; i++) {
System.out.println("a[" + i + "] = " + a[i]);
}
}
}
|
cs |
배열의 복제 (클론)
- 배열의 복제는 아래처럼 clone 메서드를 호출하여 쉽게 만들 수 있다.
- 배열 이름.clone() //배열의 복제
배열의 요소를 하나씩 차례로 살펴보는 과정을 알고리즘 용어로 주사(走査, traverse)
* 주사를 스캔이라고도한다 스캐닝(Scanning)
배열 정렬
- list.sort(Comparator.reverseOrder());
- Arrays.sort(a, Comparator.reverseOrder());
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
|
package algorithm;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import javax.sound.sampled.ReverbType;
public class IntArrayInit {
public static void main(String[] args) {
Integer [] a = { 1, 2, 3, 4, 5 }; // 배열 a의 요소 각각 처음부터 순서대로 초기화 된다.
List<Integer> list = new ArrayList<Integer>();
// 다음과 같이 new 연산자를 사용하여 좀 더 명확하게 선언할 수 있다.
// int [] a = new int [] { 1, 2, 3, 4, 5 };
for (int i = 0; i < a.length; i++) {
System.out.println("a[" + i + "] = " + a[i]);
list.add(i, a[i]);
}
list.sort(Comparator.reverseOrder());
Arrays.sort(a, Comparator.reverseOrder());
//배열이 Object 이어야만 Comparator를 사용할 수 있따.
}
}
|
cs |
Random 함수
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
34
35
36
37
38
39
40
|
package algorithm;
import java.util.Random;
import java.util.Scanner;
public class MaxOfArrayRand {
public static void main(String[] args) {
Random rand= new Random();
Scanner sc = new Scanner(System.in);
System.out.println("키의 최댓값을 구합니다.");
System.out.print("사람 수 : ");
int num = sc.nextInt();
int [] height = new int [num];
System.out.println("키 값은 아래와 같습니다.");
for(int i = 0; i < num; i ++) {
height[i] = 100 + rand.nextInt(90); // random : 1 ~ 89
System.out.println("height[" + i + "] : " + height[i]);
}
System.out.println("최댓값은 " + maxOf(height) + "입니다.");
}
public static int maxOf(int [] a) {
int max = a [0];
for(int i = 1; i < a.length; i++) {
if(a[i] > max) max = a[i];
}
return max;
}
}
|
cs |
* Q1 : 키 뿐만 아니라 사람 수도 난수로 생성하도록 프로그램 작성
- int num = rand.nextInt(21);
System.out.println("사람 수 : " + num);
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
34
35
36
37
38
39
40
41
|
package algorithm;
import java.util.Random;
import java.util.Scanner;
public class MaxOfArrayRand {
public static void main(String[] args) {
Random rand= new Random();
Scanner sc = new Scanner(System.in);
System.out.println("키의 최댓값을 구합니다.");
int num = rand.nextInt(21);
System.out.println("사람 수 : " + num);
int [] height = new int [num];
System.out.println("키 값은 아래와 같습니다.");
for(int i = 0; i < num; i ++) {
height[i] = 100 + rand.nextInt(90); // random : 1 ~ 89
System.out.println("height[" + i + "] : " + height[i]);
}
System.out.println("최댓값은 " + maxOf(height) + "입니다.");
}
public static int maxOf(int [] a) {
int max = a [0];
for(int i = 1; i < a.length; i++) {
if(a[i] > max) max = a[i];
}
return max;
}
}
|
cs |
'알고리즘' 카테고리의 다른 글
Do It! 자료구조와 함께 배우는 알고리즘 - 두 배열의 비교 (0) | 2021.09.19 |
---|---|
Do it! 자료구조와 함께 배우는 알고리즘 입문 : 소수의 나열 (0) | 2021.09.13 |
Do it! 자료구조와 함께 배우는 알고리즘 입문 (자바편) - 직각 이등변 삼각형 출력(Q15~17) (0) | 2021.09.04 |
Do it! 자료구조와 함께 배우는 알고리즘 입문 (자바편) - 다중 루프 (Q12~14) (0) | 2021.09.04 |
Do it! 자료구조와 함께 배우는 알고리즘 입문 (자바편) - 반복 (Q10~11) (0) | 2021.09.02 |