2011년7월26일_JAVA 배열의 선언과 초기화(사용법), 연습문제 5개


● 1차원 배열의 선언
  ① int[ ] a = new int[10];       // JAVA스타일?  
  ② int a[ ] = new int[10];       // C언어처럼 허용하나 인자수를 적으면 안 됨.

● 1차원 배열의 선언과 동시에 초기화
  ① char[ ] b = {1, 2, 3, 4, 5};       //어느 영역에 위치하나?

● 1차원 배열에 대입
  a[0] = 1;
  a[1] = 2;

★ JAVA는 배열의 인덱스를 벗어난 곳을 엑세스하면 에러!!
★ 여러 자료구조 중 배열이라도 잘 쓰자.
★ 플렉션: 복잡한 자료구조를 배열로 만듬.





● 문제1

정수형배열 odd[4]를 만든 후 1, 3, 5, 7을 대입한 후 그대로 화면에 출력하라.


<소스코드>

   1: //배열문제1
   2: //2011년7월26일
   3: package kr.ac.busanit;
   4:  
   5: public class ArrayTest {
   6:  
   7:     /**
   8:      * @param args
   9:      */
  10:     public static void main(String[] args) {
  11:         // TODO Auto-generated method stub
  12:  
  13:         
  14:         // 1번 문제
  15:         int[] odd = new int[4];
  16:         
  17:         for(int i = 0, cnt = 1 ; i < odd.length ; i++, cnt = cnt + 2)
  18:         {
  19:             odd[i] = cnt;
  20:             System.out.print(odd[i] + " ");
  21:         }        
  22:     }
  23:  
  24: }


<실행결과>

image




● 문제2

char배열 Alpha[26]을 만들어 알파벳 대문자 A부터 Z까지 대입한 후,
화면에 순서대로 출력하라.


<소스코드>

   1:  
   2: package kr.ac.busanit;
   3:  
   4: public class ArrayTest2 {
   5:  
   6:     /**
   7:      * @param args
   8:      */
   9:     public static void main(String[] args) {
  10:         // TODO Auto-generated method stub
  11:         // 2번 문제
  12:         
  13:         char[] Alpha = new char[26];
  14:         
  15:         for(int i = 0, cnt = 65 ; i < Alpha.length ; ++i, ++cnt)
  16:         {
  17:             Alpha[i] = (char)cnt;
  18:             System.out.print(Alpha[i] + " ");
  19:         }
  20:         
  21:         System.out.println();
  22:         
  23:         //선생님꺼
  24:         for(char i = 'A', cnt = 0 ; i <= 'Z' ; ++i, ++cnt)
  25:         {
  26:             Alpha[cnt] = i;
  27:             System.out.print(Alpha[cnt] + " ");
  28:         }
  29:     }
  30:  
  31: }


<실행결과>

image




● 문제3

이번엔 char배열 Alpha[26]에 소문자z부터 a까지 역순으로 대입한 후,
화면에 배열의 첫 원소부터 차례대로 출력하라.


<소스코드>

   1: package kr.ac.busanit;
   2:  
   3: public class ArrayTest3 {
   4:  
   5:     /**
   6:      * @param args
   7:      */
   8:     public static void main(String[] args) {
   9:         // TODO Auto-generated method stub
  10:         char[] Alpha = new char[26];
  11:         
  12:         for(char i = 0, cnt = 'z' ; cnt >= 'a' ; --cnt, ++i)
  13:         {
  14:             Alpha[i] = cnt;
  15:             System.out.print(Alpha[i] + " ");
  16:         }
  17:     }
  18:  
  19: }



<실행결과>

image




● 2차원 배열의 선언
   int[ ][ ] array = new int[5][5];

행, 열 순서이고 초기화와 사용법은 1차원배열과 유사함.


● 문제4

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



<소스코드>

   1: package kr.ac.busanit;
   2:  
   3: public class ArrayTest4 {
   4:  
   5:     /**
   6:      * @param args
   7:      */
   8:     public static void main(String[] args) {
   9:         // TODO Auto-generated method stub
  10:         int[][] array = new int[5][5];
  11:         
  12:         for(int i = 0, cnt = 1 ; i < 5 ; ++i)
  13:         {
  14:             for(int j = 0 ; j < 5 ; ++j, ++cnt)
  15:             {
  16:                 array[i][j] = cnt;
  17:                 //System.out.printf("%3d", array[i][j]);
  18:                 System.out.print(array[i][j] + "\t");
  19:             }
  20:             System.out.println();
  21:         }
  22:     }
  23:  
  24: }


<실행결과>

image




● 문제5

A F K P U
B G L Q V
C H M R W
D I N S X
E J O T Y



<소스코드>

   1: //2차원배열에 알파벳집어넣기
   2: package kr.ac.busanit;
   3:  
   4: public class ArrayTest5 {
   5:  
   6:     /**
   7:      * @param args
   8:      */
   9:     public static void main(String[] args) {
  10:         // TODO Auto-generated method stub
  11:         char[][] array = new char[5][5];
  12:         
  13:         //배열초기화
  14:         for(char i = 0, cnt = 'A' ; i < 5 ; ++i)
  15:         {
  16:             for(char j = 0 ; j < 5 ; ++j, ++cnt)
  17:             {
  18:                 array[j][i] = cnt;
  19:             }
  20:         }
  21:         //배열에 들은 값들을 화면에 출력
  22:         for(char i = 0 ; i < 5 ; ++i)
  23:         {
  24:             for(char j = 0 ; j < 5 ; ++j)
  25:             {
  26:                 System.out.print(array[i][j] + "\t");
  27:             }
  28:             System.out.println();
  29:         }
  30:     }
  31: }


<실행결과>

image







참조(Reference)





DSCN3779 DSCN3780 DSCN3781