0

That's weird because I wrote these two codes,bellow,they have actually the same functionality but you can see the first has error because you are declaring a TextView(that's name is:wordlist) for two times but as you can see the second code has no error while that is doing the same as the first one but in shape of while loop.

The first code:

    int index = 0;

    LinearLayout rootview = (LinearLayout) findViewById(R.id.numbers);

    TextView wordList = new TextView(this);
    wordList.setText(names.get(index));
    rootview.addView(wordList);

    index++;

    TextView wordList = new TextView(this);
    wordList.setText(names.get(index));
    rootview.addView(wordList);

The second code:

 int index = 0;
 LinearLayout rootview = (LinearLayout) findViewById(R.id.numbers);
    while(index<2) {
        TextView wordList = new TextView(this);
        wordList.setText(names.get(index));
        rootview.addView(wordList);

        index++;
    }

Can you explain what is actually happening to the second code that makes it something with no error.

2
  • 1
    Yes, it's fine to declare a variable within a loop. If you could give more details as to why you've expect it to fail in the second case, it would be easier to help you. Commented Jul 31, 2016 at 8:30
  • while that is doing the same. No it declares them once only. Not twice. There is only one variable then. Commented Jul 31, 2016 at 8:37

2 Answers 2

1

The code with the while loop declares the two wordList variables in separate scopes, so unlike the first snippet, you don't get a Duplicate local variable error.

You can consider the while loop as equivalent to :

{
    TextView wordList = new TextView(this);
    wordList.setText(names.get(index));
    rootview.addView(wordList);

    index++;
}

{
    TextView wordList = new TextView(this);
    wordList.setText(names.get(index));
    rootview.addView(wordList);

    index++;
}
Sign up to request clarification or add additional context in comments.

Comments

0

First code you declare the variable wordList twice and you simply cant do that , you will get error wordList variable is already defined in scope

Second why you repeat yourself , for loop do this for you

my advice to you dont use fix number in for loop better use length of names array

hope this help

int index = 0;
     LinearLayout rootview = (LinearLayout) findViewById(R.id.numbers);
    TextView wordList ;
        while(index<name.size()) {
            wordList = new TextView(this);
            wordList.setText(names.get(index));
            rootview.addView(wordList);

            index++;
        }

1 Comment

Please explain why you took it out of the loop. I like it more inside. Declare them where you use them.

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.