**why? 버전이 업되면서 개선된 것이 ArrayList이기 때문에. 가급적 ArrayList를 쓰는 편이 좋다.
메서드/생성자
설명
Vector()
10개의 객체를 저장할 수 있는 Vector인스턴스 생성 *10개 이상의 인스턴스 저장 시, 자동으로 크기 증가
boolean add(Object o)
Vector에 객체를 추가 *추가에 성공하면 true, 실패하면 false를 반환
boolean remove(Object o)
Vector에 저장된 객체를 제거 *제거 후 Vector에 객체가 없으면 true, 있으면 false반환
boolean isEmpty()
Vecotor가 비어 있는지 검사
Object get(int index)
index위치의 객체 주소 반환
int size()
Vecotor에 저장된 객체의 갯수 반환
package mymain;
import java.util.Vector;
public class VectorTest {
public static void main(String[] args) {
/*
Product[] item = new Product[3];
item[0] = new Tv();
item[1] = new Audio();
item[2] = new Computer();
//AirConditioner를 넣을 공간이 없다.
*/
Vector<Product> item = new Vector<Product>(); //Product타입의 10개의 배열
item.add(new Tv()); //순서대로 item[0] = new Tv();
item.add(new Tv());
item.add(new AirConditioner());
item.add(new Computer());
item.add(new Audio());
System.out.println(item.get(0).toString());
System.out.println(item.get(1).toString());
System.out.println(item.get(2).toString());
System.out.println(item.get(3).toString());
System.out.println(item.get(4).toString());
item.remove(item.get(1)); //삭제하면 빈 곳으로 객체주소 한칸씩 이동
System.out.println("--------------------");
System.out.println(item.get(0).toString());
System.out.println(item.get(1).toString());
System.out.println(item.get(2).toString());
System.out.println(item.get(3).toString()); //4를 얻으려하면 에러 발생
System.out.println("--------------------");
System.out.println("item isEmpty? : "+item.isEmpty());
System.out.println("--------------------");
System.out.println("item 갯수 : "+item.size());
}
}
class Product{}
class Tv extends Product{
public String toString() {return "Tv";}
}
class Audio extends Product{
public String toString() {return "Audio";}
}
class Computer extends Product{
public String toString() {return "Computer";}
}
class AirConditioner extends Product{
public String toString() {return "AirConditioner";}
}
>> 결과
Tv
Tv
AirConditioner
Computer
Audio
--------------------
Tv
AirConditioner
Computer
Audio
--------------------
item isEmpty? : false
--------------------
item 갯수 : 4
- 프로그램이 시작되었을 때 클래스변수, 클래스메서드가 메서드영역(클래스영역)에 적재되는 것을 응용 - 단순히 기능만 제공하는 서비스 객체(클래스)의 인스턴스를 반복적으로 생성해야할 때, 메모리의 불필요한 사용을 줄이고자 싱글톤을 사용한다.
<일상생활을 예로 이해하기 - 못 박기>
10번 못을 박기 위해, 1) 철물점에 가서 망치를 산다 -> 2) 못을 박는다. 2) 철물점에 가서 망치를 산다 -> 2) 못을 박는다. 3) 철물점에 가서.... x 10
->
10번 못을 박기 위해, 1) 철물점에 가서 망치를 산다. -> 2) 못을 박는다. 1) 이미 사둔 망치를 또 쓰자 -> 2) 못을 박는다. ....... x10
▼ 그림으로 표현한 싱글톤 원리
[예제_망치로 못을 박기]
package myutil;
public class MySingleTonTest {
public static void main(String[] args) {
int times = 10;
for(int i = 0; i < times; i++) {
Hammer hammer = Hammer.getInstance();
hammer.nail();
}
}
}
class Hammer{
public static Hammer single;
protected Hammer(){}
public static Hammer getInstance() {
if(single == null) {
single = new Hammer();
}
return single;
}
public void nail() {
System.out.println("못!");
}
}
문자를 입력하세요(Ctrl+z하면 완료)
hey there, wanna hang out? :)
hey there, wanna hang out? :)
알파벳 : 20
숫자 : 0
화이트 : 7
특수문자 : 4
[2] 사용자로부터 두 개의 양수와 연산자를 받아 계산하기
package practice2_Q;
import java.util.Scanner;
public class Q3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("두 개의 양수와 연산자를 입력하세요 : ");
int x = scanner.nextInt();
int y = scanner.nextInt();
String z = scanner.next();
int result = 0;
switch(z){
case "+" : result = x + y; break;
case "-" : result = x - y; break;
case "*" : result = x * y; break;
case "/" : result = x / y; break;
case "%" : result = x % y; break;
default : result = -1;
}
System.out.printf("%d %s %d = %d",x,z,y,result);
scanner.close();
}
}
>> 결과
두 개의 양수와 연산자를 입력하세요 : 2 3 +
2 + 3 = 5
[3] 사용자로부터 알파벳 문자를 하나 받아 나라명 출력하기 (if - else문 활용)
package practice2_Q;
import java.io.IOException;
public class Q4 {
public static void main(String[] args) throws IOException {
String[] nation = {"America","Bangladesh", "Canada", "Demmark","Ezypt", "France",
"Greece","Hungary","Iceland","Jamaica","Korea",
"Lebanon","Malta","North Korea","Oman","Peru","Qatar","Romania","Singapore",
"Thailand","Uganda","Vietnam","W로 시작하는 나라는 없습니다.","X로 시작하는 나라는 없습니다.","Yemen","Zambia"};
String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
System.out.print("문자 한 개를 입력하세요. >> ");
int ch = System.in.read();
//소문자 -> 대문자
if(ch >= 'a' || ch <= 'z') ch -= 32;
for(int i = 0; i < alpha.length(); i++) {
if(ch == alpha.charAt(i)) {
System.out.printf("%c로 시작하는 나라는 : %s",ch,nation[i]);
}
}
}
}
>> 결과
문자 한 개를 입력하세요. >> a
A로 시작하는 나라는 : America
3. 사칙연산 결과를 테이블 형식으로 출력
[1] 정상가격과 세일가격(30%) 출력하기 (정상가격 범위 : 20000원~30000원)
package practice2_Q;
public class Q5 {
public static void main(String[] args) {
System.out.println("정상가격 세일가격");
System.out.println("--------------------------");//14
for(int price = 20000; price <= 30000; price+=1000) {
System.out.printf("%-13d %.0f\n",price,price*0.7);
}
}
}
package practice2_Q;
import java.util.Scanner;
public class Q6 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("양수 n : ");
int n = scanner.nextInt();
int sum = 0;
for(int i = 1; i <= n; i++) {
sum += i;
}
System.out.println("sum : "+sum);
scanner.close();
}
}
>> 결과
양수 n : 5
sum : 15
[3] 1부터 10까지 양의 정수 중 짝수의 곱을 구하기
package practice2_Q;
public class Q7 {
public static void main(String[] args) {
int n = 10;
int result = 1;
for(int i = 1; i <= n; i++) {
if(i % 2 == 0)
result = result * i;
}
System.out.println("result : "+result);
}
}
>> 결과
result : 3840
[4] 원주율을 구하는 공식에 따라 원주율 값 구하기
4/1 - 4/3 + 4/5 - 4/7 + 4/9 .....
package practice2_Q;
public class Q8 {
public static void main(String[] args) {
int n = 1;
int sign = 1;
double result = 0;
for(long i = 0; i < 2000000000; i++) {
result += sign*(4.0/n);
sign = -sign;
n += 2;
}
System.out.println("PI = "+result);
}
}
>> 결과
PI = 3.141592658505181
4. 제곱과 팩토리얼
[1] (제곱) 0~6까지의 제곱과 세제곱을 구하여 테이블 형식으로 출력
package practice2_Q;
public class Q9 {
public static void main(String[] args) {
System.out.println("기존 제곱 세제곱");
System.out.println("-----------------------------");
for(int i = 0; i < 7; i++) {
System.out.printf("%-10d%-10d%d%n",i,i*i,i*i*i);
}
}
}
[2] (팩토리얼) 사용자에게 양의 정수 n을 받아, n! 구하기 (n! = n * (n-1)! // 단, 0!은 1이다)
package practice2_Q;
import java.util.Scanner;
public class Q10 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("n값 : ");
long n = scanner.nextInt();
long result = 1;
//f(x) = x * f(x-1)
//단, x가 0일 때 값은 1이다.
if(n == 0) {
result = 1;
}else {
for(long i = n; i > 0; i--) {
result = result * i;
}
}
System.out.printf("%d! = %d",n,result);
scanner.close();
}
}
팩토리얼은 고등수학에서 순열과 조합을 배우면서 배우는 것인데, 경우의 수를 구할 때 사용한다. 원리는 다음과 같다. 만약 A,B,C 세 사람이 앉는 순서를 정한다 할 때 경우의 수는 아래처럼 구할 수 있다. 0!이 1인 이유는, 아무것도 없을 때에 "없다"는 것도 하나의 경우에 해당하기 때문
[3] 실수의 거듭 제곱 값 구하기 (실수 : r, 정수 : n)
package practice2_Q;
import java.util.Scanner;
public class Q11 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("실수, 정수를 하나씩 입력하세요.(ex.5.34 7)");
double r = scanner.nextDouble();
int n = scanner.nextInt();
double result = 1;
for(int i = 0; i < n; i++) {
result = result * r;
}
System.out.printf("%f^%d = %f",r,n,result);
scanner.close();
}
}
>> 결과
실수, 정수를 하나씩 입력하세요.(ex.5.34 7)
5.231 3
5.231000^3 = 143.137741
package practice;
public class dan99TestBasic {
public static void main(String[] args) {
for(int i = 2; i <= 9; i++) {
System.out.println("--------["+i+"단 시작]--------");
for(int j = 1; j <= 9; j++) {
System.out.printf("%d x %d = %2d%n",i,j,i*j);
}
}
}
}
>> 결과
--------[2단 시작]--------
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
--------[3단 시작]--------
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
--------[4단 시작]--------
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
--------[5단 시작]--------
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
--------[6단 시작]--------
6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
--------[7단 시작]--------
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
--------[8단 시작]--------
8 x 1 = 8
8 x 2 = 16
8 x 3 = 24
8 x 4 = 32
8 x 5 = 40
8 x 6 = 48
8 x 7 = 56
8 x 8 = 64
8 x 9 = 72
--------[9단 시작]--------
9 x 1 = 9
9 x 2 = 18
9 x 3 = 27
9 x 4 = 36
9 x 5 = 45
9 x 6 = 54
9 x 7 = 63
9 x 8 = 72
9 x 9 = 81
[1-2] 사용자가 입력한 x단의 구구단을 출력하라_do_while 활용
(단, 사용자는 여러 번 구구단을 입력하고 결과를 받을 수 있어야 한다.)
package practice;
import java.util.Scanner;
public class Dan99Test2 {
public static void main(String[] args) {
int dan = 0;
String yn = "Y";
Scanner scanner = new Scanner(System.in);
do
{
System.out.print("단 입력(2~9단 중 하나) : ");
dan = scanner.nextInt();
if(dan >= 2 && dan <= 9) {
for(int i = 1; i <=9; i++) {
System.out.printf("%d x %d = %2d\n",dan,i,dan*i);
}
System.out.print("더 하시겠습니까? (y/n) :");
yn = scanner.next();
}else {
System.out.println("2~9 중 하나를 입력해주세요.");
}
}while(yn.equals("Y") || yn.equals("y"));
scanner.close();
}
}
>> 결과
단 입력(2~9단 중 하나) : 3
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
더 하시겠습니까? (y/n) :n