java - 如何连接邻接列表中的 2 个顶点?
问题描述
我正在尝试学习不同的数据结构,我目前正在努力学习图表。
我正在尝试使用链接列表映射来实现邻接列表。这使我能够搜索顶点的名称并快速查看其所有连接。
但我在实际添加任何连接时遇到了麻烦。我目前正在做的是
public void add(V vertexName) {
if (!contains(vertexName)) {
ArrayList<V> array = new ArrayList<>();
adjList.put(vertexName,array);
}
}
public void connect(V start, V destination) {
if (!contains(start) || !contains(destination))
throw new NoSuchElementException();
adjList.get(start).add(destination); //adds destination to starts arraylist
}
System.out.print(adjList.toString);
但是,例如,如果我将 12、10 和 20 添加到我的列表中,然后连接 20 和 12,我仍然会得到:
12
10
20
代替
12
10
20 - > 12
任何提示将不胜感激。如果有帮助,adjList 是一个
Map<V,ArrayList<V>> adjList;
我只是想从一个有向未加权图开始,以使事情变得更容易
解决方案
推荐阅读
- swift - 类变量有类本身是什么设计模式
- angular - Auth Guard canActivate 由于某种原因无法正常工作
- javascript - 在 React (v15) 的 render() 中访问 DOM
- java - 哪些方法可用于从 Java 文件中返回有效和无效的 XML 数据?
- nutch - 将 Nutch 爬网数据索引到 Elasticsearch 时出错
- javascript - Webpack 解析导入库的外部
- java - 如何仅打印字符串的某些部分?
- r - 使用 Rcpp 重写 R 的 cummin() 函数并允许 NA
- monitoring - Prometheus 增加不处理进程重启
- angular - 在标签或跨度中使用 formControlName?