Array

๋ฐฐ์—ด

  • ํŠน์ • ํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌถ์Œ์œผ๋กœ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ

  • ์ง€์ •๋œ ์ž๋ฃŒํ˜•๊ณผ ๊ฐœ์ˆ˜๋งŒํผ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ๋‚˜๋ž€ํžˆ ํ™•๋ณด

    • โš ๏ธ ๊ฐœ์ˆ˜์˜ ๋ณ€๊ฒฝ์ด ๋ถˆ๊ฐ€๋Šฅํ•จ

๋„
๊ฐœ
๊ฑธ
์œท
๋ชจ

0

1

2

3

4

package sec03.chap08;

public class Ex01 {
    public static void main(String[] args) {
        //  ๐Ÿ’ก ์‚ฌ์šฉํ•  ์ž๋ฃŒํ˜• ๋’ค์— []๋ฅผ ๋ถ™์—ฌ ์„ ์–ธ
        char[] yutnori = {'๋„', '๊ฐœ', '๊ฑธ', '์œท', '๋ชจ'};
        //char[] yutnori = new char[];
        //  ๐Ÿ’ก length : ๋ฐฐ์—ด์˜ ๊ธธ์ด ๋ฐ˜ํ™˜
        int length = yutnori.length;

        //  ๐Ÿ’ก [] ์•ˆ์— ์ธ๋ฑ์Šค ์ •์ˆ˜๋ฅผ ๋„ฃ์–ด ์ ‘๊ทผ
        //  โญ 0๋ถ€ํ„ฐ ์‹œ์ž‘
        char first = yutnori[0];
        char last = yutnori[yutnori.length - 1];
    }
}
        //  ๐Ÿ’ก ์ดˆ๊ธฐํ™”ํ•˜์ง€ ์•Š๊ณ  ์ผ๋‹จ ์„ ์–ธํ•˜๊ธฐ
        //  โญ ์–ด๋–ค ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”๋˜๋Š”์ง€ ํ™•์ธ
        boolean[] boolAry = new boolean[3];
        int[] intAry = new int[3];
        double[] dblAry = new double[3];
        char[] chrAry = new char[3]; // ์•„๋ž˜ ํ™•์ธ
        String[] strAry = new String[3];

        //  ์•„์Šคํ‚ค ์ฝ”๋“œ์˜ 0๋ฒˆ ๊ธ€์ž. ๋ฌธ์ž์—ด์˜ ๋์„ ํ‘œ์‹œํ•˜๋Š”๋ฐ ์‚ฌ์šฉ
        char NUL = chrAry[0];
        //  ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์›ํ•˜๋Š” ์œ„์น˜์˜ ๊ฐ’ ๋ณ€๊ฒฝ
        intAry[0] = 123;
        intAry[1] = 456;
        intAry[2] = 789;

์›์‹œ์ž๋ฃŒํ˜•์˜ ๋ฐฐ์—ด์€ ๊ทธ ๊ฐ’์ด ์ง์ ‘ ๋‹ด๊ธฐ๊ณ ,

์ฐธ์กฐ์ž๋ฃŒํ˜•์˜ ๋ฐฐ์—ด์€ ๊ทธ๊ฒƒ์ด ์ €์žฅ๋œ ์ฃผ์†Œ๋ฅผ ๊ฐ€์ง€๊ณ  ๋ฐฐ์—ด์„ ๋งŒ๋“ ๋‹ค.

๋ฐฐ์—ด ์ดˆ๊ธฐํ™”

package sec03.chap08;

public class Ex02 {
    public static void main(String[] args) {
        //  ์ดˆ๊ธฐํ™” ๋ธ”๋ก์„ ์‚ฌ์šฉํ•œ ์„ ์–ธ ๋™์‹œ ์ดˆ๊ธฐํ™”
        //  ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ• ์‚ฌ์šฉ ๊ฐ€๋Šฅ
        char[] dirAry1 = {'๋™', '์„œ', '๋‚จ', '๋ถ'};
        char[] dirAry2 = new char [] {'๋™', '์„œ', '๋‚จ', '๋ถ'};

        char[] dirAry3;

        //  ์„ ์–ธ๋งŒ ๋จผ์ € ํ•œ ์ƒํƒœ์—์„œ๋Š” ๋‘ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•๋งŒ ๊ฐ€๋Šฅ
        //dirAry3 = {'๋™', '์„œ', '๋‚จ', '๋ถ'}; // โš ๏ธ ๋ถˆ๊ฐ€
        dirAry3 = new char[] {'๋™', '์„œ', '๋‚จ', '๋ถ'};

        int [] intAry = {1, 2, 3, 4, 5};

        //  โš ๏ธ ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ: ArrayIndexOutOfBoundsException
        int outOfAry = intAry[intAry.length];
    }
}

๋‹ค์ค‘ ๋ฐฐ์—ด

package sec03.chap08;

public class Ex03 {
    public static void main(String[] args) {
        //  ์ด์ค‘ ๋ฐฐ์—ด
        boolean[][] dblBoolAry = new boolean[3][3];

        int[][] dblIntAry = new int[][] {
                //  โญ๏ธ ์š”์†Œ ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๊ฐ€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Œ
                {1, 2, 3},
                {4, 5},
                {6, 7, 8, 9},
        };

        //  ์‚ผ์ค‘ ๋ฐฐ์—ด
        char[][][] trpChrAry = {
                {{'์ž', '์ถ•'}, {'์ธ', '๋ฌ˜'}},
                {{'์ง„', '์‚ฌ'}, {'์˜ค', '๋ฏธ'}},
                {{'์‹ ', '์œ '}, {'์ˆ ', 'ํ•ด'}},
        };

        int int1 = dblIntAry[0][1];
        int int2 = dblIntAry[2][3];
        int[] intAry = dblIntAry[1];

        char[][] dblChrAry = trpChrAry[0];
        char[] chrAry = dblChrAry[0];
        char chr1 = chrAry[1];
        char chr2 = trpChrAry[2][0][1];

    }
}

์›์‹œ ์ž๋ฃŒํ˜• primitive type vs ์ฐธ์กฐ ์ž๋ฃŒํ˜• reference type

package sec03.chap08;

public class Ex04 {

    public static void main(String[] args) {
        //  ๐Ÿงช ๋””๋ฒ„๊น…ํ•ด์„œ ๊ฒฐ๊ณผ๋ฅผ ๋ณผ ๊ฒƒ
        //  โญ ์›์‹œ ์ž๋ฃŒํ˜•์€ ๊ฐ’ ์ž์ฒด๋ฅผ ๋ณต์‚ฌ - ๋ณ„๊ฐœ์˜ ๊ฐ’์ด ๋จ

        boolean bool1 = true;
        boolean bool2 = false;
        bool2 = bool1; // ๐Ÿ”ด bool2์— bool1์˜ ๊ฐ’์„ ๋ณต์‚ฌํ•ด์„œ ๋„ฃ์–ด์คŒ
        bool1 = false;

        int int1 = 1;
        int int2 = 2;
        int2 = int1;
        int2 = 3;

        char ch1 = 'A';
        char ch2 = 'B';
        ch2 = ch1;
        ch1 = '๊ฐ€';


        //  โญ ์ฐธ์กฐ ์ž๋ฃŒํ˜•์€ ๊ฐ’ ์ฃผ๋จธ๋‹ˆ์˜ ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌ
        //  ๋‘ ๋ณ€์ˆ˜๊ฐ€ ๊ฐ™์€ ์ฃผ๋จธ๋‹ˆ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ๋จ

        boolean[] boolAry1 = { true, true, true };
        boolean[] boolAry2 = { false, false, false };
        boolAry2 = boolAry1; // ๐Ÿ”ด
        boolAry1[0] = false;
        //


        int[] intAry1 = { 1, 2, 3 };
        int[] intAry2 = { 4, 5 };
        intAry2 = intAry1;
        intAry2[1] = 100;

        char[] chAry1 = { 'A', 'B', 'C' };
        char[] chAry2 = { 'a', 'b', 'c', 'd', 'e' };
        chAry2 = chAry1;
        chAry1[2] = '๋‹ค';
    }
}

๋ฌธ์ž์—ด ๋ฐฐ์—ด

package sec03.chap08;

public class Ex05 {
    public static void main(String[] args) {
        //  โญ๏ธ ๋ฌธ์ž์—ด์€ ๊ฐ์ฒด(์ฐธ์กฐํ˜•)์ง€๋งŒ ์›์‹œํ˜•์ฒ˜๋Ÿผ ๋‹ค๋ค„์ง
        String str1 = "์ฝ”์ธ ํ•จ";
        String str2 = "๊ด€์‹ฌ ์—†์Œ";
        str2 = str1; // ๐Ÿ”ด

        str1 = "๊ณ ์ ์— ์ต์ ˆ";
    }
}

์ƒ์ˆ˜ ๋ฐฐ์—ด

package sec03.chap08;

public class Ex06 {
    public static void main(String[] args) {
        //  ์ƒ์ˆ˜ ๋ฐฐ์—ด์˜ ๊ฒฝ์šฐ
        final int[] NUMBERS = {1, 2, 3, 4, 5};

        //  โš ๏ธ ๋‹ค๋ฅธ ๋ฐฐ์—ด์„ ํ• ๋‹นํ•˜๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€
        //NUMBERS = new int[] {2, 3, 4, 5, 6};

        //  โญ๏ธ ๋ฐฐ์—ด์˜ ์š”์†Œ๋ฅผ ๋ฐ”๊พธ๋Š” ๊ฒƒ์€ ๊ฐ€๋Šฅ
        NUMBERS[0] = 11;
    }
}

๋ฌธ์ž์—ด ๋ฐฐ์—ด ๊ด€๋ จ String ๊ธฐ๋Šฅ

package sec03.chap08;

public class Ex07 {
    public static void main(String[] args) {
        String[] strings = {"ํ•œ๋†ˆ", "๋‘์‹œ๊ธฐ", "์„์‚ผ", "๋„ˆ๊ตฌ๋ฆฌ"};

        //  ๐Ÿ’ก join ์ •์  ๋ฉ”์†Œ๋“œ - ๋ฌธ์ž์—ด(์ •ํ™•ํžˆ๋Š” CharSequence)์˜ ๋ฐฐ์—ด์„ ํ•˜๋‚˜๋กœ ์ด์–ด๋ถ™์ž„
        //  ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋ฅผ ๊ฐ ์‚ฌ์ด์— ์‚ฝ์ž…
        String join1 = String.join(", ", strings);
        String join2 = String.join("-", strings);
        String join3 = String.join(" ๊ทธ๋ฆฌ๊ณ  ", strings);
        String join4 = String.join("", strings);
        
    }
}

Last updated