如何找到数组中值为 0 的索引?Java

huangapple 未分类评论43阅读模式
英文:

How to find indexes of array with value = 0? Java

问题

以下是翻译好的内容:

有没有办法找到数组中 0 的索引?

我是新手,所以我不太了解。

Scanner scanner = new Scanner(System.in);
System.out.println("输入数组大小 - ");
int arraySize = scanner.nextInt();

int[] array = new int[arraySize];
int a = -1;
for (int i = 0; i < arraySize; i++) {
    array[i] = a + (int) (Math.random() * 10);
}

int numberOfZeros = 0;
for (int i = 0; i < arraySize; i++) {
    if (array[i] == 0) {
        numberOfZeros++;
    }
}
if (numberOfZeros == 0) {
    System.out.print("数组中没有零!");
} else if (numberOfZeros > 0) {
    System.out.print("数组中零的个数 - " + numberOfZeros);
}
英文:

Is there any way to find indexes of 0's in my array?

Im a newbie, so i dont know much.

    Scanner scanner = new Scanner(System.in);
            System.out.println(&quot;Enter Array Size - &quot;);
            int arraySize = scanner.nextInt();
    
            int[] array = new int[arraySize];
            int a = -1;
            for (int i = 0; i &lt; arraySize; i++) {
                array[i] = a + (int) (Math.random() * 10);
            }
    
int numberOfZeros = 0;
        for (int i = 0; i &lt; arraySize; i++) {
            if (array[i] == 0) {
                numberOfZeros++;
            }
        }
        if (numberOfZeros == 0) {
            System.out.print(&quot;No Zero&#39;s in Array!&quot;);
        } else if (numberOfZeros &gt; 0) {
            System.out.print(&quot;Number of Zero&#39;s in Array - &quot; + numberOfZeros);
        }

答案1

得分: 0

只打印(或者按照你需要的方式处理)索引 i。你已经找到了零:

for (int i = 0; i < arraySize; i++) {
    if (array[i] == 0) {
        numberOfZeros++;
        System.out.print("零的值位于索引:" + i);
    }
}
英文:

Just print (or do whatever you need to do with it) index i. You already found zeros:

for (int i = 0; i &lt; arraySize; i++) {
    if (array[i] == 0) {
        numberOfZeros++;
        System.out.print(&quot;The value zero is at index: &quot; + i);
    }
}

答案2

得分: 0

你可以解析数组,并每次遇到零时记录索引:

Scanner scanner = new Scanner(System.in);
System.out.println("输入数组大小 - ");
int arraySize = scanner.nextInt();

int[] array = new int[arraySize];
int a = -1;
for (int i = 0; i < arraySize; i++) {
    array[i] = a + (int) (Math.random() * 10);
}

for (int i = 0; i < arraySize; i++) {
    if (array[i] == 0) {
        System.out.println("索引 " + i + " 处有零");
    }
}
英文:

You can parse the array , and each time you encounter a zero you can stock the index :

        Scanner scanner = new Scanner(System.in);
        System.out.println(&quot;Enter Array Size - &quot;);
        int arraySize = scanner.nextInt();

        int[] array = new int[arraySize];
        int a = -1;
        for (int i = 0; i &lt; arraySize; i++) {
            array[i] = a + (int) (Math.random() * 10);
        }
        
        for(int i=0;i&lt;arraySize;i++) {
           if ( array[i] == 0 ) {
               System.out.println(&quot; There is a zero on the index &quot;+i );
           }
        }

答案3

得分: 0

你可以使用 ArrayList 来跟踪索引,在找到零元素时将其打印出来。

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入数组大小 - ");
        int arraySize = scanner.nextInt();

        int[] array = new int[arraySize];
        int a = -1;
        int numberOfZeros = 0;
        List<Integer> indexes = new ArrayList<>();

        for (int i = 0; i < arraySize; i++) {
            array[i] = a + (int) (Math.random() * 10);
            if (array[i] == 0) {
                numberOfZeros++;
                indexes.add(i);
            }
        }

        if (numberOfZeros == 0) {
            System.out.print("数组中没有零元素!");
        } else if (numberOfZeros > 0) {
            System.out.print("数组中零元素的数量 - " + numberOfZeros + ",位置在: " + indexes);
        }
        scanner.close();
    }
英文:

You can keep track the indexes using ArrayList and print it when you find zeros

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println(&quot;Enter Array Size - &quot;);
        int arraySize = scanner.nextInt();

        int[] array = new int[arraySize];
        int a = -1;
        int numberOfZeros = 0;
        List&lt;Integer&gt; indexes = new ArrayList&lt;&gt;();

        for (int i = 0; i &lt; arraySize; i++) {
            array[i] = a + (int) (Math.random() * 10);
            if (array[i] == 0) {
                numberOfZeros++;
                indexes.add(i);
            }
        }

        if (numberOfZeros == 0) {
            System.out.print(&quot;No Zero&#39;s in Array!&quot;);
        } else if (numberOfZeros &gt; 0) {
            System.out.print(&quot;Number of Zero&#39;s in Array - &quot; + numberOfZeros + &quot; , in positions: &quot; + indexes);
        }
        scanner.close();
    }

答案4

得分: 0

只要您已经在循环遍历数组并对其进行计数,只需将它们添加到 List<Integer> 以供将来参考。

然而,如果您需要在与其他代码隔离的情况下找到它们,可以按照以下方式进行。

int[] vals = { 1,2, 0,20,33,2, 0, 99,9,10,0,4};
int[] zeroIndices = IntStream.range(0,vals.length)
          .filter(i->vals[i] == 0)
          .toArray();

System.out.println(zeroIndices.length + " 个零被找到!");
System.out.println(Arrays.toString(zeroIndices));

输出结果为

找到 3 个零!
[2, 6, 10]
英文:

As long as you are already looping thru the array and counting them, just add them to a List&lt;Integer&gt; for future reference.

However, if you need to find them in isolation of other code you can do it this way.

int[] vals = { 1,2, 0,20,33,2, 0, 99,9,10,0,4};
int[] zeroIndices = IntStream.range(0,vals.length)
          .filter(i-&gt;vals[i] == 0)
          .toArray();

System.out.println(zeroIndices.length + &quot; zeroes found!&quot;);
System.out.println(Arrays.toString(zeroIndices));

Prints

3 zeroes found!
[2, 6, 10]


</details>



huangapple
  • 本文由 发表于 2020年7月23日 21:31:20
  • 转载请务必保留本文链接:https://java.coder-hub.com/63055488.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定