0

Hi everyone this is my first post and I am fairly new to Java. I did search the sites for duplicates before posting this and saw a couple that were similar but not similar enough for me to come up with a working solution from.

I am trying to construct a two-dimensional array double X[][] from two one-dimensional arrays X1[] and X2[].

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]);
}

However, my current problem is that the inputs 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

What I tried:

// 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();
}

However it threw an exception: java.lang.ArrayIndexOutOfBoundsException: 2

Any ideas, hints, or examples would be greatly appreciated. I want to learn what I am doing wrong.

Flexicoder
  • 8,251
  • 4
  • 42
  • 56
Jason R
  • 416
  • 6
  • 13
  • Hint: you want us to spend our time to help you. So you please spend the two minutes it takes to **properly** align/indent/format your input; instead of dumping such a (sorry) mess on us. – GhostCat Dec 01 '16 at 08:40
  • Take time and format your code. You obviously haven't taken time you properly display your question, if you can't be bothered to put effort into this question, why should we? – IronAces Dec 01 '16 at 08:45

1 Answers1

2
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]);
            }
        }

here you have a working example. this should help you with your problem

XtremeBaumer
  • 6,275
  • 3
  • 19
  • 65