๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java

[Java] ๋ฐฐ์—ด

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 2. 19.
๋ฐ˜์‘ํ˜•

โ–ถ ๋ฐฐ์—ด์ด๋ž€?

ํ•™์ƒ 100๋ช…์˜ ํ•™๋ฒˆ์„ ๊ด€๋ฆฌํ•  ๋•Œ 1..100 ๊นŒ์ง€ ์ผ์ผ์ด ์“ฐ๋Š”๊ฒƒ์€ ๋„ˆ๋ฌด ๊ท€์ฐฎ๊ณ  ๋ฒˆ๊ฑฐ๋กญ๋‹ค. ์ด๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์ž๋ฃŒํ˜•์ด ๋ฐฐ์—ด(array)์ด๋‹ค. ๋ฐฐ์—ด์€ ์ž๋ฃŒ ๊ตฌ์กฐ์˜ ๊ฐ€์žฅ ๊ธฐ์ดˆ ๋‚ด์šฉ์ด๋‹ค.

โ€‹

 

โ€‹

โ–ถ ๋ฐฐ์—ด ์„ ์–ธ

๋ฐฐ์—ด๋„ ๋ณ€์ˆ˜์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ž๋ฃŒํ˜•์„ ํ•จ๊ป˜ ์„ ์–ธํ•œ๋‹ค.

new ์˜ˆ์•ฝ์–ด๋Š” ๋ฐฐ์—ด์„ ์ƒˆ๋กœ ๋งŒ๋“ค๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค.

int[] studentIDs = new int[10];

์œ„ ๋ฌธ์žฅ์€ intํ˜• ์š”์†Œ๊ฐ€ 10๊ฐœ์ธ ๋ฐฐ์—ด์„ ์„ ์–ธํ•œ ๊ฒƒ์ด๋‹ค

โ€‹

 

โ€‹

โ–ถ ๋ฐฐ์—ด ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ

์ž๋ฐ”์—์„œ ๋ฐฐ์—ด์„ ์„ ์–ธํ•˜๋ฉด ๊ทธ์™€ ๋™์‹œ์— ๊ฐ ์š”์†Œ์˜ ๊ฐ’์ด ์ดˆ๊ธฐํ™”๋œ๋‹ค.

๋ฐฐ์—ด ์„ ์–ธ๊ณผ ๋™์‹œ์— ํŠน์ • ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™” ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

11๋ฒˆ์งธ ์ค„์ด ์™œ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜๋Š”๊ฐ€?

 

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

 

โ€‹

โ€‹

โ–ถ ๋ฐฐ์—ด ์ˆœ์„œ๋Š” 0๋ฒˆ๋ถ€ํ„ฐ

๋ฐฐ์—ด ๊ธธ์ด๊ฐ€ n์ด๋ผ๊ณ  ํ•˜๋ฉด ๋ฐฐ์—ด ์ˆœ์„œ๋Š” 0๋ฒˆ๋ถ€ํ„ฐ n-1๋ฒˆ๊นŒ์ง€๋‹ค.

0๋ฒˆ ์š”์†Œ๋ฅผ ๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ผ๊ณ  ํ•œ๋‹ค.

package array;

public class ArrayTest {
	public static void main(String[] args) {

		int[] number = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

		for (int i = 0; i < number.length; i++) {  // ๋ฐฐ์—ด ๊ธธ์ด ์ถœ๋ ฅ
			System.out.println(number[i]);
		}
	}
}

 

 

 

1๋ถ„ ๋ณต์Šต ) ์œ„ ์˜ˆ์ œ์˜ main( ) ํ•จ์ˆ˜์— int sum = 0; ์„ ์ž‘์„ฑํ•˜๊ณ , 8ํ–‰ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜์—ฌ ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ ํ•ฉ์„ ๊ณ„์‚ฐํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค์–ด ๋ณด์„ธ์š”.

โ€‹

<๋‚ด๊ฐ€ ํ•œ ๊ฒƒ>

package array;

public class ArrayTest {
	public static void main(String[] args) {
        int sum = 0;
		int[] num = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

		for (int i = 1; i <= 10; i++) {
            sum += i;
			System.out.println(sum);
		}
	}
}

<๊ฒฐ๊ณผ๊ฐ’>

โ€‹

๋”๋ณด๊ธฐ

<์„ ์ƒ๋‹˜์ด ํ•œ ๊ฒƒ>

package array;

public class ArrayTest {
	public static void main(String[] args) {
		int[] numb = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        int sum = 0;

		for (int i = 0; i < num.length; i++) {
            sum += num[i];
			System.out.println("ํ•ฉ๊ณ„ : " + sum);
		}
	}
}

<๊ฒฐ๊ณผ๊ฐ’>

โ€‹

 

 

 

โ–ถ ์ „์ฒด ๋ฐฐ์—ด ๊ธธ์ด๋งŒํผ ์ถœ๋ ฅํ•˜๊ธฐ

package array;

public class ArrayTest2 {
	public static void main(String[] args) {

		double[] data = new double[5];  // doubleํ˜•์œผ๋กœ ๊ธธ์ด 5์ธ ๋ฐฐ์—ด ์„ ์–ธ

		data[0] = 10.0;
		data[1] = 20.0;
		data[2] = 30.0;

		for (int i = 0; i < data.length; i++) {  // ์ „์ฒด ๋ฐฐ์—ด ๊ธธ์ด๋งŒํผ ๋ฐ˜๋ณต
			System.out.println(data[i]);

		}
	}
}

<๊ฒฐ๊ณผ๊ฐ’>

โ€‹

 

package array;

public class ArrayTest3 {
	public static void main(String[] args) {
		double[] data = new double[5];
		int size = 0;  // ์œ ํšจํ•œ ๊ฐ’์ด ์ €์žฅ๋œ ๋ฐฐ์—ด ์š”์†Œ ๊ฐœ์ˆ˜๋ฅผ ์ €์žฅํ•  ๋ณ€์ˆ˜ ์„ ์–ธ

		data[0] = 10.0; size++;
		data[1] = 20.0; size++;
		data[2] = 30.0; size++;  // ๊ฐ’์„ ์ €์žฅํ•œ ํ›„ size ๋ณ€์ˆ˜ ๊ฐ’ ์ฆ๊ฐ€

		for (int i = 0; i < size; i++) {  // ์œ ํšจํ•œ ๊ฐ’์ด ์ €์žฅ๋œ ๋ฐฐ์—ด ์š”์†Œ ๊ฐœ์ˆ˜๋งŒํผ ๋ฐ˜๋ณต๋ฌธ ์‹คํ–‰
			System.out.println(data[i]);

		}
	}
}

-12ํ–‰ ๋ฐ˜๋ณต๋ฌธ์€ ์ „์ฒด ๋ฐฐ์—ด ๊ธธ์ด๋งŒํผ ๋ฐ˜๋ณตํ•˜๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ ์œ ํšจํ•œ ์š”์†Œ ๊ฐœ์ˆ˜๋งŒํผ๋งŒ ๋ฐ˜๋ณตํ•œ๋‹ค.

โ€‹

โ€‹

<๊ฒฐ๊ณผ๊ฐ’>

โ€‹

 

 

โ–ถ ๋ฌธ์ž ์ €์žฅ ๋ฐฐ์—ด ๋งŒ๋“ค๊ธฐ

package array;

public class CharArray {
	public static void main(String[] args) {

		char[] alpahabets = new char[26];
		char ch = 'A';

		for (int i = 0; i < alpahabets.length; i++, ch++) {
			alpahabets[i] = ch;
		}

		for (int i = 0; i < alpahabets.length; i++) {
			System.out.println(alpahabets[i] + "," + (int) alpahabets[i]);
		}
	}
}

<๊ฒฐ๊ณผ๊ฐ’>

-๊ฐ ์•ŒํŒŒ๋ฒณ ๋ฌธ์ž๋Š” ์‹ค์ œ ๋ฉ”๋ชจ๋ฆฌ์— ์•„์Šคํ‚ค ์ฝ”๋“œ ๊ฐ’์œผ๋กœ ์ €์žฅ๋˜๊ธฐ ๋•Œ๋ฌธ์— ch ๊ฐ’์— 1์„ ๋”ํ•˜๋ฉด(ch++) 1๋งŒํผ ์ฆ๊ฐ€ํ•œ ๊ฐ’์ด ๋ฐฐ์—ด์— ์ €์žฅ๋œ๋‹ค.

 

 

 

โ–ถ ๊ฐ์ฒด ๋ฐฐ์—ด ์‚ฌ์šฉํ•˜๊ธฐ

์ฐธ์กฐ ์ž๋ฃŒํ˜•์œผ๋กœ ์„ ์–ธํ•˜๋Š” ๊ฐ์ฒด ๋ฐฐ์—ด.

๋™์ผํ•œ ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•(int ๋“ฑ) ๋ณ€์ˆ˜ ์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ๋ฐฐ์—ด๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋“ฏ์ด ์ฐธ์กฐ ์ž๋ฃŒํ˜• ๋ณ€์ˆ˜๋„ ์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ๋ฐฐ์—ด๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

package array;

public class Book {

	private String bookName;
	private String author;

	public Book() {  // ๋””ํดํŠธ ์ƒ์„ฑ์ž
	}

	public Book(String bookName, String author) {  // ์ฑ… ์ด๋ฆ„๊ณผ ์ €์ž ์ด๋ฆ„์„ ๋งค๊ฐœ ๋ณ€์ˆ˜๋กœ ๋ฐ›๋Š” ์ƒ์„ฑ์ž
		this.bookName = bookName;
		this.author = author;
	}

	public String getBookName() {
		return bookName;
	}

	public void setBookName(String bookName) {
		this.bookName = bookName;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public void showBookInfo() {
		System.out.println(bookName + "," + author);
	}
}

 

package array;

public class BookArray {
	public static void main(String[] args) {
		Book[] library = new Book[5];  // Book ํด๋ž˜์Šคํ˜•์œผ๋กœ ๊ฐ์ฒด ๋ฐฐ์—ด ์ƒ์„ฑ

		for (int i = 0; i < library.length; i++) {
			System.out.println(library[i]);
		}
	}
}

<๊ฒฐ๊ณผ๊ฐ’>

โ€‹

 

package array;

public class BookArray2 {
	public static void main(String[] args) {
		Book[] library = new Book[5];

        // ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ ํ›„ ๋ฐฐ์—ด์— ์ €์žฅ
		library[0] = new Book("ํƒœ๋ฐฑ์‚ฐ๋งฅ", "์กฐ์ •๋ž˜");
		library[1] = new Book("๋ฐ๋ฏธ์•ˆ", "ํ—ค๋ฅด๋งŒ ํ—ค์„ธ");
		library[2] = new Book("์–ด๋–ป๊ฒŒ ์‚ด ๊ฒƒ์ธ๊ฐ€", "์œ ์‹œ๋ฏผ");
		library[3] = new Book("ํ† ์ง€", "๋ฐ•๊ฒฝ๋ฆฌ");
		library[4] = new Book("์–ด๋ฆฐ์™•์ž", "์ƒํ…์ฅํŽ˜๋ฆฌ");

		for (int i = 0; i < library.length; i++) {
			library[i].showBookInfo();
		}

		for (int i = 0; i < library.length; i++) {
			System.out.println(library[i]);
		}
	}
}

<๊ฒฐ๊ณผ๊ฐ’>

 

 

 

์‹ค์Šต) ๊ฐ์ฒด ๋ฐฐ์—ด ๋งŒ๋“ค์–ด ํ™œ์šฉํ•˜๊ธฐ

ํ•™์ƒ ํด๋ž˜์Šค Student๋ฅผ ๋งŒ๋“ค๊ณ  ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋กœ studentID, name์„ ์„ ์–ธํ•œ๋‹ค. showStudentInfo( ) ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ค์–ด studentID์™€ name ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  StudentArray ํด๋ž˜์Šค์—์„œ Student 3๋ช…์˜ ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ  Student๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์ €์žฅํ•œ ํ›„ for๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ Student ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๊ฒฐ๊ณผ๊ฐ’

 

โ€‹

<๋‚ด๊ฐ€ ํ•œ ๊ฒƒ>

Student ํด๋ž˜์Šค

 

StudentArray ํด๋ž˜์Šค

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€