ArrayList


ArrayList


  • java.util ํŒจํ‚ค์ง€์—์„œ ์ œ๊ณต, List ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์†๋ฐ›์€ ํด๋ž˜์Šค

  • ํฌ๊ธฐ๊ฐ€ ๊ฐ€๋ณ€์ ์œผ๋กœ ๋ณ€ํ•˜๋Š” ์„ ํ˜• ๋ฆฌ์ŠคํŠธ

  • ์ผ๋ฐ˜ ๋ฐฐ์—ด๊ณผ ๊ฐ™์ด ์ˆœ์ฐจ๋ฆฌ์ŠคํŠธ์ด๋ฉฐ ์ธ๋ฑ์Šค๋กœ ๋‚ด๋ถ€ ๊ฐ์ฒด ๊ด€๋ฆฌ

  • ๋ฐฐ์—ด์€ ํ•œ๋ฒˆ ์ƒ์„ฑ๋˜๋ฉด ํฌ๊ธฐ๊ฐ€ ๋ณ€ํ•˜์ง€ ์•Š์ง€๋งŒ, ArrayList๋Š” ๊ฐ์ฒด๊ฐ€ ์ถ”๊ฐ€๋˜์–ด ์ €์žฅ์šฉ๋Ÿ‰(capacity)์„ ์ดˆ๊ณผํ•œ๋‹ค๋ฉด ์ž๋™์œผ๋กœ ๋ถ€์กฑํ•œ ํฌ๊ธฐ๋งŒํผ ์ €์žฅ์šฉ๋Ÿ‰์ด ๋Š˜์–ด๋‚จ

  • ArrayList์˜ ํฌ๊ธฐ ๋™์ ์œผ๋กœ ๋ณ€๊ฒฝ๋จ > List์˜ Size๋ฅผ ์ดˆ๊ธฐํ™” ํ•  ํ•„์š” ์—†์Œ

  • ArrayList<๊ฐ์ฒด> arrayList = new ArrayList<>();

  • < > ์ œ๋„ค๋ฆญ์Šค(Generics) - ArrayList ์•ˆ์— ์–ด๋–ค ์ž๋ฃŒํ˜•/๊ฐ์ฒด๋ฅผ ๋„ฃ์„๊ฑด์ง€ ์ง€์ •

import java.util.ArrayList;

public class ArrayListTest {

	public static void main(String[] args) {

		ArrayList<Integer> arrayList = new ArrayList<>();
		
		arrayList.add(1);
		arrayList.add(2);
		arrayList.add(3);
		arrayList.add(4);
		
		for (int i : arrayList) {
			System.out.println(i);
		}
		
	}

}

// ์ถœ๋ ฅ๊ฒฐ๊ณผ
// 1
// 2
// 3
// 4


ArrayList๋ฅผ ํ™œ์šฉํ•ด ๊ฐ„๋‹จํ•œ ์„ฑ์  ์‚ฐ์ถœ ํ”„๋กœ๊ทธ๋žจ ๋งŒ๋“ค๊ธฐ


  • Student.java

      import java.util.ArrayList;
    
      public class Student {
    
          String studentName;
          ArrayList<Subject> subjectList;
            
          Student(String studentName) {
                
              this.studentName = studentName;
                
              subjectList = new ArrayList<>();
          }
            
          public void addSubject(String subjectName, int score) {
                
              Subject subject = new Subject();
              subject.setName(subjectName);
              subject.setScore(score);
                
              subjectList.add(subject);
          }
            
          public void showInfo() {
                
              int total = 0;
                
              for (Subject subject : subjectList) {
                    
                  total += subject.getScore();
                  System.out.println(studentName + " ํ•™์ƒ์˜ " + subject.getName()
                  + " ๊ณผ๋ชฉ์˜ ์„ฑ์ ์€ " + subject.getScore() + "์  ์ž…๋‹ˆ๋‹ค.");
              }
                
              System.out.println(studentName + " ํ•™์ƒ์˜ ์ด์ ์€ " + total + "์  ์ž…๋‹ˆ๋‹ค.");
          }
            
      }
    


  • Subject.java

      public class Subject {
    
          private String subjectName;
          private int score;
            
          public Subject(String subjectName, int score) {
              this.subjectName = subjectName;
              this.score = score;
          }
            
          public Subject() {
          }
    
          public String getName() {
              return subjectName;
          }
    
          public void setName(String subjectName) {
              this.subjectName = subjectName;
          }
    
          public int getScore() {
              return score;
          }
    
          public void setScore(int score) {
              this.score = score;
          }
      }
    


  • Main.java

      public class Main {
    
          public static void main(String[] args) {
    
              Student student1 = new Student("์ด์ž๋ฐ”");
                
              student1.addSubject("์ˆ˜ํ•™", 67);
              student1.addSubject("์˜์–ด", 98);
                
              student1.showInfo();
                
              System.out.println();
                
              Student student2 = new Student("๊น€์ž์Šค");
                
              student2.addSubject("์ˆ˜ํ•™", 79);
              student2.addSubject("์˜์–ด", 55);
                
              student2.showInfo();
          }
    
      }
    


  • ์ถœ๋ ฅ ๊ฒฐ๊ณผ

      ์ด์ž๋ฐ” ํ•™์ƒ์˜ ์ˆ˜ํ•™ ๊ณผ๋ชฉ์˜ ์„ฑ์ ์€ 67์  ์ž…๋‹ˆ๋‹ค.
      ์ด์ž๋ฐ” ํ•™์ƒ์˜ ์˜์–ด ๊ณผ๋ชฉ์˜ ์„ฑ์ ์€ 98์  ์ž…๋‹ˆ๋‹ค.
      ์ด์ž๋ฐ” ํ•™์ƒ์˜ ์ด์ ์€ 165์  ์ž…๋‹ˆ๋‹ค.
    
      ๊น€์ž์Šค ํ•™์ƒ์˜ ์ˆ˜ํ•™ ๊ณผ๋ชฉ์˜ ์„ฑ์ ์€ 79์  ์ž…๋‹ˆ๋‹ค.
      ๊น€์ž์Šค ํ•™์ƒ์˜ ์˜์–ด ๊ณผ๋ชฉ์˜ ์„ฑ์ ์€ 55์  ์ž…๋‹ˆ๋‹ค.
      ๊น€์ž์Šค ํ•™์ƒ์˜ ์ด์ ์€ 134์  ์ž…๋‹ˆ๋‹ค.
    

Categories:

Java