본문 바로가기

JAVA

[JAVA] java.util 패키지

 

 

import java.util.*;

public class Main {

    public static void main(String[] args) {

        // java.util.Arrays
        int[] scoreList = {87, 56, 79};
        Arrays.sort(scoreList);
        for (int i=0; i<scoreList.length; i++) {
            System.out.print(scoreList[i] + ", ");
        }

        
        // Date 클래스
        Date currentDate = new Date();
        long start = currentDate.getTime();

        System.out.println(currentDate.toString());
        System.out.println(currentDate.toLocaleString());

        currentDate = new Date();
        long end = currentDate.getTime();
        System.out.println(end - start);

        
        // Calendar 클래스는 추상클래스
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int mon = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DAY_OF_MONTH);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int min = cal.get(Calendar.MINUTE);
        int sec = cal.get(Calendar.SECOND);
        System.out.println();
        System.out.println(year + "." + mon + "." +day + "." + hour + "." + min + "." + sec + ".");

        
        // Formatter 클래스
        StringBuffer sb = new StringBuffer();
        Formatter formatter = new Formatter(sb);
        int i = 10;
        int j = 20;
        int k = i + j;
        String str = "<< 연산 결과 >>";

        System.out.println();
        formatter.format("%s%n", str);
        formatter.format("%d 과 %d 을 더하면 %d 이 됩니다. %n", i, j, k);
        System.out.println(formatter.toString());


        // Vector 클래스
        Vector list = new Vector(3);
        System.out.println("저장 능력: " + list.capacity());
        System.out.println("저장 요소 개수: " + list.size());

        list.addElement(new Integer(10));
        list.addElement(new Double(10.0));
        list.addElement(new String("자바"));
        list.addElement(new Date());

        if (list.contains("자바")) {
            System.out.println("\"자바\" 스트링은 " + list.indexOf("자바") + " 번 인덱스에 존재한다.");
        }
        for (int l=0; l<list.size(); l++) {
            System.out.println(l + "번째 요소는 " + list.elementAt(l));
        }


        // Enumeration 인터페이스
        Enumeration e = list.elements();
        System.out.println();
        System.out.println("<< Vector로부터 생성한 Enumeration의 요소들 >>");
        while(e.hasMoreElements()) {
            System.out.println("e의 요소: " + e.nextElement());
        }


        // Stack 클래스
        Stack stack = new Stack();
        for (int n=1; n<=5; n++) {
            stack.push("데이터-" + n);
        }

        System.out.println();
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.peek());
        System.out.println(stack.peek());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());


        // Queue 인터페이스
        // Queue 를 구현한 LinkedList 클래스를 통해 객체 생성
        Queue<String> queue = new LinkedList<String>();
        for (int m=1; m<=3; m++) {
            queue.add("데이터-" + m);
        }
        System.out.println();
        System.out.println("<< 가장 앞에 있는 요소 >>");
        System.out.println(queue.peek());
        System.out.println("<< 순차적으로 요소 꺼내기 >>");
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());


        // Generics
        Vector<Integer> list2 = new Vector<Integer>();

        
        // 확장된 for 문
        int[] scoreList2 = {87, 56, 79};
        int scoreSum = 0;
        for (int score: scoreList2) {
            scoreSum += score;
        }
        System.out.println();
        System.out.println("점수의 총합: " + scoreSum);


        // Collection 에서 확장 for 문
        Vector<String> subjectList = new Vector<String>();
        subjectList.add("JAVA");
        subjectList.add("SQL");
        subjectList.add("Servlet");

        System.out.println();
        for (String subject: subjectList) {
            System.out.println(subject);
        }

    }

}
더보기

결과

 

56, 79, 87, Thu Jul 15 01:51:24 KST 2021
Jul 15, 2021 1:51:24 AM
52

2021.7.15.1.51.24.

<< 연산 결과 >>
10 과 20 을 더하면 30 이 됩니다. 

저장 능력: 3
저장 요소 개수: 0
"자바" 스트링은 2 번 인덱스에 존재한다.
0번째 요소는 10
1번째 요소는 10.0
2번째 요소는 자바
3번째 요소는 Thu Jul 15 01:51:24 KST 2021

<< Vector로부터 생성한 Enumeration의 요소들 >>
e의 요소: 10
e의 요소: 10.0
e의 요소: 자바
e의 요소: Thu Jul 15 01:51:24 KST 2021

데이터-5
데이터-4
데이터-3
데이터-3
데이터-3
데이터-2
데이터-1

<< 가장 앞에 있는 요소 >>
데이터-1
<< 순차적으로 요소 꺼내기 >>
데이터-1
데이터-2
데이터-3

점수의 총합: 222
JAVA
SQL
Servlet

'JAVA' 카테고리의 다른 글

[JPA] n+1 문제  (0) 2022.01.26
[JAVA] 자바 언어의 특징  (0) 2021.07.04