java oop : variable vs array
This commit is contained in:
32
JavaOOP/src/Java04.java
Normal file
32
JavaOOP/src/Java04.java
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
public class Java04 {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// 변수 VS 배열
|
||||||
|
int a = 10, b = 20, c = 30;
|
||||||
|
|
||||||
|
// a + b + c = ? 메소드 처리 -> hap()
|
||||||
|
hap(a, b, c);
|
||||||
|
|
||||||
|
int[] arr = new int[3];
|
||||||
|
arr[0] = 10;
|
||||||
|
arr[1] = 20;
|
||||||
|
arr[2] = 30;
|
||||||
|
|
||||||
|
hap(arr);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 하나의 값마다 파라미터로 받아야함 -> 데이터 이동이 불편
|
||||||
|
public static void hap(int a, int b, int c) {
|
||||||
|
int sum = a + b + c;
|
||||||
|
System.out.println(sum);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 여러 값을 배열로 한번에 이동
|
||||||
|
public static void hap(int[] arr) {
|
||||||
|
int sum = 0;
|
||||||
|
for (int i : arr) {
|
||||||
|
sum += i;
|
||||||
|
}
|
||||||
|
System.out.println(sum);
|
||||||
|
}
|
||||||
|
}
|
||||||
40
JavaOOP/src/Java05.java
Normal file
40
JavaOOP/src/Java05.java
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
public class Java05 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// 2차원 배열
|
||||||
|
int[][] a = new int[3][3];
|
||||||
|
a[0][0] = 1;
|
||||||
|
a[0][1] = 2;
|
||||||
|
a[0][2] = 3;
|
||||||
|
|
||||||
|
a[1][0] = 1;
|
||||||
|
a[1][1] = 2;
|
||||||
|
a[1][2] = 3;
|
||||||
|
|
||||||
|
a[2][0] = 1;
|
||||||
|
a[2][1] = 2;
|
||||||
|
a[2][2] = 3;
|
||||||
|
|
||||||
|
for (int i = 0; i < a.length ; i++) {
|
||||||
|
for (int j = 0; j < a[i].length; j++) {
|
||||||
|
System.out.print(a[i][j] + "\t");
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 가변길이 배열
|
||||||
|
int[][] star = new int[5][];
|
||||||
|
star[0] = new int[1];
|
||||||
|
star[1] = new int[2];
|
||||||
|
star[2] = new int[3];
|
||||||
|
star[3] = new int[4];
|
||||||
|
star[4] = new int[5];
|
||||||
|
|
||||||
|
for (int i = 0; i <star.length ; i++) {
|
||||||
|
for (int j = 0; j < star[i].length ; j++) {
|
||||||
|
star[i][j] = '*';
|
||||||
|
System.out.print((char) star[i][j]);
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user