๊ด€๋ฆฌ ๋ฉ”๋‰ด

ruriruriya

[JAVA] ์ž๋ฐ”์˜ ๋ฐฐ์—ด ๋ณธ๋ฌธ

๐ŸชJava

[JAVA] ์ž๋ฐ”์˜ ๋ฐฐ์—ด

๋ฃจ๋ฆฌ์•ผใ…‘ 2023. 10. 17. 17:48
๋ฐ˜์‘ํ˜•

์‚ฌ์ง„: Unsplash ์˜ Fotis Fotopoulos

1. ์ž๋ฐ”์˜ ๋ฐฐ์—ด

์ž๋ฐ”์—์„œ๋Š” ๋ฐฐ์—ด์„ ํ™œ์šฉํ•ด ๋‹ค๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

- ๋ฐฐ์—ด์€ ํ•ญ์ƒ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค. ์ฆ‰, 0์ด ์ฒซ๋ฒˆ ์งธ๊ฐ€ ๋œ๋‹ค.

- ๋ฐฐ์—ด์€ ๊ฐ™์€ ๋ฐ์ดํ„ฐํ˜•์œผ๋กœ๋งŒ ์„ ์–ธํ•˜๊ณ  ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

- ๋ฐฐ์—ด์€ ์„ ์–ธํ•˜๋Š” ๋ฐฉ๋ฒ•์€ 4๊ฐ€์ง€๋กœ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.
๋ฐฐ์—ด ์„ ์–ธ ํ›„ ๊ฐ’์„ ๋ณ€๊ฒฝ ํ•  ์ˆ˜ ์žˆ๋‹ค.

public class Main {
    public static void main(String[] args) {
		//๋ฐฐ์—ด ์„ ์–ธ ์ฒซ๋ฒˆ ์งธ ๋ฐฉ๋ฒ•
		String[] coffees01 = new String[4];

		coffees01[0] = "์•„๋ฉ”๋ฆฌ์นด๋…ธ";
		coffees01[1] = "์นดํŽ˜๋ชจ์นด";
		coffees01[2] = "๋ผ๋–ผ";
		coffees01[3] = "์นดํ‘ธ์น˜๋…ธ";

		//๋ฐฐ์—ด ์„ ์–ธ ๋‘๋ฒˆ ์งธ ๋ฐฉ๋ฒ•
		String coffee02[] = new String[4];

		coffees02[0] = "์•„๋ฉ”๋ฆฌ์นด๋…ธ";
		coffees02[1] = "์นดํŽ˜๋ชจ์นด";
		coffees02[2] = "๋ผ๋–ผ";
		coffees02[3] = "์นดํ‘ธ์น˜๋…ธ";

		//๋ฐฐ์—ด ์„ ์–ธ ์„ธ๋ฒˆ ์งธ ๋ฐฉ๋ฒ•
		String[] coffees03 = new String[]{"์•„๋ฉ”๋ฆฌ์นด๋…ธ","์นดํŽ˜๋ชจ์นด","๋ผ๋–ผ","์นดํ‘ธ์น˜๋…ธ"};

		//๋ฐฐ์—ด ์„ ์–ธ ๋„ค๋ฒˆ ์งธ ๋ฐฉ๋ฒ•
		String[] coffes04 = {"์•„๋ฉ”๋ฆฌ์นด๋…ธ","์นดํŽ˜๋ชจ์นด","๋ผ๋–ผ","์นดํ‘ธ์น˜๋…ธ"};

		//๋ฐฐ์—ด ํ™œ์šฉ
		System.out.println(coffees04[0]+" ํ•˜๋‚˜");
		System.out.println(coffees04[1]+" ํ•˜๋‚˜");
		coffees[2] = "์—์Šคํ”„๋ ˆ์†Œ"; // ๊ฐ’ ๋ณ€๊ฒฝ
		System.out.println(coffees04[2]+" ํ•˜๋‚˜");
		System.out.println(coffees04[3]+" ํ•˜๋‚˜");
	}
}

 

- ๋ฌธ์ž์—ด ์™ธ ๋‹ค๋ฅธ ์ž๋ฃŒํ˜•์œผ๋กœ ๋ฐฐ์—ด ์„ ์–ธ ์‹œ

public class Main {
    public static void main(String[] args) {
    	int[] i = new int[3];
        
        i[0] = 1;
        i[1] = 2;
        i[2] = 3;
        
        double[] d = new double[]{20.5.45.5.88.2}; //double ๋ฐ์ดํ„ฐ ์„ธ๋ฒˆ ์งธ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฐฐ์—ด ์„ ์–ธ
        boolean[] b = {true,false,true}; // boolean ๋ฐ์ดํ„ฐ ๋„ค๋ฒˆ ์งธ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฐฐ์—ด ์„ ์–ธ
    }
}

 

2. ๋ฐฐ์—ด์˜ ์ˆœํšŒ

๋ฐฐ์—ด์˜ ๋ฐ์ดํ„ฐ ๊ฐ’์ด ๋งŽ์„ ๋•Œ ํ•˜๋‚˜ํ•˜๋‚˜ ์„ค์ •ํ•˜์—ฌ ์ถœ๋ ฅํ•˜๊ธฐ ์‰ฝ์ง€ ์•Š๋‹ค.
๊ทธ๋ž˜์„œ for๋ฌธ์„ ํ™œ์šฉํ•ด์„œ ์ข€๋” ํŽธํ•˜๊ฒŒ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

public class Main {
    public static void main(String[] args) {
        //for๋ฌธ์„ ์ด์šฉํ•œ ๋ฐฐ์—ด์˜ ์ˆœํšŒ

        String[] coffees = {"์•„๋ฉ”๋ฆฌ์นด๋…ธ","์นดํŽ˜๋ชจ์นด","๋ผ๋–ผ","์นดํ‘ธ์น˜๋…ธ"};

        for(int t =0;t< coffees.length;t++){ //int i ๋ณ€์ˆ˜ ์„ ์–ธ ํ›„ coffees์˜ ๊ธธ์ด๋งŒํผ 1์”ฉ ์ฆ๊ฐ€ํ•˜๋ฉฐ ์ถœ๋ ฅ
            System.out.println(coffees[t] + "ํ•˜๋‚˜");
        }
        System.out.println("์ฃผ๋ฌธ์ด์š”");
        System.out.println();

        //enhanced for(for-each) ๋ฐ˜๋ณต๋ฌธ
        for(String coffee : coffees){
            System.out.println(coffee+" ํ•˜๋‚˜");
        }
        System.out.println("์ฃผ๋ฌธ์ด์š”");

    }
}

 

๋ฐ˜์‘ํ˜•