Results 1 to 6 of 6
  1. #1
    jack DANIEL's is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

    Default Naming variables inside a cicle

    Hello everybody,

    I've got a quite simple question to ask you. I need to declare a bunch of variables inside a "for" cicle, and their names have to depend from the value of the parameter of the cicle.

    I post this code to better explain what I'm asking (obviously it doesn't work, and it's the part in bold font that I need to correct, I suppose):

    for (int k=0; k<toLink.capacity(); k++) {
    Connection link + k = new Connection (....);
    }
    So, I mean that, when k=0 it must create the variable "link0", with k=1 "link1" and so on?

    Is it possible to do such a thing, guess right?

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Why not use an array?
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    yep, I second the recommendation above if the variables need to be referenced by an index number. Or use an ArrayList if you don't know in advance how many objects will be added to the collection.

  4. #4
    jack DANIEL's is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

    Default

    You're right guys. I solved the problem

    I'm in front of the screen since this morning trying to complete my project and I was about to go crazy. I was sure that there was an easier solution to my problem: the one you proposed me. Thank you so much for your helpfulness :)

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default

    I love the term "for circles". I'm going to start calling them that.

    One time I tried to teach a girlfriend some basic programming. She thought it was spelled "forloops" because apparently I say it as one word. It was adorable.

  6. #6
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    I think the OP misspelled "cycle".
    Ever seen a dog chase its tail? Now that's an infinite loop.

Similar Threads

  1. Naming Variables
    By counterfox in forum New To Java
    Replies: 3
    Last Post: 05-07-2010, 04:40 PM
  2. Variable Naming
    By 67726e in forum Advanced Java
    Replies: 2
    Last Post: 03-30-2010, 08:55 AM
  3. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 05:48 PM
  4. RMI naming problem
    By Robbinz in forum New To Java
    Replies: 1
    Last Post: 12-06-2007, 11:32 PM
  5. Naming conventions
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 09:53 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
  •