Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Arraylist添加顶点_Java_Graph_Arraylist - Fatal编程技术网

Java 使用Arraylist添加顶点

Java 使用Arraylist添加顶点,java,graph,arraylist,Java,Graph,Arraylist,我正在尝试将顶点添加到2 arraylist(setA,setB),我将在其中存储顶点。稍后,我将使用BFS对它们进行比较,并找到最大值大小(二部图),但我无法获得输入 这是我到目前为止所拥有的 import java.util.*; public class BipartiteGraph { private String strName; ArrayList<Vertex>[] vertexList; public BipartiteGraph()

我正在尝试将顶点添加到2 arraylist(setA,setB),我将在其中存储顶点。稍后,我将使用BFS对它们进行比较,并找到最大值大小(二部图),但我无法获得输入

这是我到目前为止所拥有的

import java.util.*;



public class BipartiteGraph {

    private String strName;
    ArrayList<Vertex>[] vertexList;

    public BipartiteGraph(){

        vertexList = new ArrayList[2];
        vertexList[0] = new ArrayList<Vertex>();
        vertexList[1] = new ArrayList<Vertex>();
    }

    public boolean setA(Vertex v1){
        vertexList[0].add(v1);
        return false;
    }

    public boolean setB(Vertex v2){
        vertexList[1].add(v2);
        return false;
    }  
}

class Vertex{
    public String name;   
    public List adj; 
    public Vertex next; 
    public Vertex(String nm){
        name = nm;
        adj = new LinkedList();
      }
}

class Edge{
    public Vertex vertA;   
    public Vertex vertB;  
    public Edge( Vertex destination, Vertex comesfrom ){
        vertA = comesfrom;
        vertB = destination;
    }

    public static void main(String[] args){
      Scanner vertexInput = new Scanner(System.in);
      BipartiteGraph g = new BipartiteGraph( );
      g.setA(vertexInput.nextBoolean()); <-- I get the error here and to resolve it I tried new Vertex(.....) but that did not work as well
    } 
}
import java.util.*;
公共类二部图{
私有字符串strName;
ArrayList[]顶点列表;
公共二部图(){
vertexList=newArrayList[2];
vertexList[0]=新的ArrayList();
vertexList[1]=新的ArrayList();
}
公共布尔集(顶点v1){
顶点列表[0]。添加(v1);
返回false;
}
公共布尔setB(顶点v2){
顶点列表[1]。添加(v2);
返回false;
}  
}
类顶点{
公共字符串名称;
公开名单;
公共顶点下一步;
公共顶点(字符串nm){
名称=纳米;
adj=新链接列表();
}
}
阶级边缘{
公共顶点顶点;
公共顶点顶点b;
公共边(顶点目标,顶点从){
vertA=从中返回;
vertB=目的地;
}
公共静态void main(字符串[]args){
扫描仪垂直输入=新扫描仪(System.in);
二分图g=新的二分图();
g、 setA(vertexInput.nextBoolean());
由于
Scanner.nextBoolean()
返回布尔值(顾名思义),因此您试图使用
boolean
类型的参数调用方法setA(顶点v1)

即使你试着像这样调用这个方法

g.setA(new Vertex(String));

然后您仍然无法使用nextBoolean(),因为顶点构造函数只接受字符串参数。请尝试使用
g.setA(新顶点(vertexInput.nextLine());

几分钟前您没有问过这个问题吗?是的,我问过,但尝试了不同的方法。
g.setA(new Vertex(String));