qkslvrwolf
Programmer
I'm having a weird scoping issue where I add a new instantiation of a class (edge) to a Vector (edges). However, no matter how many layers of newness I add to the edge that I add, it creates a pointer to the same object...so I get a Vector with a bunch of copies of the last object that I add...
while (line != null)
{
StringTokenizer makeAToken = new StringTokenizer(line);
int w = Integer.valueOf(makeAToken.nextToken().trim()).intValue();
int n1 = Integer.valueOf(makeAToken.nextToken().trim()).intValue();
int n2 = Integer.valueOf(makeAToken.nextToken().trim()).intValue();
edge newEdge = new edge(w, n1, n2);
System.out.println(numEdges);
System.out.println(newEdge.weight);
addEdge(newEdge);
System.out.println( ((edge)edges.elementAt(0)).weight);
numEdges++;
line = r.readLine();
}
public static void addEdge(edge e1)
{
edge toAdd = new edge(e1);
edges.addElement(toAdd);
}
I was originally doing just edges.addElement(new edge(w, n1, n2)
(edge has 2 contructors, now)
but that was giving me the same issue.
Whats going on? Please help....
while (line != null)
{
StringTokenizer makeAToken = new StringTokenizer(line);
int w = Integer.valueOf(makeAToken.nextToken().trim()).intValue();
int n1 = Integer.valueOf(makeAToken.nextToken().trim()).intValue();
int n2 = Integer.valueOf(makeAToken.nextToken().trim()).intValue();
edge newEdge = new edge(w, n1, n2);
System.out.println(numEdges);
System.out.println(newEdge.weight);
addEdge(newEdge);
System.out.println( ((edge)edges.elementAt(0)).weight);
numEdges++;
line = r.readLine();
}
public static void addEdge(edge e1)
{
edge toAdd = new edge(e1);
edges.addElement(toAdd);
}
I was originally doing just edges.addElement(new edge(w, n1, n2)
(edge has 2 contructors, now)
but that was giving me the same issue.
Whats going on? Please help....