Results 1 to 7 of 7
  1. #1
    FaintSmile is offline Member
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

    Default 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)

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by FaintSmile View Post
    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)
    Your question is not clear, i.e. do you want to set, say, v6 to an entirely different Vertex or do you want to change a member (state) of a Vertex? If the latter it depends on your implementation of the Vertex class.

    kind regards,

    Jos

  3. #3
    FaintSmile is offline Member
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

    Default 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?

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by FaintSmile View Post
    Is it possible?

    put your objects Vertex in a collection, example ArrayList, and then you can iterate through the collection. if you want to use a for-loop, you could also create an array of Vertex.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Whereby the city name is created from v0 to v6
    You can not "create" variable names at run time. The must be typed in when the code is editted.
    You can put the items into an array with unique indexes at runtime.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by FaintSmile View Post
    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?
    Yes it is possible; this is one way (without using a loop):

    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")
                       };
    kind regards,

    Jos

  7. #7
    FaintSmile is offline Member
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

Similar Threads

  1. Help with While and For Looping
    By gmoney8316 in forum New To Java
    Replies: 2
    Last Post: 03-03-2010, 10:54 PM
  2. basic Java looping
    By ace_hl in forum New To Java
    Replies: 16
    Last Post: 09-03-2009, 01:49 AM
  3. Problem With Looping [JAVA]
    By jude113 in forum New To Java
    Replies: 2
    Last Post: 03-06-2009, 01:00 PM
  4. Java Looping and decision
    By susan in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 04:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •