Foggy day

[Dart] Collection - List, Map, Set 본문

Flutter/Dart 문법

[Dart] Collection - List, Map, Set

jinhan38 2023. 3. 2. 15:47

 

이번 포스팅에서는 List, Map, Set에 대해 알아보겠습니다.

 

1. List

List는 같은 타입의 자료를 여러개 담을 수 있습니다. 순서가 있으며 다른 언어와 다른 점은 Dart에서는 Array(배열)과 List를 구분하지 않고, List를 사용합니다. List의 순서는 0번부터 시작됩니다. 

 

기본적인 선언 형태는 List<타입> 변수명 = []; 입니다. 

<>안에 선언해준 타입과 동일한 타입만 List에 넣을 수 있습니다. 

List<String> listString = ["aa", "bb", "cc"];
List<int> listInt = [1, 2, 3, 4, 5];
List<bool> listBool = [true, true, true];

 

 

List는 가지고 있는 데이터를 추가, 삭제할 수 있습니다. 

 

- 데이터 추가

데이터를 추가할 때 add, addAll, insert 함수를 사용합니다.

  • add : list의 가장 마지막에 데이터 한개를 추가합니다.
  • addAll : list의 가장 마지막에 여러개의 데이터를 추가합니다.
  • insert : 특정 위치에 데이터 한개를 추가합니다. 기존의 데이터는 한칸씩 뒤로 밀려납니다. 
  void listAdd() {
    List<int> listInt = [1, 2, 3, 4, 5];
    print('Collection.listAdd listInt : $listInt');	// [1, 2, 3, 4, 5]
    listInt.add(5);
    print('Collection.listAdd listInt add : $listInt'); // [1, 2, 3, 4, 5, 5]
    listInt.addAll([1, 2, 3]);
    print('Collection.listAdd listInt addAll : $listInt');	// [1, 2, 3, 4, 5, 5, 1, 2, 3]

    listInt.insert(1, 20); // 1번 index에 20이라는 값을 추가합니다. 
    print('Collection.listAdd listInt insert 1 : $listInt'); // [1, 20, 2, 3, 4, 5, 5, 1, 2, 3]
    
    listInt.insert(1, 50); // 1번 index에 50이라는 값을 추가합니다. 
    print('Collection.listAdd listInt insert 2 : $listInt'); // [1, 50, 20, 2, 3, 4, 5, 5, 1, 2, 3]
  }

 

 

- 데이터 삭제

데이터를 삭제할 때 remve, removeLast, removeAt, clear를 사용할 수 있습니다. 

  • removeLast : 마지막 데이터 삭제
  • remove : 입력한 값과 동일한 데이터 삭제
  • removeAt : 입력한 index의 값 삭제
  • clear : 전부 삭제 
  void listRemove() {
    List<int> listInt = [1, 2, 3, 4, 5, 6, 7, 8];
    print('Collection.listRemove listInt : $listInt');
    listInt.removeLast();
    print('Collection.listRemove listInt removeLast : $listInt'); // [1, 2, 3, 4, 5, 6, 7];
    listInt.remove(2);
    print('Collection.listRemove listInt remove : $listInt'); // [1, 3, 4, 5, 6, 7];
    listInt.removeAt(1);
    print('Collection.listRemove listInt removeAt 1 : $listInt'); // [1, 4, 5, 6, 7];
    listInt.removeAt(1);
    print('Collection.listRemove listInt removeAt 2 : $listInt'); // [1, 5, 6, 7];
    listInt.clear();
    print('Collection.listRemove clear : $listInt'); // [];
  }

 

 

2. Map

Map은 순서가 없으며, key : value 형태로 데이터를 입력할 수 있습니다.

Map은 데이터를 다룰 때 key를 사용합니다. 

- Map변수[key] => key에 해당되는 value 리턴

 

데이터를 입력할 때는 putIfAbsent와 []를 사용합니다. putIfAbsent는 입력한 key가 없을 때 데이터를 입력하고, []는 입력한 key가 없으면 데이터를 입력하고, 있으면 기존에 있는 데이터를 입력한 값으로 변경합니다.

데이터를 제거할 때는 remove함수에 소괄호와 key 값을 입력해서 사용합니다.

  void collectionMap() {
    Map map = {1: 10, "20": 20, "30": 30, "aaa": "aaa", "가나다라": "마바사"};

    print('Collection.collectionMap map : $map'); // {1: 10, 20: 20, 30: 30, aaa: aaa, 가나다라: 마바사}
    print('Collection.collectionMap map[1] key int : ${map[1]}'); // key int : 10
    print('Collection.collectionMap map[1] key String : ${map["1"]}'); // key String : null
    print('Collection.collectionMap map[aaa] : ${map["aaa"]}'); // aaa 
    print('Collection.collectionMap map.length : ${map.length}'); // 5

    map.putIfAbsent("가나다라", () => "마");
    print('Collection.collectionMap putIfAbsent : $map'); // {1: 10, 20: 20, 30: 30, aaa: aaa, 가나다라: 마바사}
    map["가나다라"] = "마";
    print('Collection.collectionMap map["가나다라"] : $map'); // {1: 10, 20: 20, 30: 30, aaa: aaa, 가나다라: 마}
    map.putIfAbsent("ABC", () => "D");
    print('Collection.collectionMap putIfAbsent ABC : $map'); // {1: 10, 20: 20, 30: 30, aaa: aaa, 가나다라: 마, ABC: D}
    map["K"] = "M";
    print('Collection.collectionMap map["K"] : $map'); // {1: 10, 20: 20, 30: 30, aaa: aaa, 가나다라: 마, ABC: D, K: M}

    map.remove("K");
    print('Collection.collectionMap remove : $map'); // {1: 10, 20: 20, 30: 30, aaa: aaa, 가나다라: 마, ABC: D}
    map.clear();
    print('Collection.collectionMap clear : $map'); // {}

    Map<String, String> stringMap = {"aa": "bb"};
  }

Map을 선언할 때 타입을 지정해주지 않는다면 아무 타입이나 입력이 가능하지만 <key type, value type>를 이용해서 타입을 선언해주면 해당 타입만 입력이 가능합니다.

예제의 마지막에 Map<String, String> stringMap = {"aa": "bb"}; 로선언한 것을 보면 <String, String>으로 타입을 선언했습니다. Key와 Value의 타입 모두 String으로 선언했기 때문에 데이터는 String만 입력할 수 있습니다. 그렇지 않으면 에러가 발생합니다. 

 

 

 

3. Set

Set은 List처럼 특정 데이터를 추가할 수 있는데 차이점은 순서가 없다는 것과, 동일한 값은 입력할 수 없다는 점입니다. 

<>부호를 사용해서 타입을 지정해주면 해당 타입만 입력 가능하지만 그렇지 않다면 타입에 상관 없이 데이터를 입력할 수 있습니다. 

주요 함수로는 add, addAll, remove, removeAll, clear 등이 있습니다. 

  void collectionSet() {
    Set set = {"a", 1, 2, 3, 4, "가", true};
    print('Collection.collectionSet set : $set');

    print('Collection.collectionSet set.elementAt(0) : ${set.elementAt(0)}');

    set.add("나");
    print('Collection.collectionSet add 나 : $set');
    set.addAll(["b", "c"]);
    print('Collection.collectionSet addAll b, c: $set');
    set.remove("a");
    print('Collection.collectionSet remove a : $set');
    set.removeAll([1, 2, 3]);
    print('Collection.collectionSet removeAll 1,2,3 : $set');

    set.clear();
    print('Collection.collectionSet clear : $set');
    
    Set<String> setString ={"aa", "bb"};
  }

 

'Flutter > Dart 문법' 카테고리의 다른 글

[Dart] 함수  (0) 2023.03.02
[Dart] 반복문(for문)  (0) 2023.03.02
[Dart] 조건문(conditional)  (0) 2023.03.02
[Dart] 연산자, 형변환  (0) 2023.03.02
[Dart] 변수(variable)와 상수(constant)  (0) 2023.03.02