Array colorName []如何使用变量i更改值

时间:2017-10-30 18:01:10

标签: android

var CreatureStory, zombie;

function CreatureStory(species, action) {
  this.species = species;
  this.action = action;
  this.story = function(speciesNew, actionNew) {
  this.species=speciesNew;
  this.action=actionNew
  console.log('The ' +this.species +' is about to ' +this.action +'.');
  }
}

zombie = new CreatureStory('zombie', 'walk');  //variables passed in here.

zombie.story('zombieNew', 'walkNew');  // But how would I pass them in here?

colorName [i]如何使用Array更改值?

2 个答案:

答案 0 :(得分:1)

String colorName [] = {"White", "Blue", "Red"};

您正在初始化包含白色,蓝色和红色的String数组。在数组中,索引从0开始,因此White变为第0个val,Blue变为第1个,Red变为第2个元素。

现在代码:

for(int i =0; i<3 ; i++)
    {
        Log.d("Favourite Colors", "" + colorName[i]);
    }

这基本上是从0到2的循环,所以当你打印colorName[0]时,它会打印白色等等。

无论如何,你想要实现什么目标?

答案 1 :(得分:0)

您能否提供有关您在此尝试实现的更多信息?我也会像这样重写你的代码

 for(int i =0; i<colorName.length ; i++)
    {
        Log.d("Favourite Colors", "" + colorName[i]);
    }