0

3つの1次元配列を多次元配列に変換する方法はありますか。たとえば、3つの配列(テキスト、リツイート、地理)があり、それらをマージして次のように表示するにはどうすればよいですか?-

マージしたい配列は、text ='hello、'hello'の行に沿ったものです。リツイート='2,5'およびgeo='19912、929293'。

そして、次の結果になるはずです:-

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

そして1つ...すべてのアレイは同じサイズです。どういうわけかforループ中にループする必要があることはわかっていますが、それを実装する方法がよくわかりません。

どんな回答にも感謝します。

4

5 に答える 5

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];
}
于 2013-02-05T14:30:56.313 に答える
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次元配列に挿入します。

于 2013-02-05T14:31:36.947 に答える
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);
}
于 2013-02-05T14:46:26.020 に答える
1

「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を複数回呼び出したりすることができます。

于 2016-01-18T03:20:03.523 に答える
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 ]]

于 2015-05-02T02:40:36.460 に答える