ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [JAVA] 배열
    JAVA 2023. 7. 10. 23:49
    /*1차원 배열*/
    public class Test {
        public static void main(String[] args) throws Exception {
            int arr[]; //배열 변수 선언,int[] arr 가능
            arr = new int[10]; //배열 생성
            arr[0] = 1;
            arr[1] = 2;
            arr[2] = arr[0]+arr[1];
            System.out.println(arr[0]);
            System.out.println(arr[1]);
            System.out.println(arr[2]);
        }
    }
    1
    2
    3

     

    /*1차원 배열*/
    public class Test {
        public static void main(String[] args) throws Exception {
            int arr[] = {1,2,3,4};
            System.out.println(arr[0]);
            System.out.println(arr[1]);
            System.out.println(arr[2]);
        }
    }
    1
    2
    3

     

    /*2차원 배열*/
    public class Test {
        public static void main(String[] args) throws Exception {
            int arr[][] = new int[3][4]; //배열 변수 선언,생성
            arr[0][0] = 1;
            arr[1][1] = 2;
            arr[2][2] = arr[0][0]+arr[1][1];
            System.out.println(arr[0][0]);
            System.out.println(arr[1][1]);
            System.out.println(arr[2][2]);
        }
    }
    1
    2
    3

     

    /*2차원 배열*/
    public class Test {
        public static void main(String[] args) throws Exception {
            int arr[][] = {{1,2,3,4},
                           {5,6,7,8},
                           {9,10,11,12}};
            System.out.println(arr[0][0]);
            System.out.println(arr[1][1]);
            System.out.println(arr[2][2]);
        }
    }
    1
    6
    11

     

    /*length:배열의 항목 수 알아내는 예시*/
    public class Test {
        public static void main(String[] args) throws Exception {
            int arr1[] = {1,2,3,4};
            int arr2[][] = {{1,2,3,4},
                           {5,6,7,8},
                           {9,10,11,12}};
            System.out.println(arr1.length); //배열의 크기 알아냄
            System.out.println(arr2[0].length);
            System.out.println(arr2.length);
        }
    }
    4
    4
    3

    'JAVA' 카테고리의 다른 글

    [JAVA] for 반복문  (0) 2023.07.11
    [JAVA] while 반복문  (0) 2023.07.11
    [JAVA] 조건문  (0) 2023.07.11
    [JAVA] 로컬 변수의 사용 방법  (0) 2023.07.10
    [JAVA] Print출력  (0) 2023.07.10
Designed by Tistory.