관리 메뉴

nalaolla

Arrays 정리 본문

JAVA/23. Arrays

Arrays 정리

날아올라↗↗ 2015. 12. 1. 21:47
728x90
  1. package test.com;
  2.  
  3. import java.util.Arrays;
  4.  
  5. public class Test01Arrays {
  6.  
  7.     public static void main(String[] args) {
  8.         System.out.println("Arrays...");
  9.        
  10.         int[] temps = new int[]{22,33,44,11,34,41};
  11.        
  12.         //순정렬
  13.         //11,22,33,34,41,44
  14.         Arrays.sort(temps);
  15.        
  16.         for (int i : temps) {
  17.             System.out.println(i);
  18.         }
  19.        
  20.         //데이터 index검색(정렬이후에 사용)
  21.         System.out.println(Arrays.binarySearch(temps, 33));
  22.        
  23.         //배열비교
  24.         boolean bool = Arrays.equals(temps, new int[]{22,33,44,11,34,41});
  25.         System.out.println(bool);
  26.        
  27.         //배열데이터 전부 채우기, 변경
  28.         Arrays.fill(temps, 99);
  29.        
  30.         //배열데이터 부분 채우기, 변경
  31.         Arrays.fill(temps, 2418);
  32.         for (int i : temps) {
  33.             System.out.println(i);
  34.         }
  35.        
  36.        
  37.     }
  38.  
  39. }

==================================================

배열정리


[코드설명]

10. 임의의 int형 배열변수를 선언

14. Arrays 클래스의 sort 메소드를 통해 순정렬로 정리

16~18. for each구문으로 해당 배열값 출력

21. 해당 배열요소가 어느위치에 있는지 출력 - 데이터 인덱스 검색의 경우 정렬이후에 사용

24. boolean 타입으로 두 배열의 값이 동일한지 체크. 동일하면 true, 다를경우 false

28~34. Arrays.fill 메소드를 통해 배열값 전체 변경 및 특정위치 배열 값 변경 가능

728x90