java 如何停止在数组中打印空值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16522065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:12:23  来源:igfitidea点击:

How to stop printing null in an array

javaarraysnull

提问by Ibyc

How do I stop my program from printing out the positions of the array that are null, when I'm going through an array and printing the positions out?

当我遍历数组并打印出位置时,如何阻止我的程序打印出空数组的位置?

for(int i=0;i<array.length;i++)
{
  System.out.print(array[i]);
}

It's simple like this, but how do I stop it from printing out the positions that are null? Thanks in advance for an answer to this simple question...

就像这样简单,但是如何阻止它打印出空的位置?提前感谢您对这个简单问题的回答...

回答by NINCOMPOOP

for(int i=0;i<array.length;i++)
{
   if (array[i]!=null) {
     System.out.println(array[i]);
   }
}      

回答by Skillcoil

You just use the i of the for loop to check inside the array what the value is. I hope this is what you needed.

您只需使用 for 循环的 i 来检查数组内部的值是什么。我希望这是你所需要的。

for (int i = 0; i < array.length; i++)
{
  if(array[i] != null)
  {
    System.out.print(array[i]);

  }
}