자바/Java ArrayList 안에 ArrayList 넣기
안드로이드 프로젝트를 진행하다 보면, ArrayList 안에 ArrayList를 넣게 되는 경우가 발생 하게 됩니다. 만약 그런 경우가 발생하게 된다면 ArrayList<Stirng>, ArrayList<Interger> 등과 같이 기존에 제네릭스를 사용했던 방법과 동일하게 ArrayList<ArrayList<String>>으로 ArrayList 자체를 제네릭 선언을 해주면 됩니다.
자 그럼, ArrayList안에 ArrayList를 넣는 방법에 대해 알아 보겠습니다.
package arabiannight.tistory.com.arrayintoarray;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class TestArrayIntoArrayActivity extends Activity {
private ArrayList<ArrayList<String>> mGroupList = null;
private ArrayList<String> mChildList = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mGroupList = new ArrayList<ArrayList<String>>();
mChildList = new ArrayList<String>();
mChildList.add("차일드1");
mChildList.add("차일드2");
mChildList.add("차일드3");
mGroupList.add(mChildList);
mChildList = new ArrayList<String>();
mChildList.add("new 차일드1");
mChildList.add("new 차일드2");
mChildList.add("new 차일드3");
mGroupList.add(mChildList);
for(int i=0 ; i<mGroupList.size() ; i++){
Log.d("array", "" + i);
for(int j=0 ; j<mChildList.size() ; j++){
Log.d("array", "" + mGroupList.get(i).get(j));
}
}
}
}
아래는 위 코드의 결과 화면 입니다.
04-12 01:05:03.645: D/array(12139): 0 04-12 01:05:03.685: D/array(12139): 차일드1 04-12 01:05:03.685: D/array(12139): 차일드2 04-12 01:05:03.685: D/array(12139): 차일드3 04-12 01:05:03.685: D/array(12139): 1 04-12 01:05:03.685: D/array(12139): new 차일드1 04-12 01:05:03.685: D/array(12139): new 차일드2 04-12 01:05:03.685: D/array(12139): new 차일드3
파일첨부 :
'JAVA > ArrayList' 카테고리의 다른 글
| 자바/Java ArrayList 사용법 ~! (2) | 2013.01.21 |
|---|---|
| 자바/Java String[]을 List , List를 String[] 배열로 변환 (4) | 2012.03.15 |
TestArrayIntoArray.zip