개발메모장

ArrayList 예제

dorosy 2014. 12. 18. 16:56

1. ArrayList 로 만드는 스택과 큐

1) 스택 (가장 마지막에 들어간 데이터가 가장 먼저 나온다.)

 

class ArrayListStack{
 private ArrayList list = new ArrayList();    //ArrayList  객체 생성 

 

//push를 호출하면 배열에 문자 하나씩 push!

 public void push(char ch){
  list.add(ch);
 }

 

//pop을 호출하면, 배열의 가장 마지막 데이터 하나씩 빼내어 반환.
 public char pop(){
  return (char)list.remove(list.size() - 1);
 }

 

2) 큐 (가장 먼저 들어간 데이터가 가장 먼저 나온다.)

 

class ArrayListQueue{
 private ArrayList list = new ArrayList();   //ArrayList 객체 생성

 

//push를 호출하면, 배열에 데이터 하나씩 집어 넣는다.
 public void push(char ch){
  list.add(ch);
 }

 

//pop을 호출하면 배열의 가장 첫번째 데이터를 하나씩 빼내어 반환
 public char pop(){
  return (char)list.remove(0);
 }
}

 

 

2. 예제

//주소와 전화번호로 이뤄진 info 클래스를 작성하시오.
//이름, info, 잔액으로 이뤄진 고객 클래스를 작성하시오.
// 고객들의 목록을 관리하는 고객 리스트 클래스를 작성하시오.
// 고객을 추가하고 고객들의 목록을 출력하는 기능이 필요하다.

 

1) Info class

 

public class Info {
 private String address;
 private String tel;
 
 //기본 생성자
 public Info() {
  this("-", "-");
 }
 //완전한 생성자
 public Info(String address, String tel) {
  this.address = address;
  this.tel = tel;
 }
 

//Info의 정보를 String 형식으로 반환하는 함수.
 public String toString(){
  return "address : " + address + " tel : "+ tel;
 }
 

//get, set 함수들
 public String getAddress() {
  return address;
 }
 public void setAddress(String address) {
  this.address = address;
 }
 public String getTel() {
  return tel;
 }
 public void setTel(String tel) {
  this.tel = tel;
 }
}

 

2) Client Class (Info Class 의 객체를 포함)

 

public class Client {
 private String name;
 private Info info;
 private int change;
 

//기본 생성자.
 public Client() {
  this("-", new Info(), 0);
 }

 

//완전한 생성자.

 public Client(String name, Info info, int change) {
  this.name = name;
  this.info = info;
  this.change = change;
 }
 

//Client  전체 정보를 String 형태로 반환하는 함수.
 public String toString(){
  return "Name : " + name + ", Address : " + info.getAddress() + ", Tel. : " + info.getTel()
    + ", 잔액 : " + change;
 }

 

//get, set 함수들

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getAddress() {
  return this.info.getAddress();
 }
 public String getTel(){
  return this.info.getTel();
 }

 public void setAddress(String address) {
  this.info.setAddress(address);
 }
 public void settel(String tel){
  this.info.setTel(tel);
 }

 public int getChange() {
  return change;
 }

 public void setChange(int change) {
  this.change = change;
 }
}

 

 

3) ClientList Class

 

//고객들의 목록을 관리하는 고객 리스트 클래스를 작성하시오.
//고객을 추가하고 고객들의 목록을 출력하는 기능이 필요하다.

 

public class ClientList {
 private ArrayList<Client> clientList;  //Client  객체들을 담을 배열을 정의
 

//생성자.
 public ClientList() {
  this(new ArrayList<Client>());
 }

 public ClientList(ArrayList clientList) {
  this.clientList = clientList;
 }
 

//1명의 Client 객체를 clientlist 배열에 집어넣음.
 public void addClient(Client client){
  clientList.add(client);
 }
 

//clientList 에 있는 Client 들의 전체 정보를 출력하는 함수.
 public void printClientList(){
  Client temp;
  for(int i =0; i< clientList.size(); i++){
   temp = (Client)clientList.get(i);
   System.out.println("name : "+ temp.getName()+ " Address : " + temp.getAddress()
     + " Tel. : " + temp.getTel() + " 잔돈 : " + temp.getChange());
  }
 }
 
 // 고객의 이름으로 검색하여 일치하는 고객의 잔돈 액수를 반환하는 함수.
 public int searchChange(String name){
  Client temp = new Client();
  int i;
  for(i =0; i< clientList.size(); i++){
   temp = (Client)clientList.get(i);
   if(name.matches(temp.getName()))
    break;
  }

  if(i == clientList.size()){
   System.out.println("해당 고객이 없습니다.");
   return 0;
  }
  else{
   return temp.getChange();
  }
 }
 

//고객의 이름으로 검색하여, 일치하는 고객의 주소 정보를 반환하는 함수.
 public String searchAddress(String name){
  Client temp = new Client();
  int i;
  for(i =0; i< clientList.size(); i++){
   temp = (Client)clientList.get(i);
   if(name.matches(temp.getName()))
    break;
  }
  if(i == clientList.size()){
   System.out.println("해당 고객이 없습니다.");
   return "-";
  }
  else{
   return temp.getAddress();
  }
 }
 

//고객 이름으로 검색하여, 일치하는 고객의 모든 정보를 String 형태로 반환하는 함수.
 public String searchAll(String name){
  Client temp = new Client();
  int i;
  for(i =0; i< clientList.size(); i++){
   temp = (Client)clientList.get(i);
   if(name.matches(temp.getName()))
    break;
  }
  if(i == clientList.size()){
   System.out.println("해당 고객이 없습니다.");
   return "-";
  }
  else{
   String str = "name : "+ temp.getName()+ ", Address : " + temp.getAddress()
     + ", Tel. : " + temp.getTel() + ", 잔돈 : " + temp.getChange();
   return str;
  }
 }
 
 
 public static void main(String[] args) { 

  ClientList myList = new ClientList();


//1번째 고객 정보를  client List 에 집어넣음(default 값으로.)
  myList.addClient(new Client());
  

//2번째 고객정보를 clientList에 집어넣음
  Client client = new Client("halim", new Info("Incheon", "8859"), 900);
  myList.addClient(client);


//3번째 고객정보를 clientList에 집어넣음.
  myList.addClient(new Client("Somi", new Info("Gang", "9297"), 2000));
  

//halim 이라는 이름을 가진 고객의 잔돈 정보를 출력!
  System.out.println(myList.searchChange("halim"));

//halim 이라는 이름을 가진 고객의 주소 정보를 출력   
  System.out.println(myList.searchAddress("halim"));

//Somi 라는 이름을 가진 고객의 전체 정보를 출력.
  System.out.println(myList.searchAll("Somi"));
 }

}