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.