내가 가진 : 나는`ArrayList의 ArrayList에서 2 차원 배열을 얻는 방법은 무엇입니까?
int[][] adjL = new int[?][?];
로 변환 할
ArrayList<ArrayList<Integer>> adjList = new ArrayList<ArrayList<Integer>>();
가능인가요? 그리고 배열 크기로 ?
을 대체해야하는 것은 무엇입니까?
내가 가진 : 나는`ArrayList의 ArrayList에서 2 차원 배열을 얻는 방법은 무엇입니까?
int[][] adjL = new int[?][?];
로 변환 할
ArrayList<ArrayList<Integer>> adjList = new ArrayList<ArrayList<Integer>>();
가능인가요? 그리고 배열 크기로 ?
을 대체해야하는 것은 무엇입니까?
간단하지만 이것은 당신을 도움이되지 않음 :이 같은 입력에 대한
ArrayList<ArrayList<Integer>> adjList = new ArrayList<>();
Integer[][] result = new Integer[adjList.size()][];//create your array of arrays
for (int i = 0; i < adjList.size(); i++) {
//foreach array you have to inisialize it with the correct size
result[i] = new Integer[adjList.get(i).size()];
//the fill the array with the value of your list
for(int j = 0; j < adjList.get(i).size(); j++){
result[i][j] = adjList.get(i).get(j);
}
}
:
adjList.add(new ArrayList<>(Arrays.asList(1, 2, 3, 4)));
adjList.add(new ArrayList<>(Arrays.asList(1, 2, 3)));
...
System.out.println(Arrays.deepToString(result));
출력 : 제안하지만
[[1, 2, 3, 4], [1, 2, 3]]
정말 고마워요. 나는이 단계를 놓친 것을 본다 'result [i] = new Integer [adjList.get (i) .size()]; ' 그리고 나는 정확한 크기를 얻지 못했다. – Thammarith
문제는 기본적으로 대답했다 솔루션은 Integer[][]
을 반환하지만 요청 된 int[][]
은 반환하지 않습니다. 질문에 -하지만 그것은 세부 수 있습니다.
그러나 "컬렉션의 숫자 모음"을 2D 프리미티브 배열로 변환하는 작업은 매우 일반적이며 따라서 은으로 작성해야합니다.
여기 스트림을 사용하는 다른 솔루션이 있으며 임의의 숫자로 구성된 임의의 컬렉션에 적용 할 수 있습니다. 반드시 다른 솔루션보다 "더 나은"것은 아니지만 다른 응용 프로그램의 경우에는 참고 자료 일 수 있습니다.
ArrayList<ArrayList<Integer>>
을 int[][]
어레이로 변환하는 경우를 나타내지 만, 방법은 showingThatThisIsFarMoreGeneric
을 포함한다.
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.Vector;
public class ToIntArray2D
{
public static void main(String[] args)
{
ArrayList<ArrayList<Integer>> adjList = createTestLists();
int[][] result = toIntArray2D(adjList);
System.out.println(Arrays.deepToString(result));
}
private static int[][] toIntArray2D(
Collection<? extends Collection<? extends Number>> collections)
{
return collections.stream()
.map(c -> toIntArray(c))
.<int[]> toArray(int[][]::new);
}
private static int[] toIntArray(
Collection<? extends Number> collection)
{
return collection.stream().mapToInt(Number::intValue).toArray();
}
private static void showingThatThisIsFarMoreGeneric()
{
ArrayList<ArrayList<Integer>> exampleA = null;
List<List<Integer>> exampleB = null;
Set<Vector<Float>> exampleC = null;
LinkedList<HashSet<? extends BigInteger>> exampleD = null;
LinkedList<? extends HashSet<? extends BigDecimal>> exampleE = null;
toIntArray2D(exampleA); // Works
toIntArray2D(exampleB); // Works
toIntArray2D(exampleC); // Works
toIntArray2D(exampleD); // Works
toIntArray2D(exampleE); // Works
}
private static ArrayList<ArrayList<Integer>> createTestLists()
{
ArrayList<ArrayList<Integer>> adjList =
new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> adjList0 = new ArrayList<Integer>();
adjList0.add(0);
adjList0.add(1);
adjList0.add(2);
adjList.add(adjList0);
ArrayList<Integer> adjList1 = new ArrayList<Integer>();
adjList1.add(3);
adjList1.add(4);
adjList.add(adjList1);
ArrayList<Integer> adjList2 = new ArrayList<Integer>();
adjList2.add(5);
adjList2.add(6);
adjList2.add(7);
adjList2.add(8);
adjList.add(adjList2);
return adjList;
}
}
측면 참고 : usually program to an interface해야,
ArrayList<ArrayList<Integer>> adjList;
같은 선언이 거의 이제까지는 어쨌든 실제 코드에 표시되지해야한다는 점을 감안. 오히려해야합니다
List<List<Integer>> adjList;
아무 것도 시도해 보셨습니까? –
@OliverCharlesworth 죄송합니다. 시도한 단계를 추가하는 것을 잊어 버렸습니다. 나는 adjList.toArray()로 시도했다. 그러나 1-D 배열 만 반환합니다. 루프를 사용하여 전송하려고했지만 크기를 구성하는 방법을 모르겠습니다. – Thammarith