반응형
public class Start {
//배열의 선언방법
public static void exam1(){
//명시적 배열 생성
int [] arr1 = new int[10];
//암시적 배열 생성
int [] arr2 = {1,2,3,4,5,6,7,8,9};
}
//배열에서의 울타리말뚝 오류
public static void exam2(){
try{
int[] arr = new int[2];
arr[2] = 10;
}
catch(java.lang.IndexOutOfBoundsException e){
System.out.println(e.getMessage());
}
}
//배열 복사
public static void exam3(){
int [] source = new int[]{5,4,6,9,7,9};
int [] target = {100, 200, 300, 400,500, 600, 700};
//1.arrarycopy : 부분복사
System.arraycopy(source, 2, target, 3, 4);
//2.clone : 전체 복사
int[] target1 = source.clone();
System.out.println(target1[2]);
//3. 2번과 차이점 이해
int[] target2 = source;
System.out.println(target2[2]);
}
//2차원 배열의 형태1
public static void exam4(){
//암시적
int [][] arr = {{1,2,3},
{4,5,6},
{7,8,9}};
//명시적
int [][] arr1 = new int[3][3];
for(int i=0; i<arr.length; i++){ //length:행의갯수
for(int j=0; j<arr[i].length; j++){//열의 갯수
System.out.println(arr[i][j]);
}
}
}
//2차원 배열의 형태2 : 가변배열
public static void exam5(){
//암시적
int [][] arr = {{1,2,3},
{4},
{7,8,9,10}};
//명시적
int [][] arr1 = new int[3][];
arr1[0] = new int[3];
arr1[1] = new int[1];
arr1[2] = new int[4];
for(int i=0; i<arr.length; i++){ //length:행의갯수
for(int j=0; j<arr[i].length; j++){//열의 갯수
System.out.println(arr[i][j]);
}
}
}
public static void main(String[] args) {
exam5();
}
}
반응형
'01.Bit 단기 > Java' 카테고리의 다른 글
06_상속성 다형성 (0) | 2021.11.29 |
---|---|
05_캡슐화 (0) | 2021.11.29 |
03_foreach (0) | 2021.11.29 |
02_문자열 (0) | 2021.11.29 |
01_기본입출력 (0) | 2021.11.29 |
댓글