<배열>
"동일한" 크기의 메모리 공간이 연속
ex. int a 같은 변수 여러 개를 만들어라
객체변수로 생각해도 무방하다.
-------------------------★
똑같은 사이즈의 변수가 반복되면 배열
다른 데이터 타입이 섞여 있으면 인스턴스
-------------------------★
[ ] :배열첨자
int [여기 있어도 됨] a [ ] = new int[3]
변수 선언 데이터 할당
4바이트짜리 데이터(int) 3개를 할당한다 = heap영역(인스턴스 들어가는 자리)에 총 12바이트를 할당한다.
new: 힙 영역에 메모리 할당
-> 4바이트짜리(int) 여러 개가 있고, 그 데이터 묶음 장소의 이름을 a라고 한다 .
-> a[0], a[1], a[2] 처럼 소속된 변수 뒤에 번지수 부여한다(0부터 시작, 즉 index 생성)
1)변수 초기화: 메모리 영역에 데이터 입력
a[1] = 99;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public class Array_test {
public static void main(String[] args) {
String[] str = new String[5]; //상수 문자열 다섯 개를 가리킬거다 선언
str[0] = "말미잘";
str[1] = "해삼";
str[2] = "미역";
str[3] = "다시마";
str[4] = "멍게";
for(int i = 0; i < str.length; i++) {
System.out.println(str[" + i + "] = " + str[i]);
}
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
2)변수 할당과 초기화를 동시에
int[] a = {변수1, 변수2, 변수3}
1
2
3
4
5
6
7
8
9
10
11
12
|
public class Array_Hap {
public static void main(String[] args) {
int[] su = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; //new 대신 {}로 할당, 초기화 동시에
int i, hap = 0;
for(i = 0; i < 10; i++) {
hap += su[i];
}
System.out.println("1부터 10까지의 합: " + hap);
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
a.length: 배열의 길이
★배열 뒤에는 반복문이 나온다★
<배열의 장점과 단점>
1) 장점
: 반복적인 연산 작업에 유용하다.
-> 변수가 한 개밖에 없으니까, 변수 한 개로 여러개의 영역을 가리킬 수 있다. 순번 사용.
예를 들어 한 반의 인원을 모두 표기하려면 변수 30개가 필요하지만 배열은 변수 하나로 묶으면 된다.
2) 단점
: 일단 생성되면 크기 변경이 불가하다. 때문에 사실 자바에서는 배열을 잘 쓰지 않는다.
->메모리 낭비의 가능성이 크다.
->귀찮다.
-> 대안으로 vector 사용(크기 얼마든지 변경 가능)
<다차원 배열>
1차 배열이 또 몇개 있는 지에 따라 다차원 배열이 되기도 한다.
그러나 보통 2차 배열을 넘어서는 다차원 배열은 거의 사용하지 않는다.
바둑판 모양을 생각해보자, 좌표 형태로 접근하면 쉽다.
int [ ] [ ] a = new int [3(행,세로)] [4(열,가로)]
-> 4바이트 짜리가 3개 있는 배열이 4개 있다. 4 x 3 x 4. 행렬에 빗대어 생각하자.
★2차 배열 -> 2중 반복문 나온다★
반복문 -> 순번을 찾아가려고 돌리는 것
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class MultiArrayTest {
public static void main(String[] args) {
int[] arr1 = new int[3];
int[][] arr2;
arr2 = new int[2][3];
System.out.println("arr1배열의 열의 수: " + arr1.length + "\n")
System.out.println("arr2배열의 행의 수: " + arr2.length + "\n")
System.out.println("arr2배열의 1행의 열의 수: " + arr2[0].length + "\n")
System.out.println("arr1배열의 2행의 열의 수: " + arr2[1].length + "\n")
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
main 안에서 선언한 arr1 -> 지역변수
address arr1 - hashcode - jvm 안의 address와 매핑 - heap 영역의 new 변수 찾아가
heao new 메모리 생성 - hashtable던져 - hashcode바꿔 - 변수에 대입
★배열은 객체다.
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
|
public class DosInput {
public static void main(String[] args) {
DecimalFormat comma = new DecimalFormat("###,##0");
String cs1;
String cs2;
String s1 = args[0];
int i1 = Integer.parseInt(args[1]);
cs1 = comma.format(i1);
int i2 = Integer.parseInt(args[2]);
cs2 = comma.format(i2);
System.out.println("성명: " + s1);
System.out.println("급여: " + cs1);
System.out.println("세금: " + cs2);
System.out.println("실 수령액: " + comma.format(i1 - i2);
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
String[] args:
메인함수가 실행될 때 문자열로 된 인자 여러개를 받을 수 있다
★Integer.parseInt()★ ->래퍼 클래스(wrapper): 형변환 클래스
객체를 만들지 않고 만드는 메소드
Integer(정수) 클래스 안에 정수로 해석해줄 수 있는 메소드가 있다.
문자열 "100"을 정수 100으로 바꿔줄 수 있는 메소드
"백" -> 100 (X): 무조건 바꿔주는 건 아니다. 바꿀 수 있는 걸 바꿔주는 것이다.
-> 인자 주는 법
실행버튼 옆 작은 삼각형 - run configuration - 1) 클래스 이름부터 확인하기 - 2) main 옆 argument탭에서 인자 주기
'20.03 ~ 20.08 국비교육 > JAVA' 카테고리의 다른 글
클래스 멤버 메소드 (0) | 2020.03.21 |
---|---|
클래스 구조 (0) | 2020.03.21 |
7. 제어문 - 반복문(while문, for문) (0) | 2020.03.21 |
6. 제어문 - 조건문(if문, switch문) (0) | 2020.03.21 |
5. 연산자 (0) | 2020.03.21 |