'배열'에 해당되는 글 1건

  1. 2012.07.28 배열

* 배열 (Array) - 같은 type의 Data를 모아서 관리하는 객체 (상자안에 여러개의 물건을 넣는 방식)
->같은 의미를 가지는 Data들을 모을 때 사용.

배열 변수선언

Datatype[] 식별자
ex :  int[] arr;

- 배열 객체 생성 및 할당

식별자 = new Datatype[length];

length : 모을 수 있는 Data 수


ex :  arr = new int [10];->int data 10개를 모을 수 있는 배열 객체(한번 크기를 정하면 거기서 끝이다.)

- 배열에 값 대입.

변수명 [index]= 값;

대입된 값 사용 :  변수명[index];

* 2012-03-06

- 배열 생성시 값초기화
1.int i[] = {10,,20,30,40};
2.new int[]{10,20,30};


* 배열 ex :

public class ArrayTest
{

 public void createArray1(){
  int[] intArray = new int[3]; //변수 선언 = 배열객체 생성;
  //배열에 값 할당
  System.out.println(intArray[0]);
  System.out.println(intArray[1]);
  System.out.println(intArray[2]);

  System.out.println("------------------------------");

  intArray[0]=10;
  intArray[1]=20;
  intArray[2]=30;
  //배열의 값 조회
  System.out.println(intArray[0]);
  System.out.println(intArray[1]);
  System.out.println(intArray[2]);

  System.out.println("------------------------------");
  //System.out.println(intArray[3]); //length가 3이므로 마지막 index : 2 - > 오류
 }

 public static void main(String[] args)
 {
  ArrayTest at = new ArrayTest();
  at.createArray1();

 }
}


-배열 선언 ex:
public class ArrayTest
{

 public void createArray1(){
  int[] intArray = new int[3]; //변수 선언 = 배열객체 생성;
  //배열에 값 할당
  System.out.println(intArray[0]);
  System.out.println(intArray[1]);
  System.out.println(intArray[2]);

  System.out.println("------------------------------");

  intArray[0]=10;
  intArray[1]=20;
  intArray[2]=30;
  //배열의 값 조회
  System.out.println(intArray[0]);
  System.out.println(intArray[1]);
  System.out.println(intArray[2]);

  System.out.println("------------------------------");
  //System.out.println(intArray[3]); //length가 3이므로 마지막 index : 2 - > 오류
 }
 
 public void createArray2(){
  //배열 선언, 생성, 값할당(초기화)
  double doubleArray[] = {10.5,203.7,102.3,1.6,2E3};
  System.out.println(doubleArray[0]+"\n"+doubleArray[1]+"\n"+doubleArray[2]+"\n"+doubleArray[3]+"\n"+doubleArray[4]);//2E3 : 2*10.0의 세제곱
  System.out.println("doubleArray의 length : "+doubleArray.length);

//for문을 이용한 출력.
  for(int i = 0;i<doubleArray.length;i++){
   System.out.println("doubleArray["+i+"] = "+doubleArray[i]);
  }

//  char [] c ;
//  c = {'a','b','c','d','e'};//선언 생성, 초기화 같이 해야만 한다.

 }
 public static void main(String[] args)
 {
  ArrayTest at = new ArrayTest();
  at.createArray1();
  at.createArray2();

 }
}

* 배열 예제2

public class ArrayTest
{

 public void createArray1(){
  int[] intArray = new int[3]; //변수 선언 = 배열객체 생성;
  //배열에 값 할당
  System.out.println(intArray[0]);
  System.out.println(intArray[1]);
  System.out.println(intArray[2]);

  System.out.println("------------------------------");

  intArray[0]=10;
  intArray[1]=20;
  intArray[2]=30;
  //배열의 값 조회
  System.out.println(intArray[0]);
  System.out.println(intArray[1]);
  System.out.println(intArray[2]);

  System.out.println("------------------------------");
  //System.out.println(intArray[3]); //length가 3이므로 마지막 index : 2 - > 오류
 }
 
 public void createArray3(){
  //배열 선언, 생성, 값할당(초기화)
  String stringArray[] = new String[]{"가","나","다"};

//for문을 이용한 출력.
  for(int i = 0;i<stringArray.length;i++){
   System.out.println("stringArray["+i+"] = "+stringArray[i]);
  }

//  char [] c ;
//  c = {'a','b','c','d','e'};//선언 생성, 초기화 같이 해야만 한다.
  test(new boolean[]{true,false,false,false,true});
 }
 public void test(boolean[] boolArray){
  for(int idx = 0;idx<boolArray.length;idx++){
   System.out.println(boolArray[idx]);
  }
 }
  public void createArray2(){
  //배열 선언, 생성, 값할당(초기화)
  double doubleArray[] = {10.5,203.7,102.3,1.6,2E3};
  System.out.println(doubleArray[0]+"\n"+doubleArray[1]+"\n"+doubleArray[2]+"\n"+doubleArray[3]+"\n"+doubleArray[4]);//2E3 : 2*10.0의 세제곱
  System.out.println("doubleArray의 length : "+doubleArray.length);

//for문을 이용한 출력.
  for(int i = 0;i<doubleArray.length;i++){
   System.out.println("doubleArray["+i+"] = "+doubleArray[i]);
  }

//  char [] c ;
//  c = {'a','b','c','d','e'};//선언 생성, 초기화 같이 해야만 한다.

 }
 public static void main(String[] args)
 {
  ArrayTest at = new ArrayTest();
  at.createArray1();
  at.createArray2();
  at.createArray3();

 }
}

* 향상된 for문(jdk1.5이상부터 가능) = 배열/collection의 값들을 조회할때 사용.

배열 : 0번 idx~마지막 idx의 값 조회.

구문 :  for(변수선언 :  배열객체){
 변수로 작업;
}

ex : int [] arr = {1,2,3,4,5};
for(int i :  arr){
System.out.println(i); //배열의 0~마지막 idx까지 조회할때만 사용한다. 값대입이 안된다.
}


* 배열안에 들어간 숫자 객수 찍어보기

public class ArrayCount
{
 public static void main(String[] args)
 {
  int[] arr = new int[10];
  int[] arrayCount=new int[10];
  //1. arr에 0~9의 숫자를 10개 넣으세요.
  //2. 배열에 들어간 숫자들이 각각 몇개가 들어갔는지 출력하세요.
  for(int i=0;i<arr.length;i++){
   arr[i]=(int)(Math.random()*10);
   System.out.print(arr[i]+" ");
   
   arrayCount[arr[i]]++;
  
  }
  System.out.println();
  for(int i=0;i<arrayCount.length;i++){
   System.out.println("arrayCount["+i+"] = "+arrayCount[i]);
  }
 }
}


*rateTest

public class RateTest
{

 public static void starPrint(int tot){
 for(int i=0;i<tot;i++){
 System.out.print("*");
 }
 }
 public static void main(String[] args)
 {
  int data[] = {10,30,20,80,50};
  double rate[] = new double[5];
  double tot=0;


  for(int i =0;i<data.length;i++){
   tot+=data[i];
  }
  System.out.println(""+tot);
  
  for(int i=0;i<rate.length;i++){
   rate[i]=Math.round(data[i]/tot*100);

   System.out.print(data[i]+" : ");
   starPrint((int)rate[i]);
   System.out.println("<"+rate[i]+"%>");
  }

 }
}


* 클래스 배열

선언 :  Student[] stuArr = new Student[30];
생성 :  stuArr[0] = new Student();
        stuArr[1] = new Student();
값   :  stuArr[0].name ="홍길동";
 stuArr[1].study();


* type
stuArr - Student[]
stuArr[0] - Student


ex :

public class ArrayTestRefType
{
 public void createArray1(){
  Human[] humArray = new Human[3];
  humArray[0] = new Human();
  humArray[1] = new Human("이순신",20,172.3);
  humArray[2] = new Human("홍길동",30,182.5);
  
  humArray[0].printInfo();
  System.out.println("-------------------------");
  humArray[1].printInfo();
  System.out.println("-------------------------");
  humArray[2].printInfo();

  System.out.println("----------for문이용---------------");
  for(int i =0;i<humArray.length;i++){
   humArray[i].printInfo();
   System.out.println("-------------------------");
  }
  System.out.println("----------향상된 for문이용---------------");
  for(Human h : humArray){
   h.printInfo();
   System.out.println("-------------------------");
  }
 }
 public static void main(String[] args)
 {
  ArrayTestRefType atrt = new ArrayTestRefType();
  atrt.createArray1();
 }
}

ex2 :


public class ArrayTestRefType
{
 public void createArray1(){
  Human[] humArray = new Human[3];
  humArray[0] = new Human();
  humArray[1] = new Human("이순신",20,172.3);
  humArray[2] = new Human("홍길동",30,182.5);
  
  humArray[0].printInfo();
  System.out.println("-------------------------");
  humArray[1].printInfo();
  System.out.println("-------------------------");
  humArray[2].printInfo();

  System.out.println("----------for문이용---------------");
  for(int i =0;i<humArray.length;i++){
   humArray[i].printInfo();
   System.out.println("-------------------------");
  }
  System.out.println("----------향상된 for문이용---------------");
  for(Human h : humArray){
   h.printInfo();
   System.out.println("-------------------------");
  }

 }
 public void createArray2(){
  //선언,생성,초기화 한번에 처리
 Human humArray[] = {new Human(), new Human("유재석",41,180.0), new Human("박명수",43,178.5)};
 
 for(int i =0;i<humArray.length;i++){
  humArray[i].printInfo();
  System.out.println("######");

 }
 }
 public static void main(String[] args)
 {
  ArrayTestRefType atrt = new ArrayTestRefType();
  atrt.createArray1();
  atrt.createArray2();
 }
}


* class는 type , 객체는 값 잘기억해라.

* 2012.3.7

1. 2차원배열

-선언 :  Datatype[][] 변수명;

int [][]i;
int[]i[];
inti[][];
다 가능

- 생성 및 변수에 대입

변수명 = new Datatype[length][length];
i = new int[3][2];

* 2차원 배열 ex :
public class TwoDimensionArrayTest
{
 public static void main(String[] args)
 {
  //2010,2011 년도 1월~6월(전반기)강수량을 이차원배열을 통해 넣으세요.


  //double[][] rainfall = {{10,20,30,40,50,60},{10,20,30,40,50,60}};
  double[][] rainfall = new double[2][6];
  for(int i=0;i<rainfall.length;i++){
   for(int j=0;j<rainfall[i].length;j++){

    rainfall[i][j]=Math.random()*100;

   }
  }


  for(int i=0;i<2;i++){
   for(int j=0;j<6;j++){

    System.out.printf("201%d년 %d월 강수량 : %.2f\n",i,(j+1),rainfall[i][j]);

   }
  }
  System.out.println("---------향상된 for문---------------");
  for(double[] firstArr :  rainfall){
   for(double rain : firstArr){
    System.out.print(rain+"\t");
   }
   System.out.println();
  }

 

 }
}

 

'프로그래밍 > JAVA프로그래밍' 카테고리의 다른 글

package, import  (0) 2012.07.28
productManagerArray만들기 [실습]  (0) 2012.07.28
반복분(for, while)  (0) 2012.07.28
switch문  (0) 2012.07.28
if문  (0) 2012.07.28
Posted by 조은성
,