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.
while that is doing the same. No it declares them once only. Not twice. There is only one variable then.