Results 1 to 7 of 7
Thread: Java looping (need help)
- 07-25-2010, 08:33 AM #1
Member
- Join Date
- Jul 2010
- Posts
- 6
- Rep Power
- 0
Java looping (need help)
Vertex v0 = new Vertex("Harrisburg");
Vertex v1 = new Vertex("Baltimore");
Vertex v2 = new Vertex("Washington");
Vertex v3 = new Vertex("Philadelphia");
Vertex v4 = new Vertex("Binghamton");
Vertex v5 = new Vertex("Allentown");
Vertex v6 = new Vertex("New York");
Is it possible to change Vertex into a for loop ??
Whereby the city name is created from v0 to v6
(part of dijkstra code)
- 07-25-2010, 08:44 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
- 07-25-2010, 08:57 AM #3
Member
- Join Date
- Jul 2010
- Posts
- 6
- Rep Power
- 0
java loop
Sorry that my question is not clear.
I want to set, say, v6 to an entirely different Vertex.
This is my class Vertex.
public class Vertex implements Comparable<Vertex>
{
public String name;
public Edge[] adjacencies;
public double minDistance = Double.POSITIVE_INFINITY;
public Vertex previous;
public Vertex(String argName){ name = argName; }
public String toString() { return name; }
public int compareTo(Vertex other)
{return Double.compare(minDistance, other.minDistance);}
}
I was looking a way to initialize Vertex using a for loop
Vertex v0 = new Vertex("Harrisburg");
Vertex v1 = new Vertex("Baltimore");
Vertex v2 = new Vertex("Washington");
Vertex v3 = new Vertex("Philadelphia");
Vertex v4 = new Vertex("Binghamton");
Vertex v5 = new Vertex("Allentown");
Vertex v6 = new Vertex("New York");
Is it possible?
- 07-25-2010, 09:15 AM #4
- 07-25-2010, 01:07 PM #5
You can not "create" variable names at run time. The must be typed in when the code is editted.Whereby the city name is created from v0 to v6
You can put the items into an array with unique indexes at runtime.
- 07-25-2010, 02:26 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
Yes it is possible; this is one way (without using a loop):
kind regards,Java Code:Vertex[] vertexes= { new Vertex("Harrigburg"), new Vertex("Baltimore"), new Vertex("Washinton") , new Vertex("Philadelphia), new Vertex("Binghamton"), new Vertex("Allentown"), new Vertex("New York") };
Jos
- 07-26-2010, 04:32 AM #7
Member
- Join Date
- Jul 2010
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
Help with While and For Looping
By gmoney8316 in forum New To JavaReplies: 2Last Post: 03-03-2010, 10:54 PM -
basic Java looping
By ace_hl in forum New To JavaReplies: 16Last Post: 09-03-2009, 01:49 AM -
Problem With Looping [JAVA]
By jude113 in forum New To JavaReplies: 2Last Post: 03-06-2009, 01:00 PM -
Java Looping and decision
By susan in forum AWT / SwingReplies: 1Last Post: 08-07-2007, 04:29 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks