본문 바로가기
App/Flutter

[Flutter] Dart 언어 기본 문법 Part 3 - List, Map, Set

by Gina Sim 2023. 6. 11.

[ List ]

  • 중복값을 가질 수 있음

1. List 생성

List<변수타입> 변수명 = [값1, 값2, 값3 ...];
List<String> blackPink = ['제니','지수','로제','리사'];
List<int> numbers = [1,2,3,4,5];

print(blackPink);
// [제니, 지수, 로제, 리사]

print(numbers);
// [1, 2, 3, 4, 5]

 

2. List indexing

  • index는 0부터 시작
print(blackPink[0]);    // 	제니
print(blackPink[1]);    // 	지수
print(blackPink[2]);    // 	로제
print(blackPink[3]);    // 	리사
print(blackPink[4]);    // 	에러 "Uncaught Error: RangeError (index): Index out of range: ..."

 

3. List의 길이

변수명. length
print(blackPink.length);
// 4

 

4. List에 값 추가

변수명.add();
blackPink.add('코드팩토리');
print(blackPink);
// [제니, 지수, 로제, 리사, 코드팩토리]

 

5. List의 값 제거

변수명.remove();
blackPink.remove('코드팩토리');
print(blackPink);
// [제니, 지수, 로제, 리사]

 

6. List 내 특정 값의 index 번호 찾기

변수명.indexOf();
print(blackPink.indexOf('로제'));
// 2

 


 

{ Map }

1. Map 생성

  • key, value값 필요
  • 각 값의 변수타입 적어줌
Map<key타입, value타입> 변수명 = {
    key1 : value1,
    key2 : value2,
    ...
};
Map<String, String> dictionary = {
    'Harry Potter' : '해리포터',
    'Ron Weasley' : '론 위즐리',
    'Hermione Granger' : '헤르미온느 그레인저',
};
print(dictionary);
// {Harry Potter: 해리포터, Ron Weasley: 론 위즐리, Hermione Granger: 헤르미온느 그레인저}


Map<String, bool> isHarryPotter = {
    'Harry Potter' : true,
    'Ron Weasley' : true, 
    'Ironman' : false,
};
print(isHarryPotter);
// {Harry Potter: true, Ron Weasley: true, Ironman: false}

 

 

2. Map에 값 추가

방법 1. addAll을 활용하여 괄호 안의 값을 한 번에 추가할 수 있음
변수명.addAll
({
    key : value,
});

방법 2. 새로운 key값에 value 추가
변수명['새로운 key 값'] = 새로운 value;
// 방법 1 - add All사용
isHarryPotter.addAll({
    'Spiderman' : false,
});
print(isHarryPotter);
// {Harry Potter: true, Ron Weasley: true, Ironman: false, Spiderman: false}


// 방법 2 - 새로운 key값에 value 입력
isHarryPotter['Hulk'] = false;
print(isHarryPotter);
// {Harry Potter: true, Ron Weasley: true, Ironman: false, Spiderman: false, Hulk: false}

 

3. Map의 값 변경

변수명['변경할 key 값'] = 새로운 value;
isHarryPotter['Spiderman'] = true;
print(isHarryPotter);
// {Harry Potter: true, Ron Weasley: true, Ironman: false, Spiderman: true, Hulk: false}

 

4. Map의 값 제거

변수명.remove('삭제 할 key 값');
isHarryPotter.remove('Spiderman');
print(isHarryPotter);
// {Harry Potter: true, Ron Weasley: true, Ironman: false, Hulk: false}

 

5. Map의 key값 또는 value값만 모아 보기

변수명.keys;
변수명.values;
print(isHarryPotter.keys);
// (Harry Potter, Ron Weasley, Ironman, Hulk)

print(isHarryPotter.values);
// (true, true, false, false)

 


 

{ Set }

  • 중복값이 들어갈 수 없음 (중복 자동 처리)

1. Set 생성

final Set<변수타입> 변수명 = {
    값1,
    값2,
    값3,
    ...
};
final Set<String> names = {
    'Code Factory',
    'Flutter',
    'Inflearn',
};
print(names);
// {Code Factory, Flutter, Infrlearn}

 

2. Set에 값 추가

변수명.add();
names.add('Gina');
print(names);
// {Code Factory, Flutter, Infrlearn, Gina}

 

3. Set의 값 제거

변수명.remove();
names.remove('Gina');
print(names);
// {Code Factory, Flutter, Infrlearn}

 

4. Set에 값이 존재하는지 확인

변수명.contains(확인하고자 하는 값);
print(names.contains('Flutter'));
// true

 

 

반응형

댓글