Java 构建图的数组

Java 构建图的数组,java,directed-graph,adjacency-list,Java,Directed Graph,Adjacency List,我正在尝试实现一个有向图。我能够让我的大多数方法正常工作,但仍停留在inEdge和outEdge方法上。我知道这两种方法如何工作以及应该返回哪些数据,但问题是这些方法要求返回一个数组。因此,问题是我无法理解如何构建图形数组。基本上,outEdge方法的答案是“return list[a];”,但由于需要数组,因此无法工作。我尝试创建数组,但导致了一些问题,希望有人能提供帮助 public class Graph { /** Constructs a graph with the given n

我正在尝试实现一个有向图。我能够让我的大多数方法正常工作,但仍停留在inEdge和outEdge方法上。我知道这两种方法如何工作以及应该返回哪些数据,但问题是这些方法要求返回一个数组。因此,问题是我无法理解如何构建图形数组。基本上,outEdge方法的答案是“return list[a];”,但由于需要数组,因此无法工作。我尝试创建数组,但导致了一些问题,希望有人能提供帮助

public class Graph {

/** Constructs a graph with the given number of nodes, and no 
 * edges.
 * 
 * @param nodes Number of nodes in the graph
 */
Graph(int nodes) {

   this.nodes = nodes;
   this.edges = 0;
   list = new LinkedList[nodes];

for (int i = 0; i < nodes; i++){
    list[i] = new LinkedList<>();
}
}

/** Returns an array of all the nodes adjacent to a.
 * A node b is adjacent to a if there is an edge a → b in the graph.
 * If a is not a valid node, should return an empty array.
 * 
 * Complexity: O(E) (but you can do better)
 * @param a Source node
 * @return  Array of all nodes adjacent to a
 */
int[] outEdges(int a) {

    int[] tempresult = new int[nodes];
    int outs = 0;


    for (int i =0;i< nodes;i++){
        list[a].contains()= tempresult[outs];
        outs++;
    }

    int[] result = new int[outs];

    for (int i = 0 ;i<outs;i++){
        tempresult[outs] = result[i]; 
    }
    return result;
}

/** Returns an array of all the nodes that b is adjacent to.
 * b is adjacent to some node a if there exists an edge a → b.
 * If b is not a valid node, should return an empty array.
 * 
 * Complexity: O(N + E)
 * @param b Destination node
 * @return  Array of all nodes that b is adjacent to.
 */
int[] inEdges(int b) {

   LinkedList<Integer> edges = new LinkedList<>();
   for (int j = 0; j < n; j++)
   if (list[j].contains(b)) edges.add(j);
   return edges;
    }
公共类图{
/**构造具有给定节点数的图,并且
*边缘。
* 
*@param nodes图形中的节点数
*/
图(int节点){
this.nodes=节点;
此值为0;
列表=新链接列表[节点];
对于(int i=0;i=0);
}
}
}
对于(int a=0;a<10;++a)
对于(int b=0;b<10;++b){
如果(a!=b&&g_示例。相邻(a,b))
assertTrue(Arrays.binarySearch(g_示例.outEdges(a),b)>=0);
如果(a==b),则为else
assertFalse(Arrays.binarySearch(g_示例.outEdges(a),b)>=0);
}                
}
/**
*类图的inEdges方法测试。
*/
@试验
公共无效证明(){
系统输出打印项(“inEdges”);
对于(int i=1;i<10;++i){
int[]es=g_线.inEdges(i);
assertEquals(“只有一条边到“+i,即长度,1”);
assertEquals(“在边上到“+i+”来自“+es[0]+”,应该来自“+(i-1)+”;”,
i-1,es[0]);
es=g_循环。inEdges(i);
资产质量(如长度,1);
资产质量(es[0],i-1);
}
资产质量(g_线长度(0)、长度,0);
资产质量(g_循环。不等式(0)。长度,1);
资产质量(g_循环不等式(0)[0],9);
对于(int a=0;a<10;++a){
int[]es=g_完成。不完整项(a);
资产质量(如长度,9);
资产质量(g_离散不等式(a).长度,0);
对于(int b=0;b<10;++b){
如果(a!=b){
assertTrue(Arrays.binarySearch(es,b)>=0);
}如果(a==b),则为else{
assertFalse(Arrays.binarySearch(g_example.inEdges(a),b)>=0);
}
}
}
对于(int a=0;a<10;++a){
对于(int b=0;b<10;++b){
如果(a!=b&&g_示例。相邻(a,b)){
assertTrue(Arrays.binarySearch(g_-example.inEdges(b),a)>=0);
}如果(a==b),则为else{
assertFalse(Arrays.binarySearch(g_example.inEdges(b),a)>=0);
}
}
}
}
在inEdge方法中,我刚刚编写了我在普通linkedList中应该做的事情的代码。在我理解了如何构建图形数组之后,方法的实现将变得简单。

假设
i
的值是图形中的节点
从您的方法
outEdges

修改此项:

 for (int i =0;i< nodes;i++){
            list[a].contains()= tempresult[outs];
            outs++;
        }
for(int i=0;i

List resultList=new ArrayList();
对于(int i=0;ie.toArray();

希望这有帮助。

您可以使用
toArray
方法将
LinkedList
转换为数组。是的,但我的主要观点是从图形生成数组。。基本上,将邻接列表转换为数组,类似于我在outEdge方法中尝试的方法
 for (int i =0;i< nodes;i++){
            list[a].contains()= tempresult[outs];
            outs++;
        }
 List<Integer> resultList = new ArrayList<>();

    for (int i = 0; i < nodes; i++){   
              //a is input node 
            if(list.get(a).contains(i))
                resultList.add(i);
        }
    //returns int[]
    return resultList.stream().mapToInt(e -> e).toArray();