[Dart] datas.insert(1, datas.removeAt(0)); > Flutter/Dart/Node

본문 바로가기
사이트 내 전체검색

Flutter/Dart/Node

[Dart] datas.insert(1, datas.removeAt(0));

페이지 정보

작성자 sbLAB 댓글 0건 조회 2,264회 작성일 23-02-05 22:48

본문

※ First 와 Second 위치(position) 교환


List<String> datas = [

        'First',

        'Second',

      ];


// ---> [Second, First]


datas.insert(1, datas.removeAt(0)); //이 로직이 가능한 이유.

※ Dart 의 removeAt 메서드는 대상 index 를 삭제하고, 삭제된 자리로 뒤 자료들이 내려와 채워지며, 그 삭제된 값이 리턴된다.

※ datas.removeAt(0) 으로 index 0 을 삭제하면 datas[1]에 있던 'Second'는 data[0]으로 내려가고(down),  

   삭제된 'First' 리턴값으로 datas.insert(1, 'First') 로 실행되어 data[1]에 'First' 삽입되면서 자리교환된다.


removeAt abstract method 

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


[Sample]

   
      List<String> datas = [
        'First',
        'Second',
      ];

    main(){
      print(datas);  //[First, Second]
     
    var result=datas.removeAt(0);  
    print(result); //First 리턴
     
      print(datas); //[Second]
     
      datas.insert(1,result);  
      print(datas); //[Second, First]
           
      datas.insert(1, datas.removeAt(0));
      print(datas); //[First, Second]
       
    }

// [First, Second]
// First
// [Second]
// [Second, First]
// [First, Second]

 




댓글목록

등록된 댓글이 없습니다.

회원로그인

접속자집계

오늘
55
어제
433
최대
1,279
전체
218,723

그누보드5
Copyright © sebom.com All rights reserved.