ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 210810_Vector
    JAVA 2021. 8. 20. 17:47

    (클래스1-Board)

    public class Board {

    //멤버클래스

     

    private String name;

    private String subject;

    private String content;

     

    //디폴트생성자

    public Board(){

    }

     

    //명시적생성자

        public Board(String name,String subject, String content){

    this.name=name;

    this.subject=subject;

    this.content=content;

    }

     

    //setter,getter

    public String getName() {

    return name;

    }

     

    public void setName(String name) {

    this.name = name;

    }

     

    public String getSubject() {

    return subject;

    }

     

    public void setSubject(String subject) {

    this.subject = subject;

    }

     

    public String getContent() {

    return content;

    }

     

    public void setContent(String content) {

    this.content = content;

    }

    }

     

     

    (클래스2-VectorBoardEx12)

    public class VectorBoardEx12 {

     

    List<Board> list=new Vector<Board>();

    //Vector<Board> list=new vector<Board>();  이렇게도 가능

     

    //클래스형으로 지정했으므로 데이터를 꺼내고 받을 때도 클래스

     

    public void inPutData() {

    Scanner sc=new Scanner(System.in);

    String name,subject,content;

    System.out.println("작성자명");

    name=sc.nextLine();

    System.out.println("게시판 제목");

    subject=sc.nextLine();

    System.out.println("게시판 내용");

    content=sc.nextLine();

     

    //Board 멤버클래스 생성해줘야함

    //Board data=new Board(name, subject, content);

    //list.add(data); //list에 data 넣기

    list.add(new Board(name, subject, content));  //위에 두줄을 이렇게 한줄로 가능

     

    System.out.println("현재 데이터 개수: "+list.size());

    }

     

     

    public void outPutData() {

     

    System.out.println("***게시판***");

    System.out.println("=============================");

     

    for(int i=0;i<list.size();i++)

    {

    //리스트내의 지정된 위치에 있는 요소값을 찾아준다

    Board b=list.get(i);

     

    System.out.println("No."+(i+1)+"\t작성자: "+b.getName());

    System.out.println("제목: "+b.getSubject());

    System.out.println("내용: "+b.getContent());

    System.out.println("============================");

    }

    }

     

     

    public static void main(String[] args) {

     

    //VectorBoardEx12안에 있는 인풋,아우풋 데이터 가져오기 위해 생성

    VectorBoardEx12 vb=new VectorBoardEx12();

    int n=0;

     

    Scanner sc=new Scanner(System.in);

     

    while(true) //한번만 입력하는 게 아니라 브레이크문 빠져나갈때 까지 입력하기에 while문

    {

    System.out.println("1.추가   2.전체출력   9.종료");

    n=Integer.parseInt(sc.nextLine()); //형변환

     

    if(n==1)  //switch문으로 해도 됨

    vb.inPutData();

    else if(n==2)

    vb.outPutData();

    else {

    System.out.println("종료");

    break;

    }

    }

     

    //결과

    1.추가   2.전체출력   9.종료

    1

    작성자명

    요다

    게시판 제목

    자바는 재밌다

    게시판 내용

    자바가 어렵지만 재밌네요

    현재 데이터 개수: 1

    1.추가   2.전체출력   9.종료

    2

    ***게시판***

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

    No.1 작성자: 요다

    제목: 자바는 재밌다

    내용: 자바가 어렵지만 재밌네요

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

    1.추가   2.전체출력   9.종료

     

     

     

     

     

     

    }

     

    }

    'JAVA' 카테고리의 다른 글

    210823_Swing  (0) 2021.08.23
    210823_Array+List  (0) 2021.08.23
    210820_Map  (0) 2021.08.20
    210820_ArrayList  (0) 2021.08.20
    210820_List+Vector  (0) 2021.08.20

    댓글

Designed by Tistory.