반응형

           

  JAVA Study





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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import java.util.Arrays;
public class tistory {
    public static void main(String[] args) {
        //배열 출력
        int[] score = new int[5];
        int k=1;
        
        score[0= 50;
        score[1= 60;
        score[k+1= 70;
        score[3= 80;
        score[4= 90;
        
        int tmp  = score[k+2+score[4];
        
        for(int i=0; i<score.length; i++){ //score.length는 score배열의 길이값
            System.out.println("score ["+i+"] :"+score[i]); //score[0]~score[4] 의 합
        }
        
        System.out.println("tmp : "+tmp); // score[3] + score[4]의 값
//        System.out.println("score[7] : "+score[7]); //배열 범위를 벗어나므로 오류
        
System.out.println("====================");
        
        //배열의 복사
        int[] arr = new int[5];
        
        for(int i=0; i<arr.length; i++){
            arr[i] = i+1//배열arr에 1~5를 저장한다
        }
        
        System.out.println("[변경전]");
        System.out.println("arr.length:"+arr.length);
        
        for(int i=0;i<arr.length;i++){
            System.out.println("arr["+i+"] : "+arr[i]);
        }
        
        int[] temp = new int[arr.length*2];  // arr배열보다 두배 긴 길이의 temp 배열 생성
        
        for(int i=0; i<arr.length;i++){
            temp[i] = arr[i]; // arr배열의 값들을 temp배열에 저장
        } 
        
        arr = temp; //tmp에 저장된 값들을 arr에 저장
        
        System.out.println("[변경후]");
        System.out.println("arr.length:"+arr.length);
        
        for(int i=0;i<arr.length;i++){
            System.out.println("arr["+i+"] : "+arr[i]);
        }
        
System.out.println("====================");
        
        // 배열 일부 복사
        char[] abc = {'A','B','C','D'};
        char[] num = {'0','1','2','3','4','5','6','7','8','9'};
        System.out.println(abc);
        System.out.println(num);
        
            //배열 abc와 num을 붙여서 하나의 배열(result)로 만듦
        char[] result = new char[abc.length+num.length];
        System.arraycopy(abc,0,result,0,abc.length); 
        System.out.println(result);//ABCD
        
        System.arraycopy(num,0,result,abc.length,num.length);
        System.out.println(result);//ABCD0123456789
        
        System.arraycopy(abc, 0, num, 0, abc.length);
        System.out.println(num);//ABCD456789
        
        System.arraycopy(abc, 0, num, 63);
        System.out.println(num);//ABCD45ABC9
    
System.out.println("====================");
        
        //배열 값 총합과 평균
        int sum = 0;
        float avg = 0f;
        
        int[] score1 = {100,88,100,100,90};
        
        for(int i=0;i<score1.length;i++){
            sum += score1[i];
        }
        avg = sum /(float)score.length;
        
        System.out.println("총점 : "+sum);
        System.out.println("평균 : "+avg);
    
System.out.println("====================");
        
        //최대값 최소값
        int max = score1[0];
        int min = score1[0];
        
        for(int i=1; i<score1.length;i++){
            if(score1[i] >max){
                max = score1[i];
            } else if(score1[i]<min){
                min = score1[i];
            }
        }
        
        System.out.println("최대값 : "+max);
        System.out.println("최소값 : "+min);
System.out.println("====================");
        //섞기
        int[] numArr = new int[10];
        
        for(int i=0; i<numArr.length; i++){
            numArr[i] = i;
            System.out.print(numArr[i]);
        }
        System.out.println();
        
        for(int i=0;i<100; i++){
            int n = (int)(Math.random()*10);
            int tmp1 = numArr[0];
            numArr[0= numArr[n];
            numArr[n] = tmp1;
        }
        
        for(int i=0;i<numArr.length; i++){
            System.out.print(numArr[i]);
        }
        System.out.println();
System.out.println("====================");
        
        //로또번호 생성예제
        int[] ball = new int[45]; //45개 정수를 저장할 수 있는 배열
        
            //배열의 각 요소에 1~45의 값을 저장한다.
        for (int i=0; i<ball.length; i++){
            ball[i] = i+1;
        }
        
        int temp3 =0//두 값을 바꾸는데 사용할 임시변수
        int j=0// 임의 값을 얻어서 저장할 변수
        
            //배열의 i번째 요소와 임의의 요소에 저장된 값을 서로 바꿔서 값을 섞는다.
            //0번재 부터 5번째 요소까지 모두 6개만 바꾼다.
        for (int i=0; i<6; i++){
            j=(int)(Math.random()*45); // 0~44까지 임의의 값
            temp3 = ball[i];
            ball[i] = ball[j];
            ball[j] = temp3;
        }
        
            //배열 ball의 앞에서 부터 6개 요소 출력
        for(int i=0; i<6; i++){
            System.out.println("ball ["+i+"] = "+ball[i]);
        }
        
System.out.println("====================");
        
        //임의의 값으로 배열 채우기
        int[] code = {-4,-1,3,6,11}; //불 연속적 값으로 구성된 배열
        int[] arr4 = new int[10];
        
        for(int i=0; i<arr4.length; i++){
            int tmp4 = (int)(Math.random() * code.length);
            arr4[i] = code[tmp4];
        }
        System.out.println(Arrays.toString(arr4));
System.out.println("====================");
        //정렬하기
        int[] numArr5 = new int[10];
        
        for(int i=0; i<numArr5.length; i++){
            System.out.print(numArr5[i] = (int)(Math.random() *10));
        }
        System.out.println();
        for(int i=0; i<numArr5.length-1; i++){
            boolean changed= false//자리바꿈이 발생했는지 체크
            
            for(int j5=0; j5<numArr5.length-1-i; j5++){
                if(numArr5[j5] > numArr5[j5+1]){ //옆 자리 값이 작으면 서로 바꿈
                    int tmp5 = numArr5[j5];
                    numArr5[j5] = numArr5[j5+1];
                    numArr5[j5+1= tmp5;
                    changed = true//자리바꿈 발생 시 true로 변환
                }
            }
            
            if (!changed) break//자리바꿈 없으면 반복문 벗어남
            
            for(int k5=0; k5<numArr5.length; k5++){
                System.out.print(numArr5[k5]);
            }
            System.out.println();
        }
        
    }
}
 
cs
반응형

+ Recent posts