Java ๊ธฐ์ดˆ (19) ArrayList

์ฝ”๋ฆฐ์ด์„œํ˜„์ดยท2023๋…„ 7์›” 19์ผ
0

Java

๋ชฉ๋ก ๋ณด๊ธฐ
19/46

๐Ÿฅฒ๋“ค์–ด๊ฐ€๋ฉด์„œ๐Ÿฅฒ

๋ฐฐ์—ด์„ ์„ ์–ธํ•ด์„œ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š”๋ฐ ์„ค์ •ํ–ˆ๋˜ ๊ธธ์ด๋ณด๋‹ค ๋” ๋งŽ์€์–‘์ด ํ•„์š”ํ•  ๋•Œ ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ?
๊ทธ๋Ÿฐ ์ƒํ™ฉ์—์„œ ์œ ์šฉํ•œ Java์—์„œ ์ œ๊ณตํ•˜๋Š” ArrayList์— ๋Œ€ํ•ด ํฌ์ŠคํŒ… ํ•˜๊ฒ ๋‹ค.

๐Ÿ“• ArrayList

: java.utilํŒจํ‚ค์ง€์— ์†Œ์†๋˜์–ด ์žˆ๋Š” ํด๋ž˜์Šค์ด๋‹ค.
๋ฐฐ์—ด์„ ์–ธ์‹œ ๊ธธ์ด๋ฅผ ํ•„์ˆ˜๋กœ ์ง€์ •ํ•ด์ฃผ์–ด์•ผํ•˜๋Š”๋ฐ, ArrayList๋Š” ์œ ๋™์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๋‹ค.

๐Ÿ“’ ArrayList์„ ์–ธํ•˜๊ธฐ

	import java.util.ArrayList;	
	//ArrayList<์ž๋ฃŒํ˜•> ๋ฐฐ์—ด์ด๋ฆ„ = new ArrayList<์ž๋ฃŒํ˜•>();
    ArrayList<Integer> intArrayList = new ArrayList<Integer>();

๐Ÿ“’ ์ถ”๊ฐ€ ๊ธฐ๋Šฅ๋“ค

์ฝ”๋“œ๊ธฐ๋Šฅ
add(Object)Object๋ฅผ ๋์— ์ถ”๊ฐ€ํ•œ๋‹ค.
add(int index, Object)index์— ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.(์ œ๊ฑฐํ•˜์ง€ ์•Š๊ณ  ๋’ค๋กœ ๋ฐ€์–ด๋ƒ„)
size()๋ฐฐ์—ด์— ์ถ”๊ฐ€๋œ ์š”์†Œ ์ „์ฒด ๊ฐœ์ˆ˜ ๋ฐ˜ํ™˜
get(index)index์œ„์น˜์— ์žˆ๋Š” ์š”์†Œ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
remove(index)๋ฐฐ์—ด index์œ„์น˜์— ์žˆ๋Š” ์š”์†Œ๊ฐ’์„ ์ œ๊ฑฐํ•œ๋‹ค.
remove(Object)ํ•ด๋‹น ๊ฐ’๊ณผ ๊ฐ™์€ Object๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค.(๊ฐ™์€ ๊ฐ’์ด ๋‘๊ฐœ์ด์ƒ์ธ ๊ฒฝ์šฐ ์ฒซ๋ฒˆ์งธ์˜ ๊ฐ™์€ ๊ฐ’๋งŒ ์‚ญ์ œํ•œ๋‹ค.)
clear()๋ชจ๋“  ๊ฐ’์„ ์‚ญ์ œํ•œ๋‹ค.
set(index)๋ฐฐ์—ด index์œ„์น˜์— ์žˆ๋Š” ์š”์†Œ๊ฐ’์„ ๋ณ€๊ฒฝํ•œ๋‹ค.
indexOf(Object)๊ฐ’์˜ index๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ๊ฐ’์ด ์—†๋‹ค๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
contains(Object)๊ฐ’์ด ์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋งŒ ํŒŒ์•…ํ•œ๋‹ค.
isEmpty()๋ฐฐ์—ด์ด ์•„์˜ˆ ๋น„์–ด์ž‡๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

โœ๏ธ intํ˜• ์˜ˆ์‹œ์ฝ”๋“œ

package test;

import java.util.ArrayList;

public class ArrayListTest {

	public static void main(String[] args) {
		ArrayList<Integer> intArrayList = new ArrayList<Integer>();
		intArrayList.add(1);
		intArrayList.add(1);
		intArrayList.add(1);
		//add(int index, Object) : ArrayList์˜ index์— ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.
		intArrayList.add(2,3);	//์ œ๊ฑฐํ•˜์ง€ ์•Š๊ณ  ๋ฐ€์–ด๋ƒ„(์ถ”๊ฐ€๊ฐ€ ์šฉ์ดํ•˜๋‹ค.)
		
		System.out.println(intArrayList);			//[1, 1, 3, 1]
		//.size()๋ฉ”์†Œ๋“œ : ๋ฐฐ์—ด์— ์ถ”๊ฐ€๋œ ์š”์†Œ ์ „์ฒด ๊ฐœ์ˆ˜ ๋ฐ˜ํ™˜
		System.out.println(intArrayList.size());	//4
	
		//.get()๋ฐฐ์—ด index์œ„์น˜์— ์žˆ๋Š” ์š”์†Œ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
		System.out.println(intArrayList.get(2));	//3
		//ํ–ฅ์ƒ๋œ for๋ฌธ์œผ๋กœ ์ „์ฒด ์ถœ๋ ฅ๊ฐ€๋Šฅ
		for( int i : intArrayList) {
			System.out.print(i+" ");				//1 1 3 1 	
		}
		System.out.println();
		
		//.remove()๋ฐฐ์—ด index์œ„์น˜์— ์žˆ๋Š” ์š”์†Œ๊ฐ’์„ ์ œ๊ฑฐํ•œ๋‹ค.
		intArrayList.remove(2);						//
		System.out.println(intArrayList);			//[1, 1, 1]
		//remove(Object)์ธ ๊ฒฝ์šฐ์— ํ•ด๋‹น ๊ฐ’๊ณผ ๊ฐ™์€ Object๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค.
		//๊ฐ™์€ ๊ฐ’์ด ๋‘๊ฐœ์ด์ƒ์ธ ๊ฒฝ์šฐ ์ฒซ๋ฒˆ์งธ์˜ ๊ฐ™์€ ๊ฐ’๋งŒ ์‚ญ์ œํ•œ๋‹ค.
		intArrayList.remove(1);						//1์ด๋ž‘ Object๋ฅผ ์‚ญ์ œํ•˜๋Š”๋ฐ ์ฒซ๋ฒˆ์งธ ๊ฐ’์„ ์‚ญ์ œ
		System.out.println(intArrayList);			//[1,1]
		//.clear() : ๋ชจ๋“  ๊ฐ’์„ ์‚ญ์ œํ•œ๋‹ค.
		intArrayList.clear();
		System.out.println(intArrayList);			//[]
		
		intArrayList.add(10);
		intArrayList.add(20);
		intArrayList.add(30);
		//.set()๋ฐฐ์—ด index์œ„์น˜์— ์žˆ๋Š” ์š”์†Œ๊ฐ’์„ ๋ณ€๊ฒฝํ•œ๋‹ค.
		intArrayList.set(0,0);
		System.out.println(intArrayList);			//[0, 20, 30]
		
		
		//๊ฐ’ ๊ฒ€์ƒ‰ํ•˜๊ธฐ
		//.indexOf(): ๊ฐ’์˜ index๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ๊ฐ’์ด ์—†๋‹ค๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
		System.out.println(intArrayList.indexOf(40));	//-1			
		System.out.println(intArrayList.indexOf(0));	//0		
		//.contains(): ๊ฐ’์ด ์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋งŒ ํŒŒ์•…ํ•œ๋‹ค. 
		System.out.println(intArrayList.contains(40));	//false
		System.out.println(intArrayList.contains(0));	//true
		
		//.isEmpty(): ๋ฐฐ์—ด์ด ์•„์˜ˆ ๋น„์–ด์ž‡๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
		System.out.println(intArrayList.isEmpty());		//false

	}	

}

โœ๏ธ ํด๋ž˜์Šค ArrayList ์˜ˆ์‹œ์ฝ”๋“œ

import java.util.ArrayList;

class Book{
	String bookName;
	String writer;
	
	void bookInfor() {
		System.out.println("์ฑ… ์ œ๋ชฉ: "+this.bookName+"์ฑ… ์ €์ž: "+this.writer);
	}
	Book(String bookname, String writer){
		this.bookName = bookname;
		this.writer = writer;
		
	}
}

public class ArrayListTest2 {

	public static void main(String[] args) {
		ArrayList<Book> books = new ArrayList<Book>();
		
		books.add(new Book("์—ฌ๋ฆ„", "์ด๋””์Šค ์›ŒํŠผ"));
		books.add(new Book("์ด๋ฐฉ์ธ", "์•Œ๋ฒ ๋ฅด ์นด๋ฎˆ"));
		books.add(new Book("๋ธŒ๋žŒ์Šค๋ฅผ ์ข‹์•„ํ•˜์‹œ๋‚˜์š”...", "ํ”„๋ž‘์ˆ˜์•„์ฆˆ ์‚ฌ๊ฐ•"));
		
		System.out.println(books); 	//์ธ์Šคํ„ด์Šค ์ฃผ์†Œ๊ฐ€ ๋ฐ˜ํ™˜๋จ. [test.Book@7c30a502, test.Book@49e4cb85, test.Book@2133c8f8]
		
		for(Book book : books) {
			book.bookInfor();
		}
	}
}

๐Ÿ‘‰ ์‹คํ–‰ํ™”๋ฉด

  ์ฑ… ์ œ๋ชฉ: ์—ฌ๋ฆ„์ฑ… ์ €์ž: ์ด๋””์Šค ์›ŒํŠผ
  ์ฑ… ์ œ๋ชฉ: ์ด๋ฐฉ์ธ์ฑ… ์ €์ž: ์•Œ๋ฒ ๋ฅด ์นด๋ฎˆ
  ์ฑ… ์ œ๋ชฉ: ๋ธŒ๋žŒ์Šค๋ฅผ ์ข‹์•„ํ•˜์‹œ๋‚˜์š”...์ฑ… ์ €์ž: ํ”„๋ž‘์ˆ˜์•„์ฆˆ ์‚ฌ๊ฐ•

๐Ÿค”๋งˆ๋ฌด๋ฆฌํ•˜๋ฉด์„œ๐Ÿค”

์ง์ ‘ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด์„œ ๊ณต๋ถ€ํ•˜๋Š”๊ฒŒ ํ™•์‹คํžˆ ๋„์›€์ด ๋˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค๐Ÿ‘๐Ÿ‘
profile
24๋…„๋„๊นŒ์ง€ ํ”„๋กœ์ ํŠธ ๋‘๊ฐœ๋ฅผ ๋งˆ์น˜๊ณ  25๋…„์—๋Š” ๊ฐœ๋ฐœ ํŒ€์žฅ์„ ํ•  ์ˆ˜ ์žˆ๋Š” ์‹ค๋ ฅ์ด ๋˜์ž!

1๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2023๋…„ 7์›” 19์ผ

๊ธ€์ด ๋งŽ์€ ๋„์›€์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค, ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ