7. ๋ฐฐ์ด๊ณผ ArrayList
๋ฐฐ์ด์ด๋
๋ฐฐ์ด์ ์๋ฃํ์ด ๊ฐ์ ์๋ฃ ์ฌ๋ฌ ๊ฐ๋ฅผ ํ ๋ฒ์ ๊ด๋ฆฌํ ์ ์๋ ์๋ฃ ๊ตฌ์กฐ๋ก, ์๋ฃ๊ฐ ์ฐ์์ผ๋ก ๋์ด๋ ํํ์ด๋ค.
์๋ฃํ[] ๋ฐฐ์ด์ด๋ฆ = new ์๋ฃํ[๊ฐ์];
์๋ฃํ ๋ฐฐ์ด์ด๋ฆ[] = new ์๋ฃํ[๊ฐ์];
ex) intํ ์์๊ฐ 10๊ฐ์ธ ๋ฐฐ์ด ์ ์ธํ๋ฉด, intํ์ ์ ์ฅํ ์ ์๋ ํฌ๊ธฐ๋ 4๋ฐ์ดํธ์ด๋ฏ๋ก ์ด ๋ฐฐ์ด์ ์ด 40๋ฐ์ดํธ์ ๋ฉ๋ชจ๋ฆฌ๊ฐ ํ ๋น๋จ
int[] studentIDs = new int[10]; // intํ ์์๊ฐ 10๊ฐ์ธ ๋ฐฐ์ด ์ ์ธ

๋ฐฐ์ด ์ ์ธ๊ณผ ์ด๊ธฐํ
์๋ฐ์์ ๋ฐฐ์ด ์ ์ธํ๋ฉด ๊ทธ์ ๋์์ ๊ฐ ์์์ ๊ฐ์ด ์ด๊ธฐํ๋จ. ๋ฐฐ์ด์ ์๋ฃํ์ ๋ฐ๋ผ ์ ์๋ 0, ์ค์๋ 0.0, ๊ฐ์ฒด ๋ฐฐ์ด์ null๋ก ์ด๊ธฐํ๋จ
๋ํ, ๋ฐฐ์ด ์ ์ธ๊ณผ ๋์์ ํน์ ๊ฐ์ผ๋ก ์ด๊ธฐํํ ์๋ ์์. ๋ฐฐ์ด์ด ์ด๊ธฐํ ์์์ ๊ฐ์๋งํผ ์์ฑ๋๋ฏ๋ก [] ์์ ๊ฐ์๋ ์๋ตํจ
int[] studentIDs = new int[] {101, 102, 103}; // ๊ฐ์ ์๋ต
int[] studentIDs = {101, 102, 103};
๋ฐฐ์ด ์ฌ์ฉํ๊ธฐ
์ธ๋ฑ์ค ์ฐ์ฐ์ []๋ ๋ฐฐ์ด ์์๊ฐ ์ ์ฅ๋ ๋ฉ๋ชจ๋ฆฌ ์์น๋ฅผ ์ฐพ์์ฃผ๋ ์ญํ ์ ํจ. ๋ฐฐ์ด์์ [i] ์ธ๋ฑ์ค ์ฐ์ฐ์ ์ํํ๋ฉด i๋ฒ์งธ ์์์ ์์น๋ฅผ ์ฐพ์ ํด๋น ์์น์ ๋ฉ๋ชจ๋ฆฌ์ ๊ฐ์ ๋ฃ๊ฑฐ๋ ์ด๋ฏธ ์ ์ฅ๋์ด ์๋ ๊ฐ์ ๊ฐ์ ธ์์ ์ฌ์ฉํ ์ ์์
- ๋ฐฐ์ด ๊ธธ์ด๊ฐ n์ด๋ผ๊ณ ํ๋ฉด, ๋ฐฐ์ด ์์๋ 0๋ฒ๋ถํฐ n-1๋ฒ๊น์ง์
- length ์์ฑ: ๋ฐฐ์ด ๊ธธ์ด๋ฅผ ๋ํ๋. ์ฆ, ๋ฐฐ์ด์ ์ ์ฒด ์์ ๊ฐ์ ์๋ฏธ
public class example {
public static void main(String[] args) {
double[] data = new 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]);
}
}
}
/*
10.0
20.0
30.0
0.0
0.0
* */
๋ฌธ์ ์ ์ฅ ๋ฐฐ์ด ๋ง๋ค๊ธฐ
๋ฌธ์ ์๋ฃํ ๋ฐฐ์ด์ ๋ง๋ค๊ณ ์ํ๋ฒณ ๋๋ฌธ์๋ฅผ A~Z๊น์ง ์ ์ฅํ ํ ๊ฐ ์์ ๊ฐ์ ์ํ๋ฒณ ๋ฌธ์์ ์ ์ ๊ฐ(์์คํค์ฝ๋ ๊ฐ)์ผ๋ก ์ถ๋ ฅํ๊ธฐ
public class example {
public static void main(String[] args) {
char[] alphabets = new char[26];
char ch = 'A';
for(int i=0; i<alphabets.length; i++, ch++) {
alphabets[i] = ch;
}
for(int i=0; i<alphabets.length;i++){
System.out.println(alphabets[i]+","+(int) alphabets[i]);
}
}
}
๊ฐ์ฒด ๋ฐฐ์ด ์ฌ์ฉํ๊ธฐ
์ฐธ์กฐ ์๋ฃํ ๋ณ์๋ ์ฌ๋ฌ ๊ฐ๋ฅผ ๋ฐฐ์ด๋ก ์ฌ์ฉํ ์ ์์
ex) Book ํด๋์ค ์์ฑ ํ ์ฑ 5๊ถ์ ๊ฐ์ฒด ๋ฐฐ์ด๋ก ๋ง๋ค๊ธฐ
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]);
}
}
}
/*
null
null
null
null
null
* */
์ ์ฝ๋๋ Book ์ฃผ์ ๊ฐ์ ๋ด์ ๊ณต๊ฐ์ด 5๊ฐ ๋ง๋ค์ด์ง๊ณ ์๋์ผ๋ก ๊ฐ ๊ณต๊ฐ์ '๋น์ด์๋ค'๋ ์๋ฏธ์ null ๊ฐ์ผ๋ก ์ด๊ธฐํ๋จ

๊ฐ ๋ฐฐ์ด ์์์ ์ธ์คํด์ค๋ฅผ ์์ฑํด ๋ฃ๊ธฐ ์ํด Book ํด๋์ค์์ ๊ตฌํํ ์์ฑ์ ์ฌ์ฉ
package array;
public class BookArray {
public static void main(String[] args){
Book[] library = new Book[5]; // Book ํด๋์คํ์ผ๋ก ๊ฐ์ฒด ๋ฐฐ์ด ์์ฑ
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]);
}
}
}
/*
ํ๋ฐฑ์ฐ๋งฅ,์กฐ์ ๋
๋ฐ๋ฏธ์,ํค๋ฅด๋ง ํค์ธ
์ด๋ป๊ฒ ์ด ๊ฒ์ธ๊ฐ,์ ์๋ฏผ
ํ ์ง,๋ฐ๊ฒฝ๋ฆฌ
์ด๋ฆฐ์์,์ํ
์ฅํ๋ฆฌ
array.Book@a09ee92
array.Book@30f39991
array.Book@452b3a41
array.Book@4a574795
array.Book@f6f4d33
* */
๋ฐฐ์ด ๋ณต์ฌํ๊ธฐ
๊ธฐ์กด ๋ฐฐ์ด๊ณผ ์๋ฃํ ๋ฐ ๋ฐฐ์ด ํฌ๊ธฐ๊ฐ ๋๊ฐ์ ๋ฐฐ์ด์ ์๋ก ๋ง๋ค๊ฑฐ๋ ๋ฐฐ์ด์ ๋ชจ๋ ์์์ ์๋ฃ๊ฐ ๊ฝ ์ฐจ์ ๋ ํฐ ๋ฐฐ์ด์ ๋ง๋ค์ด ๊ธฐ์กด ๋ฐฐ์ด์ ์ ์ฅ๋ ์๋ฃ๋ฅผ ๊ฐ์ ธ์ค๋ ค ํ ๋ ๋ฐฐ์ด์ ๋ณต์ฌํจ
[๋ฐฐ์ด ๋ณต์ฌ ๋ฐฉ๋ฒ]
1. ๊ธฐ์กด ๋ฐฐ์ด๊ณผ ๋ฐฐ์ด ๊ธธ์ด๊ฐ ๊ฐ๊ฑฐ๋ ๋ ๊ธด ๋ฐฐ์ด์ ๋ง๋ค๊ณ for๋ฌธ์ ์ฌ์ฉํด ๊ฐ ์์ ๊ฐ์ ๋ฐ๋ณตํด์ ๋ณต์ฌํ๋ ๋ฐฉ๋ฒ
2. System.arraycopy(src, srcPos, dest, destPos, length) ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ
| ๋งค๊ฐ๋ณ์ | ์ค๋ช |
| src | ๋ณต์ฌํ ๋ฐฐ์ด ์ด๋ฆ |
| srcPos | ๋ณต์ฌํ ๋ฐฐ์ด์ ์ฒซ ๋ฒ์งธ ์์น |
| dest | ๋ณต์ฌํด์ ๋ถ์ฌ ๋ฃ์ ๋์ ๋ฐฐ์ด ์ด๋ฆ |
| destPos | ๋ณต์ฌํด์ ๋์ ๋ฐฐ์ด์ ๋ถ์ฌ ๋ฃ๊ธฐ๋ฅผ ์์ํ ์ฒซ ๋ฒ์งธ ์์น |
| length | src์์ dest๋ก ์๋ฃ๋ฅผ ๋ณต์ฌํ ์์ ๊ฐ์ |
ex) System.arraycopy()๋ก ๋ฐฐ์ด ๋ณต์ฌํ๊ธฐ
package array;
public class ArrayCopy {
public static void main(String[] args){
int[] array1 = {10, 20, 30, 40, 50};
int[] array2 = {1,2,3,4,5};
System.arraycopy(array1, 0, array2, 1, 4);
for(int i=0; i<array2.length; i++){
System.out.println(array2[i]);
}
}
}
/*
1
10
20
30
40
* */
๊ฐ์ฒด ๋ฐฐ์ด ๋ณต์ฌํ๊ธฐ
package array;
public class ObjectCopy1 {
public static void main(String[] args) {
Book[] bookArray1 = new Book[3];
Book[] bookArray2 = new Book[3];
bookArray1[0] = new Book("ํ๋ฐฑ์ฐ๋งฅ", "์กฐ์ ๋");
bookArray1[1] = new Book("๋ฐ๋ฏธ์", "ํค๋ฅด๋ง ํค์ธ");
bookArray1[2] = new Book("์ด๋ป๊ฒ ์ด ๊ฒ์ธ๊ฐ", "์ ์๋ฏผ");
System.arraycopy(bookArray1, 0, bookArray2, 0, 3);
for(int i=0; i<bookArray2.length; i++){
bookArray2[i].showBookInfo();
}
}
}
/*
ํ๋ฐฑ์ฐ๋งฅ,์กฐ์ ๋
๋ฐ๋ฏธ์,ํค๋ฅด๋ง ํค์ธ
์ด๋ป๊ฒ ์ด ๊ฒ์ธ๊ฐ,์ ์๋ฏผ
* */
์ ์ฝ๋์์ bookArray2 ๋ฐฐ์ด์ ์ธ์คํด์ค๋ฅผ ๋ฐ๋ก ๋ง๋ค์ง ์์๋ ๊ฐ ์์ ๊ฐ์ด ์ ์ถ๋ ฅ๋๋ ์ด์ ๋ ์ธ์คํด์ค๋ฅผ ๋ฐ๋ก ์์ฑํ๋ ๊ฒ ์๋๋ผ ๊ธฐ์กด ์ธ์คํด์ค์ ์ฃผ์ ๊ฐ๋ง ๋ณต์ฌํ๋ ์์ ๋ณต์ฌ(shallow copy)๊ฐ ์ด๋ฃจ์ด์ง๊ธฐ ๋๋ฌธ
1) ์์ ๋ณต์ฌ(shallow copy)
๊ฐ์ฒด ๋ฐฐ์ด์ ๋ณต์ฌํ ๋ ์ธ์คํด์ค๋ฅผ ๋ฐ๋ก ์์ฑํ๋ ๊ฒ ์๋๋ผ ๊ธฐ์กด ์ธ์คํด์ค์ ์ฃผ์ ๊ฐ๋ง ๋ณต์ฌํ๋ ๊ฒ์ผ๋ก, ๊ฒฐ๊ตญ ๋ ๋ฐฐ์ด์ ์๋ก ๋ค๋ฅธ ์์๊ฐ ๊ฐ์ ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํด
๋ฐ๋ผ์, ๋ณต์ฌ๋๋ ๋ฐฐ์ด์ ์ธ์คํด์ค ๊ฐ์ด ๋ณ๊ฒฝ๋๋ฉด ๋ ๋ฐฐ์ด ๋ชจ๋ ์ํฅ์ ๋ฐ๊ฒ ๋จ
package array;
public class ObjectCopy1 {
public static void main(String[] args) {
Book[] bookArray1 = new Book[3];
Book[] bookArray2 = new Book[3];
bookArray1[0] = new Book("ํ๋ฐฑ์ฐ๋งฅ", "์กฐ์ ๋");
bookArray1[1] = new Book("๋ฐ๋ฏธ์", "ํค๋ฅด๋ง ํค์ธ");
bookArray1[2] = new Book("์ด๋ป๊ฒ ์ด ๊ฒ์ธ๊ฐ", "์ ์๋ฏผ");
System.arraycopy(bookArray1, 0, bookArray2, 0, 3);
for(int i=0; i<bookArray2.length; i++){
bookArray2[i].showBookInfo();
}
// bookArray1 ๋ฐฐ์ด์ ์ฒซ ๋ฒ์ฌ ์์ ๊ฐ ๋ณ๊ฒฝ
bookArray1[0].setBookName("๋๋ชฉ");
bookArray1[0].setAuthor("๋ฐ์์");
System.out.println("=== bookArray1 ===");
for(int i=0; i<bookArray1.length; i++){
bookArray1[i].showBookInfo();
}
System.out.println("=== bookArray2 ===");
for(int i=0; i<bookArray2.length; i++){
bookArray2[i].showBookInfo(); // bookArray2 ๋ฐฐ์ด ์์ ๊ฐ๋ ๋ณ๊ฒฝ๋์ด ์ถ๋ ฅ
}
}
}
/*
ํ๋ฐฑ์ฐ๋งฅ,์กฐ์ ๋
๋ฐ๋ฏธ์,ํค๋ฅด๋ง ํค์ธ
์ด๋ป๊ฒ ์ด ๊ฒ์ธ๊ฐ,์ ์๋ฏผ
=== bookArray1 ===
๋๋ชฉ,๋ฐ์์
๋ฐ๋ฏธ์,ํค๋ฅด๋ง ํค์ธ
์ด๋ป๊ฒ ์ด ๊ฒ์ธ๊ฐ,์ ์๋ฏผ
=== bookArray2 ===
๋๋ชฉ,๋ฐ์์
๋ฐ๋ฏธ์,ํค๋ฅด๋ง ํค์ธ
์ด๋ป๊ฒ ์ด ๊ฒ์ธ๊ฐ,์ ์๋ฏผ
* */
2) ๊น์ ๋ณต์ฌ(deep copy)
๋ฐ๋ณต๋ฌธ์ ์ฌ์ฉํ๊ฑด System.arraycopy() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๊ฑด ๊ฐ์ฒด ๋ฐฐ์ด์ ๋ณต์ฌํ๋ฉด ํญ์ ์ธ์คํด์ค ์ฃผ์๊ฐ ๋ณต์ฌ๋จ. ์ธ์คํด์ค๋ฅผ ๋ฐ๋ก ๊ด๋ฆฌํ๊ณ ์ถ๋ค๋ฉด ์ง์ ์ธ์คํด์ค๋ฅผ ๋ง๋ค๊ณ ๊ทธ ๊ฐ์ ๋ณต์ฌํ๋ ๊น์ ๋ณต์ฌ๋ฅผ ์ํํด์ผ ํจ
package array;
public class ObjectCopy1 {
public static void main(String[] args) {
Book[] bookArray1 = new Book[3];
Book[] bookArray2 = new Book[3];
bookArray1[0] = new Book("ํ๋ฐฑ์ฐ๋งฅ", "์กฐ์ ๋");
bookArray1[1] = new Book("๋ฐ๋ฏธ์", "ํค๋ฅด๋ง ํค์ธ");
bookArray1[2] = new Book("์ด๋ป๊ฒ ์ด ๊ฒ์ธ๊ฐ", "์ ์๋ฏผ");
// ๋ํดํธ ์์ฑ์๋ก bookArray2 ๋ฐฐ์ด ์ธ์คํด์ค ์์ฑ
bookArray2[0] = new Book();
bookArray2[1] = new Book();
bookArray2[2] = new Book();
// bookArray1 ๋ฐฐ์ด ์์๋ฅผ ์๋ก ์์ฑํ bookArray2 ๋ฐฐ์ด ์ธ์คํด์ค์ ๋ณต์ฌ
for(int i=0; i<bookArray1.length; i++){
bookArray2[i].setBookName(bookArray1[i].getBookName());
bookArray2[i].setAuthor(bookArray1[i].getAuthor());
}
for(int i=0; i<bookArray2.length; i++) {
bookArray2[i].showBookInfo();
}
// bookArray1 ์ฒซ ๋ฒ์งธ ๋ฐฐ์ด ์์ ๊ฐ ์์
bookArray1[0].setBookName("๋๋ชฉ");
bookArray1[0].setAuthor("๋ฐ์์");
System.out.println("=== bookArray1 ===");
for(int i=0; i<bookArray1.length; i++){
bookArray1[i].showBookInfo();
}
System.out.println("=== bookArray2 ===");
for(int i=0; i<bookArray2.length; i++){
bookArray2[i].showBookInfo();
}
}
}
/*
ํ๋ฐฑ์ฐ๋งฅ,์กฐ์ ๋
๋ฐ๋ฏธ์,ํค๋ฅด๋ง ํค์ธ
์ด๋ป๊ฒ ์ด ๊ฒ์ธ๊ฐ,์ ์๋ฏผ
=== bookArray1 ===
๋๋ชฉ,๋ฐ์์
๋ฐ๋ฏธ์,ํค๋ฅด๋ง ํค์ธ
์ด๋ป๊ฒ ์ด ๊ฒ์ธ๊ฐ,์ ์๋ฏผ
=== bookArray2 ===
ํ๋ฐฑ์ฐ๋งฅ,์กฐ์ ๋
๋ฐ๋ฏธ์,ํค๋ฅด๋ง ํค์ธ
์ด๋ป๊ฒ ์ด ๊ฒ์ธ๊ฐ,์ ์๋ฏผ
* */
ํฅ์๋ for๋ฌธ๊ณผ ๋ฐฐ์ด
for(๋ณ์: ๋ฐฐ์ด) {
๋ฐ๋ณต ์คํ๋ฌธ;
}
public class example {
public static void main(String[] args) {
String[] strArray = {"Java", "Android", "C", "JavaScript", "Python"};
for(String lang:strArray) {
System.out.println(lang);
}
}
}
๋ค์ฐจ์ ๋ฐฐ์ด
์ด์ฐจ์ ์ด์์ผ๋ก ๊ตฌํํ ๋ฐฐ์ด๋ก, ํ๋ฉด์ด๋ ๊ณต๊ฐ ๊ฐ๋ ์ ๊ตฌํํ๋ ๋ฐ ์ฌ์ฉํจ
์ด์ฐจ์ ๋ฐฐ์ด
์๋ฃํ[][] ๋ฐฐ์ด์ด๋ฆ = new ์๋ฃํ[ํ๊ฐ์][์ด๊ฐ์];
int[][] arr = new int[2][3];
int[][] arr = {{1,2,3}, {4,5,6}};
public class example {
public static void main(String[] args) {
int[][] arr = {{1,2,3}, {4,5,6}};
for(int i=0; i<arr.length; i++){
for(int j=0; j<arr[i].length; j++){
System.out.println(arr[i][j]);
}
System.out.println(); // ํ ์ถ๋ ฅ ๋๋ ํ ํ ์ค ๋์;
}
}
}
ArrayList ํด๋์ค ์ฌ์ฉํ๊ธฐ
๊ธฐ์กด ๋ฐฐ์ด์ ๋จ์ ๊ณผ ArrayList
๊ธฐ๋ณธ ๋ฐฐ์ด์ ํ๋ก๊ทธ๋จ์์ ์ฌ์ฉํ๋ ค๋ฉด ํญ์ ๋ฐฐ์ด ๊ธธ์ด๋ฅผ ์ ํ๊ณ ์์ํด์ผ ํจ. ๋ฐฐ์ด์ ์ฌ์ฉํ๋ ์ค์ ๋ฐฐ์ด ๊ธธ์ด ๋ณ๊ฒฝํ ์ ์๊ณ , ์ค๊ฐ์ ๋ฐฐ์ด ์์๋ฅผ ์ญ์ ํ ์๋ ์์
์๋ฐ๋ ๊ฐ์ฒด ๋ฐฐ์ด์ ์ข ๋ ์ฝ๊ฒ ์ฌ์ฉํ ์ ์๋๋ก ๊ฐ์ฒด ๋ฐฐ์ด ํด๋์ค ArrayList๋ฅผ ์ ๊ณตํจ.
ArrayList ํด๋์ค๋ ๊ฐ์ฒด ๋ฐฐ์ด์ ๊ด๋ฆฌํ ์ ์๋ ๋ฉค๋ฒ ๋ณ์์ ๋ฉ์๋๋ฅผ ์ ๊ณต
ArrayList ํด๋์ค์ ์ฃผ์ ๋ฉ์๋
| ๋ฉ์๋ | ์ค๋ช |
| boolean add(E e) | ์์ ํ๋๋ฅผ ๋ฐฐ์ด์ ์ถ๊ฐํจ. E๋ ์์์ ์๋ฃํ์ ์๋ฏธ |
| int size() | ๋ฐฐ์ด์ ์ถ๊ฐ๋ ์์ ์ ์ฒด ๊ฐ์ ๋ฐํ |
| E get(int index) | ๋ฐฐ์ด์ index ์์น์ ์๋ ์์ ๊ฐ ๋ฐํ |
| E remove(int index) | ๋ฐฐ์ด์ index ์์น์ ์๋ ์์ ๊ฐ์ ์ ๊ฑฐํ๊ณ ๊ทธ ๊ฐ์ ๋ฐํ |
| boolean isEmpty() | ๋ฐฐ์ด์ด ๋น์ด์๋์ง ํ์ธ |
ArrayList ํด๋์ค ํ์ฉํ๊ธฐ
ArrayList<E> ๋ฐฐ์ด์ด๋ฆ = new ArrayList<E>(); // <> ์์ ์ฌ์ฉํ ๊ฐ์ฒด์ ์๋ฃํ ์ฐ๋ฉด๋จ
- ArrayList ํด๋์ค๋ java.util ํจํค์ง์ ๊ตฌํ๋์ด ์๋ ํด๋์ค๋ก, ์ฌ์ฉํ๊ธฐ ์ํด์๋ ์ปดํ์ผ๋ฌ์๊ฒ ArrayList๊ฐ ์ด๋์ ๊ตฌํ๋์ด ์๋์ง ์๋ ค์ค์ผ ํ๊ธฐ ๋๋ฌธ์ 'import java.util.ArrayList;' ๋ฌธ์ฅ์ ๋ฐ๋์ ์จ ์ฃผ์ด์ผ ํจ
package array;
import java.util.ArrayList;
public class ArrayListTest {
public static void main(String[] args) {
ArrayList<Book> library = new ArrayList<Book>();
library.add(new Book("ํ๋ฐฑ์ฐ๋งฅ", "์กฐ์ ๋"));
library.add(new Book("๋ฐ๋ฏธ์", "ํค๋ฅด๋ง ํค์ธ"));
library.add(new Book("์ด๋ป๊ฒ ์ด ๊ฒ์ธ๊ฐ", "์ ์๋ฏผ"));
library.add(new Book("ํ ์ง", "๋ฐ๊ฒฝ๋ฆฌ"));
library.add(new Book("์ด๋ฆฐ์์", "์ํ
์ฅํ๋ฆฌ"));
for(Book book : library){
book.showBookInfo();
}
System.out.println();
}
}