참조배열

지환·2023년 10월 11일
0

JAVA

목록 보기
27/39

Test1

package com.step5;

//데이터를 처리하기 위한 클래스 설계 
class DeptVO
{
	private int deptno;
	private String dname;
	private String loc;
	public int getDeptno() {
		return deptno;
	}
	public void setDeptno(int deptno) {
		this.deptno = deptno;
	}
	public String getDname() {
		return dname;
	}
	public void setDname(String dname) {
		this.dname = dname;
	}
	public String getLoc() {
		return loc;
	}
	public void setLoc(String loc) {
		this.loc = loc;
	}
	

	
	
	
	
}
public class DeptTest {

	public static void main(String[] args) {
		
		DeptVO[] is = new DeptVO[3];
		is[0] = new DeptVO();
		is[0].setDeptno(30);
		System.out.println(is[0].getDeptno());

	}

}

(2)

package com.step5;

import java.util.Scanner;

//데이터를 처리하기 위한 클래스 설계 
class DeptVO
{
	private int deptno;
	private String dname;
	private String loc;
	public int getDeptno() {
		return deptno;
	}
	public void setDeptno(int deptno) {
		this.deptno = deptno;
	}
	public String getDname() {
		return dname;
	}
	public void setDname(String dname) {
		this.dname = dname;
	}
	public String getLoc() {
		return loc;
	}
	public void setLoc(String loc) {
		this.loc = loc;
	}
	

	
	
	
	
}
public class DeptTest {
	

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		DeptVO[] is = new DeptVO[3];

		for(int i = 0; i < is.length; i++)
		{
			int input = sc.nextInt();
			is[i] = new DeptVO();
			is[i].setDeptno(input);
			
		}
		
		for(DeptVO e : is)
		{
			System.out.println(e.getDeptno());
		}
	}

}

(3)

package com.step5;

import java.util.Scanner;

//데이터를 처리하기 위한 클래스 설계 
class DeptVO
{
	private int deptno;
	private String dname;
	private String loc;
	public int getDeptno() {
		return deptno;
	}
	public void setDeptno(int deptno) {
		this.deptno = deptno;
	}
	public String getDname() {
		return dname;
	}
	public void setDname(String dname) {
		this.dname = dname;
	}
	public String getLoc() {
		return loc;
	}
	public void setLoc(String loc) {
		this.loc = loc;
	}
	
	public DeptVO(int deptno, String dname, String loc)
	{
		this.deptno = deptno;
		this.dname = dname;
		this.loc =loc;
	}
	
	
	
	
}
public class DeptTest {
	

	public static void main(String[] args) {
		
		DeptVO[] dVOs = new DeptVO[3];
		
		
		DeptVO dVO1 = new DeptVO(10,"영업부", "대전");
		dVOs[0] = dVO1;
		
		DeptVO dVO2 = new DeptVO(20,"개발부", "대구");
		dVOs[1] = dVO2;
		
		DeptVO dVO3 = new DeptVO(30, "운영부", "부산");
		dVOs[2] = dVO3;
		
		if(dVO3 == dVOs[2])
			System.out.println("같은 주소 번지다.");
		
		for(int i = 0; i<dVOs.length; i++)
		{
			System.out.println(dVOs[i].getDeptno() + dVOs[i].getDname() + dVOs[i].getLoc());
			
			System.out.println("===============================================");
			
			System.out.println(dVOs[i].toString());
			
		}
		/*
		 * dVOs[i] 하면 당연히 주소번지가 나온다. why? 불변객체기 때문에 getter setter를 사용하여 작성해야 된다.
		 */
		
		int[] k = new int[3];
		
		for(int i =0; i<k.length; i++)
		{
			System.out.println("===============================================");
			System.out.println(k[i]);
		}
		/*
		 * 반면 이 경우는 원시형타입이기 때문에 getter / setter로 구현된 것이 아니라, 사용하는 것이다.
		 */
		
	}

}
-------------------------------------------------------------------------
같은 주소 번지다.
10영업부대전
===============================================
com.step5.DeptVO@372f7a8d
20개발부대구
===============================================
com.step5.DeptVO@2f92e0f4
30운영부부산
===============================================
com.step5.DeptVO@28a418fc
===============================================
0
===============================================
0
===============================================
0

(4)

package com.step5;

class DepT
{
	
	private int deptno;
	private String dname;
	private String loc;
	public int getDeptno() {
		return deptno;
	}
	public void setDeptno(int deptno) {
		this.deptno = deptno;
	}
	public String getDname() {
		return dname;
	}
	public void setDname(String dname) {
		this.dname = dname;
	}
	public String getLoc() {
		return loc;
	}
	public void setLoc(String loc) {
		this.loc = loc;
	}
	
	public DepT(int deptno, String dname, String loc)
	{
		this.deptno = deptno;
		this.dname = dname;
		this.loc =loc;
	}
	
	}

public class Deot {
	
	public static void main(String[] args) {
		
		DepT[] dt = new DepT[3];
		
		dt[0] = new DepT(30, "지환", "인천광역시");
		dt[1] = new DepT(40, "금진", "서울");
		
		DepT a = new DepT(70, "연진", "성남");
		dt[2] = a;
		
		for(int i = 0; i<dt.length; i++)
		{
			System.out.println(dt[i].getDeptno() + dt[i].getDname()+dt[i].getLoc());
			System.out.println(dt[i]);
			System.out.println("===============================================");
		}
		
	}

	
	
	

}
-------------------------------------------------------------------------------
30지환인천광역시
com.step5.DepT@372f7a8d
===============================================
40금진서울
com.step5.DepT@2f92e0f4
===============================================
70연진성남
com.step5.DepT@28a418fc
===============================================
  • dt[2] = a; 이런 형태로도 사용가능하다.

(5) - ToString 구현

package com.step5;

class DepT
{
	
	private int deptno;
	private String dname;
	private String loc;
	public int getDeptno() {
		return deptno;
	}
	public void setDeptno(int deptno) {
		this.deptno = deptno;
	}
	public String getDname() {
		return dname;
	}
	public void setDname(String dname) {
		this.dname = dname;
	}
	public String getLoc() {
		return loc;
	}
	public void setLoc(String loc) {
		this.loc = loc;
	}
	
	public DepT(int deptno, String dname, String loc)
	{
		this.deptno = deptno;
		this.dname = dname;
		this.loc =loc;
	}
	@Override
	public String toString() {
		return "DepT [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + "]";
	}
	
	
	
	}

public class Deot {
	
	public static void main(String[] args) {
		
		DepT[] dt = new DepT[3];
		
		dt[0] = new DepT(30, "지환", "인천광역시");
		dt[1] = new DepT(40, "금진", "서울");
		
		DepT a = new DepT(70, "연진", "성남");
		dt[2] = a;
		
		for(int i = 0; i<dt.length; i++)
		{
			System.out.println(dt[i].getDeptno() + dt[i].getDname()+dt[i].getLoc());
			System.out.println(dt[i].toString());
			System.out.println("===============================================");
		}
		
	}

	
	
	

}

(6)

public class Array10 {
            public static void main(String[] args) {
                int[][] array = new int[2][];
                array[0] = new int[2];
                array[1] = new int[3];

                array[0][0] = 0;
                array[0][1] = 1;
                array[0][2] = 2;


                array[1][0] = 3;
                array[1][1] = 4;
                array[1][2] = 5;

                for(int i = 0; i < array.length; i++) //2까지
                {
                    for(int j = 0; j < array[i].length; i++) //012 ->  3까지
                    {
                        System.out.println(array[i][j] + "\t");
                    }
                    System.out.println();
                }

            }

}
package com.week5;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

//1차 객체 배열 연습
public class JButtonArray implements ActionListener{
	//선언부
	JFrame jf = new JFrame();
	JButton jbtns[] = new JButton[4];
	String jbtnsLabels[] = {"one","two","three","four"};
	
	//생성자
	JButtonArray() {
		//객체 배열 초기화 하기 - 생성자는 전변의 초기화 담당함
		for(int i=0;i<jbtns.length;i++) {//jbtns.length=4
			jbtns[i] = new JButton(jbtnsLabels[i]);
			jbtns[i].addActionListener(this);
		}
	}
	
	//화면처리부
	public void initDisplay() {
		jf.setLayout(new GridLayout(1,4));
		for(int i=0;i<jbtns.length;i++) {
			
			jf.add(jbtns[i]);
		}
		jf.setTitle("객체 배열 연습1");
		jf.setSize(500, 300);
		jf.setVisible(true);
	}
	
	//메인메소드
	public static void main(String[] args) {
		JButtonArray ja = new JButtonArray();
		ja.initDisplay();
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stdub
		
	}

}
profile
아는만큼보인다.

0개의 댓글