Java 如何阻止迭代器跳过第一个输入值?
我正试图打印出我的“图形”程序的深度优先遍历。 DepthFirstTraversal(intv)方法应该从第一个索引开始,这个索引应该是A。但是,它似乎跳过了这个。 有什么建议吗 我曾尝试将值v从1更改为0,但这只是在同一代码的顶部打印一个额外的0Java 如何阻止迭代器跳过第一个输入值?,java,graph,iterator,Java,Graph,Iterator,我正试图打印出我的“图形”程序的深度优先遍历。 DepthFirstTraversal(intv)方法应该从第一个索引开始,这个索引应该是A。但是,它似乎跳过了这个。 有什么建议吗 我曾尝试将值v从1更改为0,但这只是在同一代码的顶部打印一个额外的0 import java.util.Arrays; import java.util.Iterator; public class Graph { private boolean[][] edges;
import java.util.Arrays;
import java.util.Iterator;
public class Graph {
private boolean[][] edges;
private int[] labels;
private int N; //number of vertices;
//constructor. This constructor will create a matrix of size n by n.
public Graph(int n) {
N=n;
edges = new boolean[n][n];
labels = new int[n];
}
//this method will allow user to add an edge
public void addEdge(int source, int target) {
edges[source][target] = true;
}
//this method will return the label of the vertex
public int getLabel(int vertex) {
return labels[vertex];
}
//this method will allow user to remove an edge
public void removeEdge(int source, int target) {
edges[source][target] = false;
}
//this method will allow user to set a label
public void setLabels(int vertex, int newLabel) {
labels[vertex] = newLabel;
}
//this method will return the size of the labels array
public int size() {
return labels.length;
}
//this method will grab the neighbors of the desired vertex
public int[] neighbors(int vertex) {
int i;
int counter = 0;
int[] result;
for (i = 0; i < labels.length; i++) {
if (edges[vertex][i])
counter++;
}
result = new int[counter];
counter = 0;
for (i = 0; i < labels.length; i++) {
result[counter++] = i;
}
return result;
}
//this method will print out the vertices starting from the first value
I tried fixing my code a little, but I ran into another problem.
I do have to use neighbors method and also I cannot use recursion.
public void DepthFirstTraversal(int v) {
boolean[] visited = new boolean[N];
Stack<Integer> stack = new Stack<>();
stack.add(v);
visited[v]=true;
while(!stack.isEmpty()){
int i = stack.pop();
if (i == 1) {
System.out.print("A" + "-");
} else if (i == 2) {
System.out.print("B" + "-");
} else if (i == 3) {
System.out.print("C" + "-");
} else if (i == 4) {
System.out.print("D" + "-");
} else if (i == 5) {
System.out.print("E" + "-");
} else if (i == 6) {
System.out.print("F" + "-");
} else if (i == 7) {
System.out.print("G" + "-");
} else if (i == 8) {
System.out.print("H" + "-");
} else if (i == 9) {
System.out.print("I" + "-");
}
System.out.print(labels[i] + " \n");
int[] neighborsOfI = neighbors(i);
for(int k=0;k<neighborsOfI.length;k++){
int neighborTest = neighborsOfI[k];
if(!visited[neighborTest]){
stack.add(neighborTest);
visited[neighborTest]=true;
}
}
}
}
}
public class graphDemo {
public static void main(String args[]){
Graph graph = new Graph(10);
graph.addEdge(1,1);
graph.addEdge(2,3);
graph.addEdge(3,5);
graph.addEdge(4,7);
graph.addEdge(5,9);
graph.addEdge(6,2);
graph.addEdge(7,3);
graph.addEdge(8,5);
graph.addEdge(9,8);
graph.setLabels(1,9);
graph.setLabels(2,3);
graph.setLabels(3,5);
graph.setLabels(4,7);
graph.setLabels(5,4);
graph.setLabels(6,8);
graph.setLabels(7,6);
graph.setLabels(8,2);
graph.setLabels(9,1);
System.out.println("Depth First Traversal from first value: \n");
graph.DepthFirstTraversal(1);
}
}
导入java.util.array;
导入java.util.Iterator;
公共类图{
私有布尔[][]边;
私有int[]标签;
private int N;//顶点数;
//构造函数。此构造函数将创建大小为n×n的矩阵。
公共图(int n){
N=N;
边=新布尔[n][n];
标签=新整数[n];
}
//此方法将允许用户添加边
公共无效添加(整数源,整数目标){
边[源][目标]=真;
}
//此方法将返回顶点的标签
公共整型getLabel(整型顶点){
返回标签[顶点];
}
//此方法将允许用户删除边
public void removedge(int源、int目标){
边[源][目标]=假;
}
//此方法允许用户设置标签
公共void集合标签(int-vertex,int-newLabel){
标签[顶点]=新标签;
}
//此方法将返回标签数组的大小
公共整数大小(){
返回标签长度;
}
//此方法将获取所需顶点的邻域
公共int[]邻域(int顶点){
int i;
int计数器=0;
int[]结果;
对于(i=0;i 我试着修改一下代码,但遇到了另一个问题。
我必须使用邻域方法,也不能使用递归。
公共空间深度第一次遍历(int v){
boolean[]访问=新的boolean[N];
堆栈=新堆栈();
堆栈。添加(v);
访问[v]=正确;
而(!stack.isEmpty()){
int i=stack.pop();
如果(i==1){
系统输出打印(“A”+“-”);
}else如果(i==2){
系统输出打印(“B”+“-”);
}else如果(i==3){
系统输出打印(“C”+“-”);
}else如果(i==4){
系统输出打印(“D”+“-”);
}else如果(i==5){
系统输出打印(“E”+“-”);
}else如果(i==6){
系统输出打印(“F”+“-”);
}else如果(i==7){
系统输出打印(“G”+“-”);
}else如果(i==8){
系统输出打印(“H”+“-”);
}else如果(i==9){
系统输出打印(“I”+“-”);
}
系统输出打印(标签[i]+“\n”);
int[]neighborsOfI=邻居(i);
for(int k=0;kJava是0索引的,这意味着数组从索引0开始。您创建了一个大小为10的数组,但使用了后9个条目。我更改了您的输入数据以匹配此项,但这不是问题所在
public class GraphDemo {
public static void main(String args[]) {
Graph graph = new Graph(9);
graph.addEdge(0, 0);
graph.addEdge(1, 2);
graph.addEdge(2, 4);
graph.addEdge(3, 6);
graph.addEdge(4, 8);
graph.addEdge(5, 1);
graph.addEdge(6, 2);
graph.addEdge(7, 4);
graph.addEdge(8, 7);
graph.setLabels(0,9);
graph.setLabels(1,3);
graph.setLabels(2,5);
graph.setLabels(3,7);
graph.setLabels(4,4);
graph.setLabels(5,8);
graph.setLabels(6,6);
graph.setLabels(7,2);
graph.setLabels(8,1);
System.out.println("Depth First Traversal from first value: \n");
graph.depthFirstTraversal(1);
}
}
我不确定标签是什么或是什么意思,所以我完全忽略了它们以及遍历这些标签的邻居。以下是两种方式中的深度优先遍历:
private void depthFirstTraversal(int v, boolean[] visitedIndexes) {
if (visitedIndexes[v]) {
System.out.println("Arrived at index " + v +
" with letter " + (char) ('A' + v) +
" but we've already been here, so skipping this.");
return;
}
System.out.println("Traversing index " + v +
" which has label " + labels[v] +
" and here's some letter " + (char) ('A' + v)
);
visitedIndexes[v] = true;
for (int i = 0; i < n; i++) {
if (edges[v][i]) {
depthFirstTraversal(i, visitedIndexes);
}
}
}
private void depthFirstTraversal(int v,boolean[]visitedIndexes){
如果(访问索引[v]){
System.out.println(“到达索引”+v+
带字母“+(字符)('A'+v)+
“但我们已经到了这里,所以跳过这个。”);
返回;
}
System.out.println(“遍历索引”+v+
“其中有标签”+标签[v]+
这是一个字母“+(char)('A'+v)
);
visitedIndexes[v]=真;
对于(int i=0;i
有几件事;v
的值根本不被使用,所以改变它也没什么帮助。我们看不到addEdge
和setLabels
会做什么,同时我们也不知道labels
或神秘的大写变量N
中有什么,这意味着我们无法决定这段代码将做什么我为你做。如果我们需要帮助,我建议发布完整的代码。@CXgamer刚刚修复了它。很抱歉,我甚至没有意识到。我只是注意到我甚至没有做深度优先遍历…你能告诉我如何从A开始吗?我想它会帮我找到一个地方start@CXgamer我编辑了我的代码,现在它做了一个深度优先的旅行rsal。我不能使用递归,我必须使用邻居方法。我认为它开始工作了,但它只运行到第二个索引。有什么建议吗?我尝试过修复代码,但遇到了另一个问题。它在前两个索引之后停止。