본문 바로가기
  • 콩's 코딩노트
Android

Android Studio - ArrayList 에 저장된 데이터를 가져와 처리하는데 좋은 반복문 코드

by Cong_S 2022. 7. 14.

for 반복문의 코드이다. 

 if(cursor.moveToFirst()){
        for(int i = 0; i < cursor.getCount(); i++ ){
            Contact contact = new Contact(cursor.getInt(0), cursor.getString(1), cursor.getString(2));
            contactList.add(contact);
            cursor.moveToNext();
        }
    }

 

while 반복문의 코드이다. do { } while { } 의 모습을 가지고 있다.

if(cursor.moveToFirst()){
            do{
                Contact contact = new Contact(cursor.getInt(0), cursor.getString(1), cursor.getString(2));
                contactList.add(contact);
            }while(cursor.moveToNext());
        }

커서를 계속 처음으로 가져오는 movetoFirst 와 커서가 다음으로 움직이게 하는 movetoNext 를 사용한 코드이다.

댓글