How to get array method from another array method from the other class

My apologize, I have a class on my Project, called test01.java. And i used the library from Tadaki Graphlib contained many class. On of them is Graph.java.

Test01.java:

    public class test01 extends Graph{

public test01(String name, int n) {
    super(name);
public test01(String name, int n) {
    super(name);

    graphLib.Vertex vList[] = new graphLib.Vertex[n];
    for (int i = 0; i < n; i++) {
        vList[i] = new graphLib.Vertex(String.valueOf(i));
        addVertex(vList[i]);
    }
    int deg = 0;
    System.out.println("<---------- Random val ---------->");
     addArc(vList[0], vList[1], String.valueOf(0));   deg++;
     addArc(vList[1], vList[0], String.valueOf(1));   deg++;
     System.out.println("Vertex-0 with Vertex-1");
     System.out.println("Vertex-1 with Vertex-0");
    int k = 2;
    int l;
    int m=0;
    Random randomval = new Random();
    int isAvailInt [] = new int[n];
    while (k<n) {
        for(l=0;l<k;l++){
                isAvailInt [l]= Integer.parseInt(vList[l].toString());
                m=isAvailInt[l];            
        }
        int chosen = randomval.nextInt(m);
        addArc(vList[k], vList[chosen], String.valueOf(k));
        System.out.println("Vertex-"+k+" with Vertex-"+chosen+
                " exp = " + String.valueOf(k));

        k++;
    }

}public static void main(String args[]) {
    int n;
    String num = JOptionPane.showInputDialog("Masukkan nilai jumlah iterasi = ");
    String degnum = null;        
    n = Integer.parseInt(num);
    int deg []= new int [n];
    test01 t = new test01("test",n);

    System.out.println("<---------- Vertex-i = Degree-i ------------>");
    graphLib.Graph g= new Graph("test");

    int [][]adj = g.getAdjacent();

    System.out.println(adj[0][0]);
    for (int i=0; i<t.getSize();i++){
        for (int j=0; j<t.getSize();j++){
        }          
    }}

and one other class called Graph.java

public class Graph extends GraphBase { int adjacent[][] = null;

public Graph(String name) {
    this.name = name;
    vertexes = Utils.createVertexList();
    arcs = Utils.createArcList();
    a2vHead = new HashMap<>();
    a2vTail = new HashMap<>();
    v2a = new HashMap<>();
}
public int[][] getAdjacent() {
    int n = vertexes.size();
    adjacent = new int[n][];
    for (int i = 0; i < n; i++) {
        adjacent[i] = new int[n];
        for (int j = 0; j < n; j++) {
            adjacent[i][j] = 0;
        }
    }
    if (directed) {
        for (int i = 0; i < n; i++) {
            Vertex v = vertexes.get(i);
            for (Arc a : v2a.get(v)) {
                Vertex t = a2vTail.get(a);
                int l = vertexes.indexOf(t);
                adjacent[l][i]++;
            }
        }
    } else {
        for (int i = 0; i < n; i++) {
            Vertex v = vertexes.get(i);
            for (Arc a : v2a.get(v)) {
                Vertex t = a2vTail.get(a);
                if (!t.equals(v)) {
                    int l = vertexes.indexOf(t);
                    adjacent[i][l]++;
                    adjacent[l][i]++;
                }
            }
        }

    }
    checkConnectedness();
    return adjacent;
}}

From above, method - int [][] Adjacent() - has an array return value:

return adjacent;

Then I want to received it with array variable declared:

int [][]adj = g.getAdjacent();

But when I run the program, the code :

System.out.println(adj[0][0]);

Has appeared error :

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

I've declare the variable vertexes in Graph.java that extended from other class, GraphBase.java:

 vertexes = Utils.createVertexList();

How do I obtain an array value form a variable adjacent in Graph.java to test01.java and how do I display it with System.out.println() ?

Jon Skeet
people
quotationmark

Well you haven't shown where vertexes is initialized (or even declared) in Graph. I suspect it's empty, so when you execute this code:

public int[][] getAdjacent() {
    int n = vertexes.size();
    adjacent = new int[n][];
    ...
    return adjacent;
}

... you'll end up with an empty array. That would cause the problem you've seen. You can easily check the size in your main method:

System.out.println(adj.length);

I suspect you'll find it's 0. Either that, or adj[0].length is 0.

It's not clear how you expect the Graph to find any vertexes - you don't supply it with any, or even the value of n. You just call the constructor with a string:

graphLib.Graph g= new Graph("test");

Unless that's meant to be the name of a file which is loaded in the constructor, there's nowhere for it to get data from. You need to take a step back and think about where you expect the data to come from, then make sure that it can actually flow through your program. The problem isn't getting the array reference back to main - the problem is that the array is empty.

people

See more on this question at Stackoverflow