如何在java中将两个一维数组放入一个二维数组? [英] How do I put two one-dimensional arrays into one two-dimensional array in java?
问题描述
我正在尝试从两个一维数组X1 []和X2 []构建一个二维数组double X [] []。
输入如下:
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
然而它引发了异常:java.lang.ArrayIndexOutOfBoundsException:2
我们将非常感谢您的想法,提示或示例。我想知道我做错了什么。
我的尝试:
这里有一段代码,显示了数组的实现和一些上下文:
以前 下面的代码 X1 []和X2 [已初始化 行 10 和行 11 分别为:
double []度= new double [numNodes];
for ( int id = < span class =code-digit> 0 ; id < numNodes; id ++){
Vector neighbors =(Vector )netInfo。 get (id);
系统。 out .println(id + + neighbors + + neighbors.size());
degree [id] = neighbors.size();
}
double X1 [ ] = new double [edgeList.size()];
double X2 [] = new 双跨度> [edgeList.size()];
double Y [] = new 双跨度> [edgeList.size()]; // EBWC也是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] =度[uID];
X2 [edgeIndex] =度[vID];
向量uNeighbors =(向量)netInfo。 get (uID) ;
向量vNeighbors =(向量)netInfo。 get (vID);
// 找到交叉点
Vector commonNeighbors = new Vector();
for ( int uindex = < span class =code-digit> 0 ; uindex < uNeighbors.size(); uindex ++){
int uNeighbID =((整数)uNeighbors。 get (uindex))。的intValue();
if (vNeighbors.contains(uNeighbID)){
commonNeighbors。 add (uNeighbID);
}
// < span class =code-comment>检查uNeighbID是否在vNeighbors
// 如果它在那里,将uNeighbID添加到commonNeighbors
}
// 查找联合
Vector AllNeighbors =(Vector)uNeighbors.clone();
// Set< Integer> temp = new HashSet< Integer>();
for ( int vindex = 0 ; vindex < vNeighbors.size(); vindex ++){
// temp.add(i);
int i =((整数)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;
// 使用交集和联合,找到边缘uID-vID的EBWC分数为1-NOVER(uID,vID)
// 放入uID vID和边界到EBMap的EBWC得分EBWC
系统。 out .println(edgeIndex + + X1 [edgeIndex] + + X2 [edgeIndex] + + Y [edgeIndex]);
}
//使用X1 []和X2构造X [] []二维数组[/]
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();
}
很难读取这样的非格式化代码!
使用List / ArrayList来放置你的二维数组项,然后从中提供一个数组。
关注:
java - 将2D数组转换为1D数组 - Stack Overflow [ ^ ]
如何在Java中将二维数组转换为一维数组 - Stack Overflow [ ^ ]
public class Main {
public static void main( String [] args){
int [] x1 = { 1 , 2 , 3 跨度>};
int [] x2 = { 4 , 5 , 6 };
int [] [] x = new int [ 3 ] [ 2 ];
for ( int i = 0 ; i< x1.length; i ++){
x [i] [ 0 ] = x1 [i];
}
for ( int i = 0 ; i< x2.length; i ++){
x [i] [ 1 ] = x2 [i] ;
}
for ( int i = 0 ; i < x.length; i ++){
for ( int j = 0 ; j < x [i] .length; j ++){
System。 out .print(x [i] [j ] + );
}
系统。 out .println();
}
}
}
该示例将转换两个1D数组:
x1:1,2,3
x2:4,5,6
到2D阵列:
x:
1 4
2 5
3 6
注意一些循环代码块可以变成一种更好地重复使用的方法。那是你要思考的问题。
I am trying to construct a two-dimensional array double X[][] from two one-dimensional arrays X1[] and X2[].
Input is as follows:
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
And my desired output is:
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
However it threw an exception: java.lang.ArrayIndexOutOfBoundsException: 2
And ideas, hints, or examples would be greatly appreciated. I want to learn what I am doing wrong.
What I have tried:
Here is a snippet of code that shows the implementation of the arrays and some context:
Previously in the code below is how X1[] and X2[] were initialized in line 10 and line 11 respectively:
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]);
}
// 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();
}
Its very hard to read such a non-formatted code!
Use a List/ArrayList to put your 2-dimensional array items and then feed an array from it.
Follow:
java - Convert a 2D array into a 1D array - Stack Overflow[^]
How can you convert a 2 dimensional array into a 1 dimensional array in Java - Stack Overflow[^]
Check out the following code snippet and adapt to your requirement:
public class Main { public static void main(String[] args) { int[] x1 = {1,2,3}; int[] x2 = {4,5,6}; int[][] x = new int[3][2]; for (int i=0;i<x1.length;i++){ x[i][0]=x1[i]; } for (int i=0;i<x2.length;i++){ x[i][1]=x2[i]; } for (int i = 0; i < x.length; i++) { for (int j = 0; j < x[i].length; j++) { System.out.print(x[i][j] + " "); } System.out.println(); } } }
The example turns two 1D array:
x1: 1,2,3 x2: 4,5,6
into a 2D array:
x: 1 4 2 5 3 6
Notice some of the looping code blocks can be turned into a method for better re-use. That is for you to ponder.
这篇关于如何在java中将两个一维数组放入一个二维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!