8 [인강] 자바의정석 ch05
Machine translation — switch to KO for the original.
| 배열의 생성과 선언, 배열의 인덱스
같은 타입의 여러 변수를 하나의 묶음으로 다루는 것
int[] score = new int[n]; //java 스타일, 둘 다 가능
int score[] //c언어 스타일
참조변수와 인덱스(연속적)
score 0, 1, 2, 3, n-1
배열의 선언 = 참조변수의 선언
배열의 요소: 저장공간에 저장된 값
| 배열의 길이, 배열의 초기화
int[] arr = new int[5];
int tmp = arr.length;
일단 실행시, 배열의 길이를 바꿀 수 없다 //공간부족하면 새로 만들어서 배열을 옮겨야 한다, 여유공간 알 수 없음
int[] score = new int[n];
for (int i=0; i System.out.println(score[i]); //배열의 모든 요소 출력 } 배열의 초기화: 각 요소에 처음으로 값을 저장 int[] score = { 1, 2, 3, 4, 5 }; | 배열의 출력 int[] iArr = { 100, 95, 80, 70, 60 }; char[] chArr = { a, b, c, d, e }; System.out.println (chArr); //char의 경우에만 출력가능, int 출력시 I@14318bb와 같은 문자열 출력 (iArr[i]); (Arrays.toString(iArr)); //Arrays 클래스의 import문 추가필요, ctrl+shift+O | 배열의 활용 1. 평균 구할 때 float로 형변환하여 소수점 고려 2. 최대/최소 구하기 - max, min을 처음 값으로 초기화, for문, if-else 활용하기 3. 배열 요소를 섞기 //로또번호 생성 - 임의의 두 값을 바꾸기*100(여러번, numArr.length) for문을 이용 끝까지 섞을 필요없이 앞에 6개 수만 랜덤으로 섞에서 구할 수 있다 | String 배열 String[] name = new String [n]; //여러 개의 문자열을 담을 수 있는 배열을 생성 String[] name = {Kim, Park, Yi}; 기본적으로 null 저장 // 참조형 변수의 기본값 char - '\u0000' byte, short, int - 0 long - 0L float - 0.0f double - 0.0d || 0.0 참조형 - null 1. 가위, 바위, 보 랜덤출력 for(int i=0;i<10;i++) { int tmp = (int)(Math.random()*3); System.out.println(strArr[tmp]); }
int tmp = int[0] int[0] = int[n] int[n] = int tmp
// 범위가 100까지인 수에서 6개 난수 배열을 구하려면,
boolean - false
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?