Post

List, ArrayList, LinkedList BigO, DI

βœ… Array

  • πŸ‘πŸ» use index and input, change, get value: O(1)
    • λ°°μ—΄μ—λŠ” indexκ°€ 있기 λ•Œλ¬Έμ—
  • πŸ‘ŽπŸ» search, insert value: O(n), need to search, compare the whole array,
  • πŸ‘ŽπŸ» array size is fixed, need to fix array size when creating array

  • input value array[100] = 1; : O(1)
  • change value array[100] = 10; : O(1)
  • get value array[100] : O(1)
  • search value if(array[i] == 10) : O(n)
  • insert certain value in the middle of array: O(n), μ˜†μœΌλ‘œ ν•œ μΉΈμ”© λ‹€ λ°€μ–΄μ•Ό 함

❓ How does index help get/change value fast?

  • in array, only one type of data is possible
    • int[], String[]
  • each dataset has same size
    • int has 4 byte
  • so memory μ‹œμž‘ μœ„μΉ˜ + index * 4byte

Image

  • memory μ‹œμž‘ μœ„μΉ˜: x100
  • int: 4byte
  • index[2]의 μœ„μΉ˜: x100 + 4byte * 2

πŸ’‘ BigO

μ•Œκ³ λ¦¬μ¦˜μ˜ 데이터 크기 변화에 λ”°λ₯Έ μ„±λŠ₯ λ³€ν™” κ²½ν–₯을 μ•Œλ €μ€€λ‹€

Image

  • O(1): find with one operation, ν•œλ²ˆλ§Œμ— 찾음
  • O(n): n number of data, n operations
  • O(n2): n number of data, much more n*n operations
  • O(log n): more than O(1), but less than O(n)
    • tree
  • O(n log n): like ` O(log n), but multiplied by n`

βœ… List

  • flexible size
  • duplication allowed
  • index

  • List πŸ†š Array: List has flexible size

βœ… ArrayList

use array
flexible size

  • πŸ‘πŸ» flexible size
    • if more space is needed, create a new arraylist with more space and copy paste the existing old array
      • default capacity:10
      • if exceeds default capacity, increase capacity by 50%
      • κΈ°λ³Έ ν¬κΈ°λŠ” 10, 11번째 데이터 μΆ”κ°€ ➑️ 크기 15 ➑️ 22 ➑️ 33…
      • then copy old arraylist with System.arraycopy()
  • πŸ‘ŽπŸ» some space of arraylist might not be used ➑️ waste memory
    • ν•˜μ§€λ§Œ arraylistλ₯Ό λ‚΄κ°€ ν•„μš”ν•œ 데이터 크기에 λ”± 맞게 λ§Œλ“œλŠ” 건 μ•„λ‹ˆλ―€λ‘œ
    • arraylist에 λ‚¨λŠ” 곡간이 생길 수 있음
    • 데이터 11만 μΆ”κ°€ν•˜λ©΄ 12~15λŠ” λ‚¨λŠ” 곡간
  • πŸ‘πŸ» get value with index, add or delete last value is O(1)
  • πŸ‘ŽπŸ» search, add or delete in the first or middle of array is O(n)
  • (μ˜†μœΌλ‘œ ν•œ μΉΈμ”© λ‹€ λ°€μ–΄μ•Ό 함)

  • add/delete data last of array: O(1)
  • add/delete data middle or front: O(n)
  • get value with index: O(1)
  • search: O(n)

❓ array λ˜λŠ” array listμ—μ„œ 데이터 쀑간 μ‚½μž…/μ‚­μ œ 연산이 끝 λΆ€λΆ„ μ‚½μž…/μ‚­μ œλ³΄λ‹€ μ™œ 일반적으둜 λΉ„νš¨μœ¨μ μΌκΉŒμš”?

  • 쀑간에 μ‚½μž…λœ/λΉ μ§„ 데이터 외에 λ‚˜λ¨Έμ§€λ₯Ό μ΄λ™μ‹œμΌœμ•Ό ν•΄μ„œ

βœ… ArrayList and generic

  • use generic in array list to ensure type safety
  • in one array list, only one type of data
1
2
3
4
5
6
7
8
9
10
11
//πŸ‘ŽπŸ» Before
MyArrayList list = new MyArrayList();
list.add(1);
list.add("hello");
//list: [1, "hello"] //Integer, String이 ν˜Όν•©λ˜μ–΄ 있음

//πŸ‘πŸ» After
MyArrayList<Inetger> list = new MyArrayList(); //use generic
list.add(1);
list.add("hello"); //πŸ”΄ compile error
//πŸ‘πŸ» listμ—λŠ” Integer만 λ“€μ–΄κ°ˆ 수 있게 됨

πŸ’‘ Node

node = value βž• next node reference address

  • node끼리 μ—°κ²°
  • μ•ž nodeλŠ” λ‹€μŒ node의 μ£Όμ†Œλ₯Ό κ°€μ§€κ³  있음

  • πŸ‘πŸ» ν•„μš”ν•œ 크기만큼만 λ©”λͺ¨λ¦¬ 확보, no memory waste
  • πŸ‘πŸ» add/delete middle/front data efficiently

Image

βœ… Linked List

has order, allow duplication
use node
reference before, next node
know first, last node

  • πŸ‘πŸ» linked list size is size of data, no memory waste
  • πŸ‘πŸ» add/delete front, middle, last: O(1), only has to change surrounding node references(address)
  • πŸ‘ŽπŸ» search data with specific index, has to search whole linked list ➑️ O(n)

πŸ’‘ Dependency Injection

  • OCP: client code should be closed, main code should be open
  • Dependency Injection:
    • postpoone for dependency injection until main code is RUN time
    • open to what specific class will be injected: OCP
    • client code does not change, postpone so that main code will decide: OCP
    • use interface
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//βœ”οΈ interface MyList
public interface MyList <E>{}

//βœ”οΈ MyList is implemented by MyArrayList and MyLinkedList
// ⭐️ depend on interface MyList
public class MyArrayList<E> implements MyList<E>{}

public class MyLinkedList<E> implements MyList<E> {}

//βœ”οΈ client code
public class BatchProcessor {
    //use interface, not specific class such as MyArrayList or MyLinkedList
    //DO NOT DEPEND on a specific class
    //POSTPONE
    // ⭐️ depend on interface MyList
    private final MyList<Integer> list;

      public BatchProcessor(MyList<Integer> list) {
        this.list = list;
    }
}

//βœ”οΈ main class to be run
main(){
    //decide here, in main code which class to run
    //dependency injection
    BatchProcessor arrayBatchProcessor = new BatchProcessor(new MyArrayList<Integer>());

}
  • BatchProcessor(client code) DOES NOT depend on a specific class
  • BatchProcessor(client code) depends on interface, MyList
  • specific class MyArrayList, MyLinkedList depends on interface, MyList
  • main class(class to be run) decides which specific class will be run
    • specific class: MyArrayList, MyLinkedList
  • POSTPONE deciding specific class ➑️ dependency injection
  • OCP: client code does not change, main class is modified
  • polymorphism: several classes could be injected to main class
  • application is loosely coupled, extendable, maintainable

  • 메인 ν΄λΌμŠ€κ°€ μ–΄λ–€ ꡬ체 클래슀λ₯Ό μ‹€ν–‰ν• μ§€ λŸ°νƒ€μž„κΉŒμ§€ 결정을 λ―Έλ£Έ, μ§€μ—°
  • ν΄λΌμ΄μ–ΈνŠΈ ν΄λΌμŠ€λŠ” μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ‚¬μš©, μΈν„°νŽ˜μ΄μŠ€μ— 의쑴 ➑️ μ˜μ‘΄μ„± μ£Όμž…
  • ꡬ체 ν΄λž˜μŠ€λŠ” μΈν„°νŽ˜μ΄μŠ€λ₯Ό κ΅¬ν˜„(의쑴) ➑️ μ˜μ‘΄μ„± μ£Όμž…
  • μ–΄λ–€ 클래슀λ₯Ό μ‹€ν–‰ν• μ§€ λ©”μΈν΄λž˜μŠ€μ—κ²Œ μ•Œλ €μ€˜μ•Ό 함

βœ”οΈ compile time dependency

  • dependency injection between class
  • BatchProcessor(client code) depends on MyList(interface)
  • specific class MyArrayList, MyLinkedList depends on MyList(interface)
    • implements interface

βœ”οΈ runtime dependency

  • dependency injection between instance
1
2
MyLinkedList<Integer> list = new MyLinkedList(); //x002
BatchProcessor processor = new BatchProcessor(list); //reference x002

βœ…

βœ…

βœ…

βœ…

βœ…

This post is licensed under CC BY 4.0 by the author.