2014-04-27 2 views
-1

내가 자바 코드를 작성하려고 나는이 목록을 List는) listOfLists에, 나는 내가 노력 listOfIntegers추가 요소는

을 취소해야합니다

listOfLists.add(listOfIntegers); 
listOfIntegers.clear(); 

하지만이 0에 대한 참조를 지 웁니다

List<Integer> newList = new ArrayList<>(); 
for (Integer i : lineIntegersList) 
{ 
    newList.add(i); 
} 

listOfLists.add(newList);     
lineIntegersList.clear(); 
+0

왜 목록 목록이 필요한가요? –

+0

지금 막 게시 한 코드가 당신이 찾고있는 코드라면, 자신 만의'for' 루프를 작성할 필요가 없습니다. 'List newList = new ArrayList <> (lineIntegersList);와 같은 코핑 생성자를 사용하면됩니다. – Pshemo

답변

0

...

ArrayList<ArrayList<String>> listOlists = new ArrayList<ArrayList<String>>(); 
ArrayList<String> singleList = new ArrayList<String>(); 
singleList.add("hello"); 
singleList.add("world"); 
listOlists.add(singleList); 

ArrayList<String> anotherList = new ArrayList<String>(); 
anotherList.add("this is another list"); 
listOlists.add(anotherList); 

@tster에 감사드립니다 - 여기에 "부모 목록"을 인스턴스화하고 어떻게하는지 알 것입니다. 부모 목록에 추가 할 하위 목록을 만드십시오 : D

-1
for (Integer i: listOfIntegers) { 
List<Integer> list = new LinkedList<Integer>(); 
list.add(i); 
listOfLists.add(list); 
} 

이 솔루션은 기본적으로 초기에 정수의 목록의 각 정수에 대해 하나 개의 요소와 새로운 목록을 추가 마누엘 실바에 listOfLists

감사의은 내가 필요한 것을 함께했다 목록 목록 당신은 이런 일을 할 수