(8) Array

JAVA/JAVA 이론 2018. 10. 25. 19:13
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
package Studypackage;
 
public class studyclass 
{
    // 배열 연습 1 
    public static void main(String[] args)
    {
        // 배열의 데이터 형식과 크기를 선언
        int[] nums = new int[7];    // 자동으로 초기 값 0
        
        System.out.println("nums[0] = " + nums[0]);
        System.out.println("nums[6] = " + nums[6]);
        // 사용할 수 없는 index(실행오류)
        // System.out.println("nums[8] = " + nums[8]);
        System.out.println();


        // 초기값을 설정하고 선언한 배열
        int[] vals = {11,22,33,44,55,66,77,88,99,110};
        
        System.out.println("vals.length = " + vals.length);
        System.out.println();
        for(int i =0; i<vals.length; i++)    //length : 배열 속성(개수 값)
        {
            System.out.printf("vals[%d] = %d\n",i,vals[i]);
        }


    }
}
 
cs




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
56
57
58
59
60
61
62
package Studypackage;
 
import java.util.Random;
 
public class studyclass 
{
    // 배열데이터 처리연습 : 합계 , 평균 , 최대 값 , 최소 값 구하기
    public static void main(String[] args)
    {
        Random r = new Random();
        
        int[] score = new int[10];
        int i =0;
        int sum = 0;
        int max = 0;
        int min = 0;
        
        // 배열에 성적값은 random 값으로 저장 40 ~ 99
        
        //1) 합계
        for(i=0; i<score.length; i++)
        {
            score[i] = r.nextInt(60+ 40;
            
            sum += score[i];
            
            System.out.printf("%d ",score[i]);
            if(i == 4)
            {
                System.out.println();
            }
        }
        System.out.println();

  
        //2) 최대 값
        
        for(i=0; i<score.length; i++)
        {
            if(max < score[i])
            {
                max = score[i];
            }
        }
        
        //3) 최소 값
        
        min = score[0];
        
        for(i=1;i<score.length;i++)
        {
            if(min > score[i])
            {
                min = score[i];
            }
        }
        System.out.printf("합계 = %d\n",sum);
        System.out.printf("최대 값 = %d\n",max);
        System.out.printf("최소 값 = %d\n",min);
        System.out.printf("평균 = %d\n",sum / score.length);

    }
}
 
cs



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
package Studypackage;
 
public class studyclass 
{
    // 배열 데이터가 기본형(int,double,....)이 아닌 String 클래스
    public static void main(String[] args)
    {
        String[] names = new String[5];        // 자동으로 초기 값 null
        
        System.out.println("names print");
        for(int i =0; i<names.length;i++)
        {
            System.out.printf("names = %s\n",names[i]);
        }
        System.out.println();

    
        String[] animals = {"cat","rabbit","tiger","lion","dog"};
        System.out.println("animals print");
        for(int i=0;i<animals.length;i++)
        {
            //*** animals[i]는 객체를 참조하는 참조 값        ->        객체의 메소드 실행
            // 일반적으로 toString() 메소드는 생략하고 사용
            System.out.printf("animals = %s\n",animals[i]);
            System.out.println("animals " + i + " : " + animals[i].toUpperCase());
            // hashCode : 참조 값 (계산에 쓰이는 값)
            System.out.printf("animals %d : %X\n",i,animals[i].hashCode());
            System.out.println();


        }
    }
}
 
cs


'JAVA > JAVA 이론' 카테고리의 다른 글

(10) 2중 for 문 & 2차원 배열  (0) 2018.10.29
(9) String Method  (0) 2018.10.28
(7) While & Do ~ While & Continue  (0) 2018.10.24
(6) If & For  (0) 2018.10.23
(5) Op & IF & Char  (0) 2018.10.22

설정

트랙백

댓글