본문 바로가기
프로그램 개발해서 돈벌기/flutter

3. 콜렉션 (Collection : List, Set, Map) - 완전 초보 Dart 언어 기초 문법 : flutter

by ubmuhan 2022. 12. 20.
반응형

사용 툴 - DartPad

https://dartpad.dev/

 

DartPad

 

dartpad.dev

DartPad 정보 : Based on Flutter 3.3.10 Dart SDK 2.18.6

Collection

  • List : 데이터 배열로 순서가 있고 중복을 허용합니다. 자바 Array와 같습니다.
  • Set : 데이터 배열로 순서가 없고 중복 데이터를 허용하지 않습니다.
  • Map : 키(Key)와 값(Value)로 구성된 클래스로서 키는 중복을 허용하지 않고 값은 중복을 허용합니다.

 

List

void main() {
  // List 선언
  List list = [];
  print(list);
  // >> []
  
  List addedList = ["one", "two", "three"];
  print(addedList);
  // >> [one, two, three]
  
  List addedList2 = List.from(["one", "two", "three"]);
  print(addedList2);
  // >> [one, two, three]
  
  // 값 추가
  list.add("one");
  list.add("two");
  list.add("three");
  print(list);
  // >> [one, two, three]
  
  // index : 값에 접근 번호
  print(list[1]);
  // >> two
  
  // index를 사용해서 값 삭제
  list.removeAt(1);
  print(list);
  // >> [one, three]
  
  // index를 사용해서 값 변경
  list[1] = "two";
  print(list);
  // >> [one, two]
  
  // List에 특정 값만 받게 정의
  List<String> strList = [];
  strList.add("one");
  print(strList);
  // >> [one]
  
  // List 길이를 알아옴
  print(list.length);
  // >> 2
}

 

Set

void main() {
  final values = {
    1, 2, 3, 4, 5, 6
  };
  
  // Set 선언
  Set set = Set();
  print(set);
  // >> {}
  
  Set set2 = {1, 2, 3};
  print(set2);
  // >> {1, 2, 3}
  
  Set set3 = <int>{};
  print(set3);
  // >> {}
  
  // 항목 추가
  set2.add(4);
  print(set2);
  // >> {1, 2, 3, 4}
  
  set.addAll([1, 2, 3, 4, 5]);
  print(set);
  // >> {1, 2, 3, 4, 5}
  
  set3.add(1);
  print(set3);
  // >> {1}
  
  set3.addAll(values);
  print(set3);
  // >> {1, 2, 3, 4, 5, 6}
  
  // 추가 항목 값이 있으면 추가 되지 않음
  set2.add(3);
  print(set2);
  // >> {1, 2, 3, 4}
  
  // 항목 제거
  set2.remove(4);
  print(set2);
  // >> {1, 2, 3}
  
  // 항목 값 체크
  print(set2.contains(3));
  // >> true
  print(set2.containsAll([1, 2]));
  // >> true
  
  // 두 Set간 교집합 Set 생성
  Set interSection = set.intersection(set2);
  print(interSection);
  // >> {1, 2, 3}
}

 

Map

void main() {
  // 빈 Map 선언
  Map dic = {};
  print(dic);
  // >> {}
  
  Map dicSpl2 = Map();
  print(dicSpl2);
  // >> {}
  
  // 선언 시 데이터가 있는 Map 선언
  Map dicSpl = {
    "apple" : "사과",
    "banana" : "바나나",
    "grape" : "포도"
  };
  print(dicSpl);
  // >> {apple: 사과, banana: 바나나, grape: 포도}
  
  Map dicSpl3 = Map.from(
    {
      "apple" : "사과",
      "banana" : "바나나",
      "grape" : "포도"
    }
  );
  print(dicSpl3);
  // >> {apple: 사과, banana: 바나나, grape: 포도}
  
  // 값 접근
  print(dicSpl["apple"]);
  // >> 사과
  
  // key, value 추가
  // key는 중복이 안됩
  // key가 중복 입력 되면 덥어 쓰게됨
  dic.addAll(
    {
      "apple" : "사과",
      "banana" : "바나나",
      "grape" : "포도"
    }
  );
  print(dic);
  // >> {apple: 사과, banana: 바나나, grape: 포도}
  
  // key, value 삭제
  dic.remove("apple");
  print(dic);
  // >> {banana: 바나나, grape: 포도}
  
  // 값 변경
  dic["banana"] = "진짜 바나나";
  print(dic["banana"]);
  // >> 진짜 바나나
  
  // 키만 출력
  print(dic.keys.toList());
  // >> [banana, grape]
  
  // 값만 출력
  print(dic.values.toList());
  // >> [진짜 바나나, 포도]
  
  Map<String, int> dicSpl4 = {
    "apple" : 1,
    "banana" : 2,
    "grape" : 3
  };
  print(dicSpl4);
  // >> {apple: 1, banana: 2, grape: 3}
  
  // 임의 값이 갖게 선언
  Map<String, void> dicSpl5 = {
    "apple" : 1,
    "banana" : "바나나",
    "grape" : true
  };
  print(dicSpl5);
  // >> {apple: 1, banana: 바나나, grape: true}
}

 

 

 

 
 
 
 
 
반응형

댓글