티스토리 뷰

https://api.dart.dev/stable/2.13.4/dart-core/List/List.filled.html

 

List.filled constructor - List class - dart:core library - Dart API

List .filled(int length, E fill, {bool growable = false} ) Creates a list of the given length with fill at each position. The length must be a non-negative integer. Example: List .filled(3, 0, growable: true); // [0, 0, 0] The created list is fixed-length

api.dart.dev

 

https://api.flutter.dev/flutter/dart-core/List/List.generate.html

 

List.generate constructor - List - dart:core library - Dart API

List .generate(int length, E generator(int index ), {bool growable = true} ) Generates a list of values. Creates a list with length positions and fills it with values created by calling generator for each index in the range 0 .. length - 1 in increasing or

api.flutter.dev

 

 

List.filled는 생성시 같은 객체가 생성된다 

예를들어 다음과 같이 2xN 크기의 배열을 생성시 arr의 0행과 1행은 같은 객체이다.

var arr = new List.filled(
    2,
    new List.filled(N, ' ', growable: false),
    growable: false);

따라서 만약 다음과 같이 배열을 수정후 출력하면 

arr[0][0] = '*';
print(arr);

이렇게 0행과 1행의 0열이 둘 다 바뀐다. 

0행과 1행은 같은 객체를 그대로 쓰기 때문이다. 

 

따라서 이럴 경우 generate를 써주면 된다. 

var arr2 = List.generate(2, (index) => List.generate(N, (_) => ' '));

'노트' 카테고리의 다른 글

Java) 함수의 파라미터에는 복사된 주소값이 전달된다  (0) 2022.11.13
MacOS Ventura 업데이트 후 clion 디버거 오류  (0) 2022.11.01
JDBC 기본 흐름  (0) 2022.07.05
JDBC  (0) 2022.06.28
Java의 인터페이스  (0) 2022.06.06
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/04   »
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 30
글 보관함