2013-02-05 3 views
0

에는 세 개의 단일 차원 배열을 다차원 배열로 변환하는 방법이 있습니다. 예를 들어 나는 3 개의 배열 (text, retweets, geo)을 어떻게 병합하여 다음과 같이 나타낼 수 있습니까? -배열을 다차원 배열로 변환 - Java

병합하려는 배열은 'hello, hello'라는 텍스트 행을 따라 배열됩니다. retweets = '2,5'및 geo = '19912, 929293'입니다.

그리고 발생한다 : -

combined = 
[hello, 2, 19912 
hello, 5, 929293] 

그래서 하나 ...하는 모든 배열은 같은 크기이다. 나는 for 루프가 어떻게 든 진행되는 동안 반복해야한다는 것을 알고 있지만 구현하는 방법을 확실히 모르겠습니다.

응답 해 주셔서 감사합니다.

+0

나는 ArrayList의 주위에 mucking 시도했지만 아무 소용이 – user1840255

답변

1
public static void main(String[] args) { 

    String[] array1 = { "hello1", "A2", "X19912" }; 
    String[] array2 = { "hello2", "B2", "Y19912" }; 
    String[] array3 = { "hello3", "C2", "Z19912" }; 
    String[] copyArrays = new String[array1.length + array2.length 
      + array3.length]; 
    System.arraycopy(array1, 0, copyArrays, 0, array1.length); 
    System.arraycopy(array2, 0, copyArrays, array1.length, array2.length); 
    System.arraycopy(array3, 0, copyArrays, array1.length + array2.length, 
      array3.length); 

    String[][] array = new String[3][3]; 
    int index = 0; 
    for (int i = 0; i < array.length; i++) 
     for (int j = 0; j < array[i].length; j++) { 
      array[i][j] = copyArrays[index++]; 
     } 

    for (int i = 0; i < array.length; i++) { 
     for (int j = 0; j < array[i].length; j++) { 

      System.out.print(array[i][j] + " "); 
     } 
     System.out.println(); 
    } 
} 

출력 :

hello1 A2 X19912 
hello2 B2 Y19912 
hello3 C2 Z19912 

먼저 새 배열로 주어진 배열을 복사합니다이 코드. 그런 다음 for 루프를 사용하여 copyArrays의 모든 요소를 ​​2 차원 배열에 삽입합니다.

+0

안녕하세요, 제가 원했던 라인을 따라 고맙습니다. 원래 게시물에 대한 내 업데이트를 확인하십시오. 당신의 도움을 주셔서 감사합니다. – user1840255

+0

@ user1840255 내 코드를 사용해 보셨습니까? 당신은 데이터가 테이블과 같은 테이블에 있기를 원합니다. 그렇습니다? 표는 행과 열을 의미하고 내 대답은 2d를 생성합니다. 먼저 지정된 배열을 하나에 복사 한 다음 2 차원 배열에 삽입합니다. –

2
int count = ...; 

String [] text = new String [count]; 
int [] retweets = new int [count]; 
int [] geo = new int [count]; 

// Fill arrays with data here 

Object [] combined = new Object [count * 3]; 

for (int i = 0, j = 0; i < count; i++) 
{ 
    combined [j++] = text [i]; 
    combined [j++] = retweets [i]; 
    combined [j++] = geo [i]; 
} 
+1

이것은 다차원 배열이 아닙니다. – UmNyobe

+0

감사합니다. 포맷과 같은 테이블에 있습니까? – user1840255

1
String[] text =...; 
int[] retweets =...; 
int[] geo =...; 

int len = text.length; 

List<List<Object>> items = new ArrayList<>(); 
for (int i = 0; i < len; i++) { 
    List<Object> item = new ArrayList<>(); 
    item.add(text[i]); 
    item.add(retweets[i]); 
    item.add(geo[i]); 
    items.add(item); 
} 
0
String[] text = {"hello", "hello"}; 
int[] retweets = {2, 5}; 
int[] geo = {19912, 929293}; 

//create table of strings for each array 
Object[][] combined = new Object[text.length][3]; 

//load information into table, converting all information into Strings 
for(int row = 0; row<text.length; row++){ 
    combined[row][0] = text[row]; 
    combined[row][1] = retweets[row]; 
    combined[row][2] = geo[row]; 
} 

이 다음과 같아야합니다 다차원 배열 생성 :

[[hello, 2, 19912], [hello, 5, 929293 ]]

0

"toTableFormTheArrays"의 영리한 방법은 다음과 같습니다

public static String[][] to2DArray(String[]... yourArrays){ 
    return yourArrays; 
} 

then the code is: 
String[] text = {"hello", "hello"}; 
String[] retweets = {2, 5}; 
String[] geo = {19912, 929293}; 

String[][] yourTableForm2DArray = to2DArray(text, retweets, geo); 

참고 : 변경할 수 있습니다 유형, 다른 D에 대한 to2darray의 다중 호출, 어쩌면.

관련 문제