0

If I have variable1, variable2 and variable 3. Is there anyway to get that variable with i?

Such as getting variablei , where i = 1.

Following code I think illustrates what I am talking about.

    ArrayList networkSetupData = new ArrayList(count);
    for(int i =0 ; i != count ; count--)
    {
        networkSetupData.add(dropDown[i]);
    }

Thank you in advance.

4
  • 6
    It's amazing how you solved your own problem in that code sample. Commented Nov 22, 2011 at 18:51
  • in this for loop you continuously add the same value while count != 0. Why do you need such a lot of code. There is a method Collections.nCopies(int n, Object o) which return List of n objects Commented Nov 22, 2011 at 19:02
  • @Martin: no, he simply adds value at 0 position of dropDown Commented Nov 22, 2011 at 19:03
  • > new ArrayList(count); This set a capacity of List, but not its size. Be careful. Commented Nov 22, 2011 at 19:11

1 Answer 1

4

You should basically restructure your code to have a single array or collection variable instead. You've got three variables which are clearly related, and which you want to access by index: you want a collection.

Instead of declaring dropDown1, dropDown2, dropDown3, you should declare dropDowns either as a List<DropDown> or possibly a DropDown[]. Then you can access them by index at any time.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, you are correct I can rewrite that section to use an array. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.