






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Notes on java arrays and arraylists, including their characteristics, declaration, initialization, accessing elements, and copying arrays. It also covers the differences between arrays and arraylists, and when to use each one.
Typology: Lecture notes
1 / 11
This page cannot be seen from the preview
Don't miss anything!
ยฉcopyright 2005 ์๋ฃ๊ตฌ์กฐ ๋ฐ ์ค์ต( INA240) NOTE 02
ArrayList Vector
3/22 3 /
๋ฐฐ์ด
๋์ง ๊ตฌ์กฐ ๋์ง๊ตฌ์กฐ(homogeneous structure): ๊ตฌ์กฐ์ ์๋ ๋ชจ๋ ์์๋ ๊ฐ์ ํ์ ์ด๋ค. ์ ์2.1)2.1) ์ผ์ฐจ์ ๋ฐฐ์ด์์ ์ฒซ ์ฌ๋กฏ์ ๋ฐฐ์ด์ ์ ์ฅ๋์ด ์๋ ์์๋ค์ ๊ฐ์๋ฅผ, ๋๋จธ์ง ์ฌ๋กฏ์ ์ ์๊ฐ์ ์ ์ฅํ ๋ฐฐ์ด ๋ฌผ๋ฆฌ์ ์ผ๋ก ๋์ง ๊ตฌ์กฐ์ด์ง๋ง ๋ ผ๋ฆฌ์ ์ผ๋ก๋ ๋์ง ๊ตฌ์กฐ๊ฐ ์๋ ์์๋ค๊ฐ์ ์์๊ฐ ์์๋ค๊ฐ์์์๊ฐ ์กด์ฌ์กด์ฌํ๋ค. ๋ฐฐ์ด์ ์์๋ ์์น์ ์ํด ์ ๊ทผ๋๋ค. (index: 0๋ถํฐ ์์) ๋ฐฐ์ด์ ์ฉ๋์ ๋ฐฐ์ด์์ฉ๋์ ์ปดํ์ผ์ปดํ์ผ ์๊ฐ์์๊ฐ์ ์ ํด์ง๋ค์ ํด์ง๋ค. ๋ฐฐ์ด์ ๋ชจ๋ ์ฌ๋กฏ์ ์ ํจํ ์์๊ฐ ๋ค์ด์์ ํ์๋ ์๋ค. ๋ฐฐ์ด์ ์ฉ๋์ ๋ณ๊ฒฝํ ์ ์๋ค. ์์ ์ ๊ทผ ์์์ ๊ทผ ์ ๊ณต์ ๊ณต: ๋ชจ๋ ์์๋ฅผ ๋ฐ๋ก ์ ๊ทผํ ์ ์๋ค. ์ฉ๋์ด ๊ณ ์ ๋์ด ์๊ธฐ ๋๋ฌธ์ ์์ ์ ๊ทผ์ด ๊ฐ๋ฅํ๋ค.
z z z ์ฉ๋ ํฌ๊ธฐ: 10: 3
์ผ์ฐจ์ ๋ฐฐ์ด
์ ์2.2)2.2) int[] numbers; // reference type C/C++์ฒ๋ผ int numbers[];์ ๊ฐ์ด ์ ์ธ๋ ์๋ ์๋ค. ๋ค๋ฅธ ์ฐธ์กฐ ํ์ ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก new๋ฅผ ์ด์ฉํ์ฌ ์์ฑ๋์ด์ผ ํ๋ค. ์์2.3) numbers = new int[10];2.3) C/C++์ฒ๋ผ int numbers[10];๊ณผ ๊ฐ์ด new๋ฅผ ์ฌ์ฉํ์ง ์๊ณ , ์ ์ธ๊ณผ ๋์์ ์์ฑํ๋ ๊ฒ์ ๊ฐ๋ฅํ์ง ์๋ค. ์ ์ธ๊ณผ ์์ฑ์ ๋์์ ํ ์ ์๋ค. ์์2.4) int[] numbers = new int[10];2.4) new๋ฅผ ์ด์ฉํ์ฌ ์์ฑ๋ ๋ฐฐ์ด์ ์ด๊ธฐ๊ฐ์? ๊ธฐ๋ณธ ๊ฐ์ผ๋ก ์๋์ผ๋ก ์ด๊ธฐํ๋๋ค.
7/22 7 /
๋ฌธ์ ๋ฐฐ์ด๊ณผ ๋ฌธ์์ด
๋ฌธ์ ๋ฐฐ์ด์ String ํ์ ์ ๊ฐ์ฒด๊ฐ ์๋๋ค. ์ ์2.10)2.10)
void f1(String s){ โฆ } void f2(){ char[] fruit = {โaโ, โpโ, โpโ, โlโ, โeโ}; String s = fruit; // error f1(fruit); // error }
char[] fruit = {โaโ, โpโ, โpโ, โlโ, โeโ}; String s1 = new String(fruit); // ok String s2 = new String(fruit, 2, 3); // ok โpleโ
์ด ์ฒ๋ผ String๋ new ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ์ฌ ์์ฑํ ์ ์์ง๋ง ๋ณดํต new๋ฅผ ์๋ตํ๋ค. ๋ฌผ๋ก ์ด ๊ฒฝ์ฐ์๋ ์๋ตํ ์ ์๋ค.
char[] fruit = {โaโ, โpโ, โpโ, โlโ, โeโ}; System.out.println(fruit); ๊ฒฐ๊ณผ: apple
๋ฐฐ์ด์ ๋ณต์ฌ
์ ์2.13)2.13) int[] numbers = {1, 2, 3, 4}; int[] values = numbers; values๋ numbers์ ๊ฐ์ ๋ฐฐ์ด์ ์ฐธ์กฐํ๊ฒ ๋๋ค. ํ ๋ฐฐ์ด์ ์๋ ๋ชจ๋ ๊ฐ์ ๋ค๋ฅธ ๋ฐฐ์ด๋ก ๋ณต์ฌํ๊ณ ์ถ์ผ๋ฉด System.arraycopy ๋ฉ์๋๋ฅผ ์ด์ฉํ๋ค. ์ ์2.14)2.14) int[] numbers = {1,2,3,4,5}; int[] values = {11,12,13,14,15}; System.arraycopy(numbers,0,values,2,3);
1 2 3 4
numbers
values
1 2 3 4
numbers
5
11 12 13 14
values
15
1 2 3 4
numbers
5
11 12 1 2
values
3 System.arraycopy(์์ค๋ฐฐ์ด, ์์ค๋ฐฐ์ด์ ์์์์น, ๋ชฉ์ ๋ฐฐ์ด, ๋ชฉ์ ๋ฐฐ์ด์ ์์์์น, ๋ณต์ฌํ ์์์ ๊ฐ์)
9/22 9 /
ํ๋ผ๋ฏธํฐ๋ก ๋ฐฐ์ด์ ์ ๋ฌ
ํ์ง๋ง ์ด ์ญ์ call-by-value ํํ๋ก ์ ๋ฌ๋๋ ๊ฒ์ด๋ค. ์ ์2.16)2.16)
void f1(int[] a){ a[2] = 10; } void f2(){ int[] A = {1,2,3,4}; f1(A); for(int i=0; i<4; i++) System.out.println(A[i]); }
๊ฒฐ๊ณผ: 1,2,10,
void f1(int[] a){ int[] B = {5,6,7,8}; a = B; } void f2(){ int[] A = {1,2,3,4}; f1(A); for(int i=0; i<4; i++) System.out.println(A[i]); }
๊ฒฐ๊ณผ: 1,2,3,
Call-by-value์ ์ฐธ์กฐ ๋ณ์
public class A{ private int value; public void set(int n) { value = n; } public int get() { return value; } } // class A public class B{ public static void f(A b){ A c = new A(); b.set(10); b = c; } // f public static void main(String[] args){ A a = new A(); a.set(5); f(a); System.out.println(a.get()); } // main } // class B
a v
value
b
c
v
value
105
๊ฒฐ๊ณผ: 10
13 13/22/
final๊ณผ ๋ฐฐ์ด
final int[] numbers = new int[5]์์ final์ numbers์ ์ฐ๊ด์ด ์๋ ๊ฒ์ด์ง ๋ฐฐ์ด ์์ฒด์ ์๋ ๊ฒ์ ์๋๋ค.
void f(){ final int[] numbers = new int[5]; numbers[3] = 4; // ok numbers = new int[3]; // error }
์ด ๋ถ๋ถ๋ง final
๋ค์ฐจ์ ๋ฐฐ์ด
์ ์2.20)2.20) double[][] alpha = new double[100][10]; ์ฒซ ๋ฒ์งธ [100]์ ํ์ ๋ ๋ฒ์งธ [10]์ ์ด์ ๋ํ๋ธ๋ค. 2 ์ฐจ์ ๋ฐฐ์ด์ ์ ๊ทผ alpha[0][5] = 36.4; ํ๊ณผ ์ด์ ๊ฐ์ ํ์ ๊ฐ์: alpha.length ์ด์ ๊ฐ์: alpha[i].length
15 15/22/
๋ค์ฐจ์ ๋ฐฐ์ด โ ๊ณ์
์ ์2.21)2.21) int[][] a = new int[10][5]; ๊ทธ๋ฌ๋ ๊ฐ ํ์ ์ฉ๋์ด ๋ค๋ฅผ ์๋ ์๋ค. ์์2.22)2.22)
int[][] a = new int[2][]; a[0] = new int[5]; a[1] = new int[3];
ArrayList
ArrayList๋ java.util ํจํค์ง์ ์ ์๋์ด ์๋ ํด๋์ค๋ก ๋ฐฐ์ด๊ณผ ์ ์ฌํ์ง๋ง ๋ฐฐ์ด๊ณผ ๋ฌ๋ฆฌ ์ฉ๋์ด์ฉ๋์ด ๊ณ ์ ๋์ด๊ณ ์ ๋์ด ์์ง์์ง ์๋ค์๋ค. ์ฆ, ํ์์ ๋ฐ๋ผ ์ฉ๋์ด ๋ณํ๋ค. ArrayList๋ size() ๋ฉ์๋๋ฅผ ์ด์ฉํ์ฌ ํ์ฌ ์ ์ฅ๋์ด ์๋ ์์์ ๊ฐ์๋ฅผ ์ป์ ์ ์๋ค. ๋ฐฐ์ด์ ํน์ ํ์ ์ ์ ์ฅํ๋๋ก ์ ์ธํ ์ ์์ง๋ง, ArrayList๋ Object ํ์ ๋ง ์ ์ฅํ ์ ์๋ค. (์ฐธ์กฐ๋ฅผ ์ ์ฅํ๋ค.) ๋ฐ๋ผ์ ์์ ํ์ ์ wrapper ํด๋์ค๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค. ์ด๋ก ์ ์ผ๋ก ์๋ก ๋ค๋ฅธ ์ข ๋ฅ์ ๊ฐ์ ArrayList์ ์์๋ก ์ฌ์ฉ ๊ฐ๋ฅ
ArrayList a = new ArrayList(); a.add(new Integer(10)); a.add(new Double(2.5));
int[] a = new int[10]; a[0] = 3; a[1] = 2.5; // error
๋ฐ๋์งํ ํ๋ก๊ทธ๋๋ฐ ์คํ์ผ์ ์๋๋ค.
19 19/22/
ArrayList โ ๊ณ์
add/remove ๋ฉ์๋ ์ฌ์ฉ์ ์ฃผ์์ add๋ ์ต์ ์ ๊ฒฝ์ฐ ํ์ฌ ์ ์ฅ๋์ด ์๋ ๋ชจ๋ ์์๋ฅผ ํ๋์ฉ ์ค๋ฅธ์ชฝ์ผ๋ก ์ด๋ํด์ผ ํ๋ค. ๋ฐ๋๋ก remove๋ ์ต์ ์ ๊ฒฝ์ฐ ํ์ฌ ์ ์ฅ๋์ด ์๋ ๋ชจ๋ ์์๋ฅผ ํ๋์ฉ ์ผ์ชฝ์ผ๋ก ์ด๋ํด์ผ ํ๋ค. ํ์ฌ ํฌ๊ธฐ๊ฐ ์ฉ๋๊ณผ ๊ฐ์ ๊ฒฝ์ฐ์๋ ํ์ฌ๋ณด๋ค ํฐ ์ฉ๋์ ๋ฐฐ์ด์ ์์ฑํ ๋ค์์ ๊ธฐ์กด ๋ฐฐ์ด์์ ์์๋ค์ ๋ณต์ฌํ๋ค. get ๋ฉ์๋ ์ฌ์ฉ์ ์ฃผ์์ ๋ฐํํ์ ์ด Object์ด๋ฏ๋ก ๋ฐํ๊ฐ์ ์ํ๋ ํ์ ์ผ๋ก ๊ฐ์ ๋ณํ ํด์ฃผ์ด์ผ ํ๋ค. ArrayList์ ์์ฑ์ ArrayList() : ์ฉ๋์ด 10 ์ธ ๋น ๋ฆฌ์คํธ๋ฅผ ์์ฑํ๋ค. ArrayList(int capacity): ์ฃผ์ด์ง ์ธ์์ ํด๋นํ๋ ์ฉ๋์ ๋น ๋ฆฌ์คํธ๋ฅผ ์์ฑํ๋ค.
๋ฐ๋ณต์
ArrayList๋ ๋ฐ๋ณต์(iterator)๋ฅผ ๊ฐ์ง๊ณ ์๋ค. ๋ฐ๋ณต์๋ ๊ตฌ์ฑ์์๋ฅผ ์ฐจ๋ก๋ก ๋ฐฉ๋ฌธํ ๋ ์ฌ์ฉํ๋ ๋๊ตฌ๋ฅผ ๋งํ๋ค. ๋ฐ๋ณต์๋ ๊ธฐ๋ณธ์ ์ผ๋ก ๋ค์ ๋ ์ฐ์ฐ์ ์ ๊ณตํ๋ค. boolean hasNext(): ๋ ์ด์์ ์์๊ฐ ์๋์ง ํ์ธํ ๋ ์ฌ์ฉ Object next(): ๋ค์ ์์๋ฅผ ์ป๊ธฐ ์ํด ์ฌ์ฉ ์์2.23)2.23) (^) void printVector(ArrayList list){ Iterator i = list.iterator(); while(i.hasNext()){ System.out.println(i.next()); } }
21 21/22/
๋จ์ ๋ฐฐ์ด vs. ArrayList
ArrayList ๊ณต๊ฐ ๋ฌธ์ ๊ฐ ์ค์ํ ๊ฒฝ์ฐ ์คํ์๊ฐ์ด ์ค์ํ์ง ์๋ ๊ฒฝ์ฐ ์๊ตฌ๋๋ ๋ฐฐ์ด์ ์ฉ๋์ด ํ๋ก๊ทธ๋จ ์คํ๋ง๋ค ํฌ๊ฒ ๋ณํ๋ ๊ฒฝ์ฐ ๋ฐฐ์ด์ ํฌ๊ธฐ๊ฐ ํ๋ก๊ทธ๋จ์ด ์คํ๋๋ ๋์ ๊ทน๋จ์ ์ผ๋ก ๋ณํ๋ ๊ฒฝ์ฐ ๋ฐฐ์ด์ ์๋ ์์์ ์์น๊ฐ ์ค์ํ์ง ์๋ ๊ฒฝ์ฐ ์ญ์ /์ฝ์ ์ด ๋๋ถ๋ถ ๋์ ์ด๋ฃจ์ด์ง๋ ๊ฒฝ์ฐ
Vector
java.util ํจํค์ง์ ํฌํจ๋์ด ์์ผ๋ฉฐ, ArrayList์ ๊ทธ ๊ธฐ๋ฅ์ด ๋งค์ฐ ์ ์ฌํ๋ค. ๊ฐ์ฅ ์ค์ํ ์ฐจ์ด์ ์ Vector๋ ๋ค์ค์ฐ๋ ๋๋ฅผ ์ง์ํ๊ณ ArrayList๋ ์ง์ํ์ง ์๋๋ค. ๋ฐ๋ผ์ ๋ค์ค์ฐ๋ ๋ ํ๊ฒฝ์ด ์๋๋ฉด Vector ๋์ ์ ArrayList๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ๋ฐ๋์งํ๋ค. ArrayList์ ๋ฌ๋ฆฌ capacity() ๋ฉ์๋๋ฅผ ์ด์ฉํ์ฌ ํ์ฌ ์ฉ๋์ ์ป์ ์ ์๋ค. Vector์ ๋ ์ด์ ์์๋ฅผ ์ฝ์ ํ ๊ณต๊ฐ์ด ์์ผ๋ฉด ์ง์ ํด ๋์ ์ฉ๋๋งํผ ๋๋ ๊ธฐ๋ณธ ์ฆ๊ฐ ์ฉ๋๋งํผ ์๋์ผ๋ก ์ฆ๊ฐํ๋ค. ๊ธฐ๋ณธ ์ฆ๊ฐ ์ฉ๋์ ํ์ฌ ์ฉ๋์ ๋ ๋ฐฐ์ด๋ค. Vector() Vector(int capacity) ์ฆ๊ฐํ๋ ์ฉ๋์ ์ง์ ํ๊ณ ์ถ์ผ๋ฉด ๋ฒกํฐ๋ฅผ ์์ฑํ ๋ ํด์ผ ํ๋ค. Vector(int capacity, int increment)