【问题标题】:In java how do I put two one-dimensional arrays into one two-dimensional array? [duplicate]在java中如何将两个一维数组放入一个二维数组中? [复制]
【发布时间】:2025-12-29 10:45:12
【问题描述】:

大家好,这是我的第一篇文章,我对 Java 还很陌生。在发布此内容之前,我确实在网站上搜索了重复项,并看到了一些相似但不足以让我想出一个可行的解决方案的夫妇。

我正在尝试从两个一维数组 X1[] 和 X2[] 构造一个二维数组 double X[][]。

之前在下面的代码中,X1[] 和 X2[] 分别在第 10 行和第 11 行被初始化:

double[] degree = new double[numNodes];
for (int id = 0; id < numNodes; id++){
    Vector neighbors = (Vector) netInfo.get(id);
    System.out.println(id+" "+neighbors+" "+neighbors.size() );
    degree[id] = neighbors.size();
}

double X1[] = new double[edgeList.size()];
double X2[] = new double[edgeList.size()];
double Y[] = new double[edgeList.size()]; //EBWC which is also 1-NOVER

for (int edgeIndex = 0; edgeIndex < edgeList.size(); edgeIndex++){
    String edge = (String) edgeList.get(edgeIndex);
    StringTokenizer stk = new StringTokenizer(edge);
    int uID = Integer.parseInt(stk.nextToken());
    int vID = Integer.parseInt(stk.nextToken());
    X1[edgeIndex] = degree[uID];
    X2[edgeIndex] = degree[vID];                                                                                  
    Vector uNeighbors = (Vector) netInfo.get(uID);
    Vector vNeighbors = (Vector) netInfo.get(vID);
    // finding the intersection
    Vector commonNeighbors = new Vector();
    for (int uindex = 0; uindex < uNeighbors.size(); uindex++){
        int uNeighbID = ( (Integer) uNeighbors.get(uindex) ).intValue();
        if (vNeighbors.contains(uNeighbID)) {
           commonNeighbors.add(uNeighbID);
        }
        // check if uNeighbID is in vNeighbors
        // if it is there, add uNeighbID to commonNeighbors
    }                                       
    // finding the union
    Vector AllNeighbors = (Vector) uNeighbors.clone();
    //Set<Integer> temp=new HashSet<Integer>();
    for(int vindex = 0; vindex < vNeighbors.size(); vindex++){
       //temp.add(i);
       int i = ( (Integer) vNeighbors.get(vindex) ).intValue();
       if (!AllNeighbors.contains(i))
           AllNeighbors.add(i);
    }
    double NOVER = 0;
    if (AllNeighbors.size() > 2)
       NOVER = ( (double) commonNeighbors.size() )/ (AllNeighbors.size()-2);

    Y[edgeIndex] = 1 - NOVER;

    // using the intersection and union, find EBWC scores for the edge uID-vID as 1-NOVER(uID, vID)

    // put uID vID and the EBWC score for the edge to the TreeMap EBWC
   System.out.println(edgeIndex+" "+X1[edgeIndex]+" "+X2[edgeIndex]+" "+Y[edgeIndex]);
}

但是,我目前的问题是输入如下:
X1 X2
0 1

0 2

1 2

1 5

2 3

2 4

3 4

3 5

4 5

4 6

5 6

5 7

6 7

我想要的输出是:

X

0 1

0 2

1 2

1 5

2 3

2 4

3 4

3 5

4 5

4 6

5 6

5 7

6 7

我尝试了什么:

// construct the X[][] two-dim array using X1[] and X2[]

double[][] X = {X1, X2};
for (int rowIndex = 0; rowIndex < edgeList.size(); rowIndex++){
    for (int colIndex = 0; colIndex < 2(); colIndex++){
        System.out.print(X[rowIndex][colIndex]+" ");
    }
    System.out.println();
}

但是它抛出了一个异常:java.lang.ArrayIndexOutOfBoundsException: 2

任何想法、提示或示例将不胜感激。我想知道我做错了什么。

【问题讨论】:

  • 提示:您希望我们花时间帮助您。因此,请您花两分钟时间正确对齐/缩进/格式化您的输入;而不是把这样一个(对不起)的烂摊子扔给我们。
  • 花时间格式化您的代码。你显然没有花时间正确地展示你的问题,如果你懒得在这个问题上付出努力,我们为什么要这样做?

标签: java arrays for-loop multidimensional-array data-structures


【解决方案1】:
int[] x1 = { 1, 2, 3, 4, 5 };
        int[] x2 = { 6, 7, 8, 9, 10 };
        int[][] y = { x1, x2 };

        for(int i=0;i<y.length;i++){
            for(int k=0;k<y[i].length;k++){
                System.out.println(y[i][k]);
            }
        }

这里有一个工作示例。这应该可以帮助您解决问题

【讨论】: