9 [인강] 자바의정석 ch05
| 커맨드 라인 입력받기
cmd
C:\jdk1.8\work\ch5\java Ex5_7 abc 123 "Hello world" //문자열 배열로 입력
" " 길이가 0인 배열
Run configuration
alt + Enter //소스파일 위치, cmd에서 실행시 bin에 경로로 입력할 것
환경변수 설정
| 2차원 배열
테이블 데이터 저장
1차원 배열 여러개 모이면 2차원 배열
int[][] score = new int[4][3] //12개 저장공간 생성
int[][] arr = {
{100, 100, 100},
{20, 30, 20}
};
| 2차원 배열의 예제
int[][] score = {
{100, 100, 100},
{20, 20, 20},
{30, 30, 30},
{40, 40, 40}
};
int sum =0;
for (int i = 0; i < score.length; i++) {
for (int j = 0; j <score[i].length; j++) {
System.out.pritnf("score[%d][%d]=%d%n",i,j,score[i][j]);
sum += score[i][j];
}
}
System.out.println("sum=" +sum);
int[][] score = {
{100, 100, 100},
{20, 20, 20},
{30, 30, 30},
{40, 40, 40}
};
int korTotal = 0, engTotal = 0, mathTotal = 0;
System.out.println("번호 국어 영어 수학 총점 평균");
System.out.println("=====================");
for(int i = 0 ; i < score.length ; i++) {
int sum = 0;
float avg = 0.0f;
korTotal += score[i][0];
engTotal += score[i][1];
mathTotal += score[i][2];
System.out.printf("%3d", i+1);
for(int i = 0 ; i < score[i].length ; i++) {
sum += score[i][j];
System.out.printf("%5d", score[i][j]);
avg = sum / (float)score[i].length;
System.out.printf("%5d %5.1f%n", sum, avg);
//질문 답 맞추는 예제
Scanner로 입력받은 값과 [i][j]값을 비교
%s 문자열 출력 지시자
%d 정수 출력 지시자
%n 줄바꿈 지시자
| String 클래스
char[] + 매서드(기능)
내용변경 불가
a = a + b;
a에 "ab"가 저장되는 것이 아니라 새로운 "ab" 메모리 주소가 a에 저장됨
str. charAt() // length() // substring(n, m) 마지막 자리 출력안됨 // equals() // toCharArr()
String str = "ABCD"
char ch = str.charAt(3);
System.out.println(ch);
| Arrays로 배열 다루기 //Math 클래스 Math.round(), Math.random()
equals(), deepEquals(), toString(), deepToString() //deep- 2차원 이상
copyOf(), copyOfRange()
int[] arr = {1,2,3,4,5};
int[] arr2 = Arrays.copyOf(arr, 2, 4}; //arr2 = [2, 3] 범위 끝자리 포함 안 됨
sort() //배열 오름차순 정리
Comments
No comments yet. Be the first!
319 posts in 테크
- 368Supabase 프로젝트 복사하기 (Restore to a New Project)NEW
- 341Migrating from Permanent Access Tokens to Token Exchange — Why Order Matters
- 326Startup & Product Glossary: Terms Every Solo Founder Should Know
- 325Context Management — How I Do It Now
- 324Claude Code Routines vs Cowork Schedule — What's the Difference?