JAVA공부

[Java] 2차원배열 출력 ,길이

코딩알파 2021. 8. 17. 17:22
728x90

2차원 배열 선언 방식

int[][] scores =new int[2][3]

2행 x 3열의 배열이 생성된다.

 

여기서 행부분이 2이므로 변수 scores의 길이는 2입니다.

인덱스[0]의 길이는 3 

인덱스[1]의 길이는 3

 

인덱스(0,0)  인덱스(0,1)  인덱스(0,2) 
인덱스(1,0)  인덱스(1,1)  인덱스(1,2) 

 

2차원 배열 출력하기

 

1
public class javaStudyTest {
 
    public static void main(String[] args) {
    
        int[][] mscore = new int[2][3];
        for(int i =0; i<mscore.length; i++) {
            for(int j=0; j<mscore[i].length; j++) {
                System.out.print("mscore["+i+"]["+j+"]="+mscore[i][j]+ " ");
            }
            System.out.println();
        }
        System.out.println("==============================================");
        int[][] escore= new int[2][];
        escore[0]= new int[2];
        escore[1]= new int[4];
        for(int i=0; i<escore.length; i++) {
            for(int j=0; j<escore[i].length; j++) {
                System.out.print("escore["+i+"]["+j+"]="+escore[i][j]+" ");
            }
            System.out.println();
        }
        System.out.println("==============================================");
        
        int[][] scores= {{80,70,60},{74,85},{11,22,33,44}}; 
        for(int i=0; i<scores.length; i++){
            for(int j=0; j<scores[i].length; j++) {
                System.out.print(scores[i][j]+" ");
            }
            System.out.println();
        }
        
        
 
    }
 
}
cs

 

 
mscore[0][0]=0 mscore[0][1]=0 mscore[0][2]=0 
mscore[1][0]=0 mscore[1][1]=0 mscore[1][2]=0 
==============================================
escore[0][0]=0 escore[0][1]=0 
escore[1][0]=0 escore[1][1]=0 escore[1][2]=0 escore[1][3]=0 
==============================================
80 70 60 
74 85 
11 22 33 44 
cs

 

여기서 2차원배열의 인덱스 값을 바로 출력하고 싶으면

 

System.out.println( scores[0][0] ); //80 

728x90