What does this for loop do?
for (var i = RegData.length - 1; i >= 0; i--){
var row = Titanium.UI.createTableViewRow();
var titl开发者_C百科e = Titanium.UI.createLabel({
text:RegData[i].title,
font:{fontSize:14,fontWeight:'bold'},
width:'auto',
top:2,
textAlign:'left',
left:2,
height:16
});
I want the explanation of this line... and is there any alternate ways of writing this.
for (var i = RegData.length - 1; i >= 0; i--){
This is using Titanium Appcelarator and is using JavaScript, not Java.
And apparently, its creating rows inside a table.
i
is initialized with one less then the length of some array(?) and it is decreased in every iteration. So instead of counting from 0
to n
(like in a "more common" for
loop) it counts from n-1
to 0
. The effect is that the array is looped over in reversed order.
This can be easier written with:
for (var i = RegData.length; i--; ){
It seems to create rows in a table with title in each row.
It apparently creates UI elements using the Titanium framework. This is however Javascript and not Java. Also, the loop is not complete (missing closing curly brackets).
It shows the title of the Elements of RegData in a Table
and is there any alternate ways of writing this.
for (var i = RegData.length - 1; i >= 0; i--){
// do stuff
}
sure, you can write it as an while loop, if you want. Is this more clear to you?
var i = RegData.length - 1
while(i >= 0){
// do stuff
i--
}
精彩评论