문서의 이전 판입니다!
데이터 타입[] 변수명; | 데이터 타입 변수명[]; |
1 2 3 4 5 |
int [] arrInt; int arrInt2[]; double [] arrDouble; String[] arrString; |
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 26 27 28 29 30 31 32 33 34 35 36 |
package chapter06; public class ArrEx { public static void main(String[] args) { int [] arrInt; int arrInt2[]; double [] arrDouble; String[] arrString; } } <code> <code java> package chapter06; public class ArrEx1 { public static void main(String[] args) { int [] arrInt; int arrInt2[] = null ; //System.out.println(arrInt[0]); // 에러 발생 System.out.println(arrInt2[ 0 ]); // 에러 발생하지 않음 } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package chapter06; public class ArrEx2 { public static void main(String[] args) { int [] arrInt = new int [ 5 ]; // 길이가 5개인 배열 객체 생성 System.out.println(arrInt[ 0 ]); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package chapter06; public class ArrEx3 { public static void main(String[] args) { int [] arrInt = new int [ 5 ]; // 길이가 5개인 배열 객체 생성 arrInt[ 0 ] = 1 ; arrInt[ 1 ] = 2 ; arrInt[ 2 ] = 3 ; arrInt[ 3 ] = 4 ; arrInt[ 4 ] = 5 ; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package chapter06; public class ArrEx4 { public static void main(String[] args) { String[] arrStr = new String[ 5 ]; // System.out.println(arrStr[0]); char [] arrChar = new char [ 3 ]; System.out.print(arrChar[ 0 ]* 2 ); System.out.print( '\u0000' * 2 ); } } |
1 2 3 4 5 6 7 8 9 10 11 12 |
package chapter06; public class ArrEx5 { public static void main(String[] args) { String[] arrStr = { "홍길동" , "이순신" , "김유신" }; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package chapter06; public class ArrEx6 { public static void main(String[] args) { String[] arrStr = { "홍길동" , "이순신" , "김유신" }; System.out.println( "배열의 길이 : " +arrStr.length); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package chapter06; public class ArrEx7 { public static void main(String[] args) { String[] arrStr = { "홍길동" , "이순신" , "김유신" }; for ( int i= 0 ; i<arrStr.length; i++) { System.out.println(arrStr[i]); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package chapter06; public class ArrEx8 { public static void main(String[] args) { int [] number = new int [ 100 ]; // 배열 변수에 대입 for ( int i= 0 ; i<number.length; i++) { number[i] = i+ 1 ; } // 배열 변수 출력 for ( int i= 0 ; i<number.length; i++) { System.out.println(number[i]); } } } |
로또번호 자동 추출기
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 26 27 28 29 30 31 32 33 34 |
package chapter06; public class Lotto { public static void main(String[] args) { int [] lotto = new int [ 6 ]; int idx = 0 ; while ( true ) { int number = ( int )(Math.random()* 45 )+ 1 ; boolean insert = true ; for ( int i= 0 ; i<=idx; i++) { if (lotto[i] == number) { insert = false ; break ; } } if (insert == true ) { lotto[idx] = number; idx++; } if (idx == 6 ) break ; } for ( int i= 0 ; i<lotto.length; i++) { System.out.println(lotto[i]); } } } |
args 변수의 배열에 값 입력
1 2 3 4 5 6 7 8 9 10 11 12 |
package chapter06; public class ArrEx9 { public static void main(String[] args) { System.out.println( "첫번째 값 : " +args[ 0 ]); System.out.println( "두번째 값 : " +args[ 1 ]); } } |
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
package chapter06; public class ArrEx10 { public static void main(String[] args) { // 첫번째 int [][] matrix = new int [ 3 ][ 3 ]; matrix[ 0 ][ 0 ] = 1 ; matrix[ 0 ][ 1 ] = 2 ; matrix[ 0 ][ 2 ] = 3 ; matrix[ 1 ][ 0 ] = 4 ; matrix[ 1 ][ 1 ] = 5 ; matrix[ 1 ][ 2 ] = 6 ; matrix[ 2 ][ 0 ] = 7 ; matrix[ 2 ][ 1 ] = 8 ; matrix[ 2 ][ 2 ] = 9 ; // 두번째 int [][] matrix2 = {{ 1 , 2 , 3 }, { 4 , 5 , 6 }, { 7 , 8 , 9 }}; // 세번째 int [][] matrix3 = { { 1 , 2 , 3 }, { 4 , 5 , 6 }, { 7 , 8 , 9 } }; System.out.println( "[첫번째]" ); for ( int i= 0 ; i<matrix.length; i++) { for ( int j= 0 ; j<matrix[i].length; j++) { System.out.print(matrix[i][j]+ " " ); } System.out.println(); } System.out.println( "[두번째]" ); for ( int i= 0 ; i<matrix2.length; i++) { for ( int j= 0 ; j<matrix2[i].length; j++) { System.out.print(matrix2[i][j]+ " " ); } System.out.println(); } System.out.println( "[세번째]" ); for ( int i= 0 ; i<matrix3.length; i++) { for ( int j= 0 ; j<matrix3[i].length; j++) { System.out.print(matrix3[i][j]+ " " ); } System.out.println(); } } } |
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 26 27 28 29 30 31 32 |
package chapter06; public class ArrEx11 { public static void main(String[] args) { int [][][] arrInt = new int [ 3 ][ 3 ][ 3 ]; int value = 0 ; // 값 대입 for ( int i= 0 ; i<= 2 ; i++) { for ( int j= 0 ; j<= 2 ; j++) { for ( int k= 0 ; k<= 2 ; k++) { arrInt[i][j][k] = value++; } } } // 값 출력 for ( int i= 0 ; i<= 2 ; i++) { for ( int j= 0 ; j<= 2 ; j++) { for ( int k= 0 ; k<= 2 ; k++) { System.out.print(arrInt[i][j][k]+ "\t" ); } System.out.println(); } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package chapter06; public class ArrEx12 { public static void main(String[] args) { int [][] arrInt = new int [ 3 ][]; arrInt[ 0 ] = new int [] { 1 }; arrInt[ 1 ] = new int [] { 2 , 3 }; arrInt[ 2 ] = new int [] { 4 , 5 , 6 }; for ( int i= 0 ; i<arrInt.length; i++) { for ( int j= 0 ; j<arrInt[i].length; j++) { System.out.print(arrInt[i][j]); } System.out.println(); } } } |
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 |
package chapter06; public class ArrEx13 { public static void main(String[] args) { // 원본 배열 int [] arrInt = { 1 , 2 , 3 }; // 복사할 배열 int [] arrInt2 = new int [ 5 ]; // 복사할 배열에 값 대입 for ( int i= 0 ; i<arrInt.length; i++) { arrInt2[i] = arrInt[i]; } // 배열 값 출력 for ( int i= 0 ; i<arrInt2.length; i++) { System.out.println(arrInt2[i]); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package chapter06; public class ArrEx14 { public static void main(String[] args) { // 원본 배열 int [] arrInt = { 1 , 2 , 3 }; // 복사할 배열 int [] arrInt2 = new int [ 5 ]; System.arraycopy(arrInt, 0 , arrInt2, 2 , arrInt.length); // 배열 값 출력 for ( int i= 0 ; i<arrInt2.length; i++) { System.out.println(arrInt2[i]); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package chapter06; import java.util.Arrays; public class ArrEx15 { public static void main(String[] args) { // 원본 배열 int [] arrInt = { 1 , 2 , 3 }; // 복사할 배열 int [] arrInt2 = null ; arrInt2 = Arrays.copyOf(arrInt, 5 ); // 배열 값 출력 for ( int i= 0 ; i<arrInt2.length; i++) { System.out.println(arrInt2[i]); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package chapter06; public class ArrEx16 { public static void main(String[] args) { // 배열 int [] arrInt = { 1 , 2 , 3 , 4 , 5 }; // 기존 for문을 이용한 출력 for ( int i= 0 ; i<arrInt.length; i++) { System.out.println(arrInt[i]); } // 향상된 for문을 이용한 출력 for ( int number : arrInt) { System.out.println(number); } } } |
값이 아닌 메모리 주소를 비교
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package chapter06; public class ReferenceType { public static void main(String[] args) { String name1 = "홍길동" ; String name2 = "홍길동" ; System.out.println(name1 == name2); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package chapter06; public class ReferenceType2 { public static void main(String[] args) { String name1 = new String( "홍길동" ); String name2 = new String( "홍길동" ); System.out.println(name1 == name2); } } |
==연산자
가 아닌 equals() 메서드를 사용
(문자열값 자체를 비교)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package chapter06; public class ReferenceType3 { public static void main(String[] args) { String name1 = new String( "홍길동" ); String name2 = new String( "홍길동" ); System.out.println(name1.equals(name2)); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package chapter06; import java.util.Arrays; public class ReferenceType4 { public static void main(String[] args) { // 배열 변수 생성 int [] arr1 = { 1 , 2 , 3 }; int [] arr2 = { 1 , 2 , 3 }; System.out.println( "arr1 == arr2 : " + (arr1 == arr2)); int [] arr3 = arr1; System.out.println( "arr1 == arr3 : " + (arr1 == arr3)); arr3[ 0 ] = 4 ; System.out.println( "arr3[0] : " +arr3[ 0 ]); System.out.println( "arr1[0] : " +arr1[ 0 ]); } } |
배열 객체 값을 그대로 받으면서 별개의 주소값을 갖는 참조자료형 만들기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package chapter06; import java.util.Arrays; public class ReferenceType5 { public static void main(String[] args) { // 배열 변수 생성 int [] arr1 = { 1 , 2 , 3 }; int [] arr2 = Arrays.copyOf(arr1, 3 ); arr2[ 0 ] = 4 ; System.out.println( "arr1[0] : " +arr1[ 0 ]); System.out.println( "arr2[0] : " +arr2[ 0 ]); } } |