简体   繁体   English

从输入文件图Java添加顶点

[英]Add Vertex From Input File Graph Java

I am attempting to implement a graph in Java. 我正在尝试用Java实现图形。 I want to add vertex from the input file. 我想从输入文件添加顶点。 Here is my code: 这是我的代码:

Scanner input = new Scanner(System.in);

try
{
    ArrayList<String> Alist= new ArrayList <String>();
    FileReader fr= new FileReader("/home/vanhook/Desktop/city.txt");
    BufferedReader reader= new BufferedReader(fr);

    String line;
    while ((line=reader.readLine()) !=null)
    {
        //Add to ArrayList
        Alist.add(line);            
    }

    //print the city
    for (int z = 0; z < 7; z++)
    {
        System.out.println(Alist.get(z));
    }

    /*
    Here is the code to add vertex manually 
    Graph graphNew = new Graph();
    graphNew.insertVertex("Jakarta");           //0
    graphNew.insertVertex("Bandung");           //1
    graphNew.insertVertex("Semarang");          //2
    graphNew.insertVertex("Surabaya");          //3
    graphNew.insertVertex("Madura");            //4
    graphNew.insertVertex("Bali");              //5
     */

Here is the city.txt: 这是city.txt:

Jakarta
Bandung
Semarang
Surabaya
Madura
Bali

What is the code to replace manually insert vertex with the input file? 用输入文件替换手动插入顶点的代码是什么? I try to use looping like this: 我尝试使用像这样的循环:

for (int z = 1; z < 7; z++)
{
    graphNew.inserVertex(Alist.get(z));
}

but I got an error. 但我有一个错误。

for (z=1;z<7;z++){
graphNew.inserVertex(Alist.get(z));
}

Indices in java are 0 based, not 1 based. Java中的索引基于0,而不是基于1。

That means the first index is 0 , and the last index (you can access) is Alist.length() - 1 , which is 5 in your case. 这意味着第一个索引为0 ,最后一个索引(您可以访问)为Alist.length() - 1 ,在您的情况下为5

As side notes: 作为旁注:

  1. You should prefer a for each loop over an old style index loop: 与旧式索引循环相比,您应该更喜欢for each循环:

    for (String city : Alist) graphNew.inserVertex(city); 对于(String city:Alist)graphNew.inserVertex(city);

  2. variables in java start with lower case letter ( aList instead of Alist ). Java中的变量以小写字母开头( aList而不是Alist )。 This is not enforced by compiler, but is a convention. 这不是由编译器强制执行的,而是一种约定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 图形数据从边输入格式转换为顶点输入格式 - Conversion of graph data from Edge Input Format to Vertex Input Format Gremlin 通过 java 读取外部 JSON 顶点并添加到现有的开放图会给出无效的顶点提供异常? - Gremlin Through java reading external JSON vertex and add to existing open graph gives Invalid vertex provided exception? 在 java 中的图中创建距起始顶点的距离数组 - Creating an array of distances from a starting vertex in a graph in java 无向图添加/删除顶点; removeEdge方法 - Undirected Graph add/remove Vertex; removeEdge methods 在OrientDB的事务图中向框架添加类型化的顶点 - Add typed Vertex with Frames in transactional graph in OrientDB 使用Java在顶点标签上的DSE图索引 - Dse graph indexing on vertex label using Java JUNG(Java Graph):如何防止顶点和边缘标签重叠? - JUNG (Java Graph): How to prevent Vertex- and Edge-Labels from overlapping? 从Java中的文件输入 - input from file in java 图从单个顶点遍历到N个顶点 - Graph Traversing From Single Vertex to N Vertices 如何从包含顶点和边的文本文件创建图形? - How can I create a graph from Text File containing the vertex and edges?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM