본문 바로가기

콩's EDUCATION/콩's JAVA_RUN

Vector 저장데이터, 총크기, 데이터 조회

Vector 저장데이터, 총 크기, 데이터 조회


 

import java.util.*;

class Employee{
 int id;   // 사번
 String name; // 이름
 double pay;  // 급여
 // 멤버 변수를 초기화해주는 것이 생성자(기본 개념 숙지)
 public Employee(int id, String name, double pay) {
  this.id = id;
  this.name = name;
  this.pay = pay;
 }
 @Override
 public String toString() {
  return "Employee [id=" + id + ", name=" + name + ", pay=" + pay + "]";
 }
 
}
public class VectorTest {
 public static void main(String[] args) {
  /**
   * Vector 생성
   * 100(interger), 3.14(Double), "java", Employeee 객체들 저장
   */
 Vector all = new Vector(5); // 5+5+5+.....
 all.add(100);
 all.add(3.14);
 all.add("java");
 
 Employee e1 = new Employee(100, "홍길동", 67000.99);
 all.add(e1);
 Employee e2 = new Employee(200, "강감찬", 31000.14);
 all.add(e2); 
 Employee e3 = new Employee(300, "장보고", 19510.50);
 all.add(e3); // 5+5 = 10 자동증가 add
 System.out.println("저장된 데이터의 갯수 ; "+all.size());
 System.out.println("벡터의 총 크기 ; "+ all.capacity());
 // 벡터 데이터 조회 ; all.elementData 불가능
 for(int i=0;i<all.size();i++){
  /*
   *  100, 3.14, java
   *  Employee@16진수 숫자 ; 주소값 정보 표현
   *  toString() 오버라이딩이 필요하다.
   */
  System.out.println(all.get(i));
 }
 }

}

 

 

 

VectorTest.java