Languages/JAVA
JAVA 실습(3)
인정이애옹
2024. 4. 15. 14:13
위치 : D:\조정인\3. 응용 SW 기초 기술활용\source\day02\src\exam02
Ex01
package exam02;
public class Ex01 { // 배열 시작
/*
int student1Id = 20241401;
int student2Id = 20241402;
...
int student99Id = 20241499; 너무 오래 걸린다.
*/
int[] studentIds = new int[99]; // 배열 이용 : 변수를 묶어서 한꺼번에 선언 -> 배열도 변수의 일종이다.
// int 형 변수 99개 - 동일 자료형임을 강조하기 위해서 자료형에 [] 붙인다!!
// int studentIds[] = new int[99]; 이렇게 쓸 수 있지만 관례적으로 자료형 앞에 붙인다!!
}
Ex02
package exam02;
public class Ex02 {
public static void main(String[] args) {
int[] nums = new int[4]; // int 변수를 4개 생성, 순차자료구조 구현
// nums[0], nums1[1], nums[2], nums[3]
nums[0] = 10;
nums[1] = 20;
nums[2] = 30;
for (int i = 0; i <= 3; i++) {
System.out.println(nums[i]); // 0번째, 1번째, 2번째, 3번째
}// nums[3]은 선언만 하고 값을 넣어주지 않았지만 기본 값이 있기 때문에 0이 나온다.
/* 결과
10
20
30
0
*/
/*
System.out.println(nums[0]); // 10
System.out.println(nums[1]); // 20
System.out.println(nums[2]); // 30
*/
}
}
Ex03
package exam02;
public class Ex03 {
public static void main(String[] args) {
//int[] nums = new int[] {10, 20, 30, 40}; JAVA는 똑똑해서 앞에만 봐도 배열인 줄 안다!
int[] nums = { 10, 20, 30, 40}; // 선언과 동시에 초기화 : 배열인 걸 충분히 알아서 new int[] 생략 가능!
/*
int[] nums;
nums = {10,20,30,40}; 분리해서 쓰는 경우에는 오류난다! 반드시 new int 써줘야 한다!!
앞에 선언 변수와 직접 대입하는 배열을 분리해서 쓸 때는 new int를 써줘야하지만
선언과 동시에 초기화 할 때엔 짧게 써서 new int를 생략가능하다.
*/
for (int i = 0; i <= 3; i++) {
System.out.println(nums[i]);
}
/*
10
20
30
40
*/
}
}
Ex03_2
package exam02;
public class Ex03_2 { //length : 공간의 갯수
public static void main(String[] args) {
// int[] nums = new int[] {10, 20, 30, 40}; // 0, 1, 2, 3
//int[] nums;
//nums = new int[] { 10, 20, 30, 40 };
//nums = { 10, 20, 30, 40 }; // 오류
int[] nums = { 10, 20, 30, 40 }; // 선언과 동시에 초기화
System.out.println("공간의 갯수 : " + nums.length);
for (int i = 0; i < nums.length; i++) {
System.out.println(nums[i]);
}
// 공간의 갯수 : nums.length -> 이 속성을 이용하면 조건식에 i <=5(배열 요소 갯수-1) 굳이 세서 할 필요 없이 편하다!!
// 개발자의 관례 : nums.length을 쓸 땐 <=을 쓰지 않고 < 를 쓴다!!
}
}
/* 결과
공간의 갯수 : 4
10
20
30
40
*/
Ex04
package exam02;
import java.util.Arrays;
public class Ex04 {
public static void main(String[] args) {
String[] strs = {"가나", "다라", "마바"};
for (String str : strs) { // 향상된 for문
System.out.println(str);
}
//Arrays.stream(strs).forEach(System.out::println); -> 나중에 배울 구문! 지금은 위에 향상된 for문으로 사용하자!
/*
for (int i = 0; i < strs.length; i++) {
System.out.println(strs[i]);
}
*/
}
}
/* 결과
가나
다라
마바
*/
Ex05
package exam02;
public class Ex05 {
public static void main(String[] args) {
int[][] nums = new int[2][4];
nums[0][0] = 10;
nums[0][1] = 20;
nums[0][2] = 30;
nums[0][3] = 40;
nums[1][0] = 10;
nums[1][1] = 20;
nums[1][2] = 30;
nums[1][3] = 40;
}
}
Ex06
package exam02;
import java.util.Arrays;
public class Ex06 {
public static void main(String[] args) {
int[][] nums = { {10, 20, 30}, {40, 50, 60} };
// System.out.println(nums.length); 결과 : 2 -> 행의 갯수가 2다!!
//System.out.println(nums[0].length); 결과 : 3 -> 열의 갯수 0번째 있는 열의 갯수 -> 10,20,30 : 3개
// Arrays : 배열의 편의 기능 모음
System.out.println(nums); // 배열을 출력하면 주소값이 나온다.
//System.out.println(Arrays.toString(nums)); // 결과 : [[I@3b07d329, [I@41629346]
// Ex07을 보면 Arrays.toString는 간편하게 배열 안을 보여주는데 여기선 주소를 보여준다
// 그 이유 ? 일차원 배열인데 배열을 출력하는 거니까 주소만 나온다. String일 땐 잘 나온다!
System.out.println(Arrays.deepToString(nums)); // 4차원 배열일 땐 이걸 쓴다!! 간편하게 배열 안에 있는 걸 확인 가능
/*
System.out.println(nums.length); // 행의 갯수
System.out.println(nums[0].length); // 열의 갯수
System.out.println(nums[1].length); // 열의 갯수
for (int i = 0; i < nums.length; i++) { // 행
for (int j = 0; j < nums[i].length; j++) { //
System.out.println(nums[i][j]); // i행, j열
}
}
*/
}
}
/* 결과
[[I@4eec7777
[[10, 20, 30], [40, 50, 60]]
*/
Ex07
package exam02;
import java.util.Arrays; // 다른 폴더 파일에서 불러오기
public class Ex07 {
public static void main(String[] args) {
String[] strs = {"가나", "다라", "마바"};
//System.out.println(strs);
System.out.println(Arrays.toString(strs));
}
}
/*
[가나, 다라, 마바]
*/