简体   繁体   English

数组的Java设置值

[英]Java set value of array in position

I am trying to set value of an array element in a certain position instead of null. 我试图在某个位置而不是null设置数组元素的值。 But its not working properly and not being set accordingly. 但是它不能正常工作,也没有相应地设置。 Below is my code. 下面是我的代码。

for(int i = 0; i<5; i++)
{
    if(footballResults[i] == null)
    {
       footballResults[i] = "Game Not Started";
    }
    else
    {
       System.out.println("Game "+num+". "+footballResults[i]);
    }
    num++;
}

好吧,如果您想按已排序的顺序在设置位置打印数组,请先对数组进行排序,然后再打印元素:

   Arrays.sort(footballResults);

You provided few information, but i guess you may actually want to print the assigned value, so you need to print it outside the else statement. 您提供的信息很少,但是我想您可能实际上想打印分配的值,因此您需要在else语句之外打印它。 Hope this helps in any way. 希望这对您有帮助。

        for(int i = 0; i<5; i++)
        {
            if(footballResults[i] == null)
            {
               footballResults[i] = "Game Not Started";
            }

            System.out.println("Game "+num+". "+footballResults[i]);

            num++;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM