Java 检查数组是否已满

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22942115/
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-08-13 19:28:42  来源:igfitidea点击:

Checking to see if array is full

javaarrays

提问by Michel Tamer

If I have an Array Candy[] type;

如果我有一个数组 Candy[] type;

and type = new Candy[capacity];

type = new Candy[capacity];

if the Array is full is capacity = type.length?

如果数组已满是capacity = type.length

采纳答案by user3437460

Since you are using array, the size of array is determined during compilation. Thus if your intention is to check whether current array's index has reached the last array element, you may use the following condtion (possibly in a loop) to check whether your current array index is the last element. If it is true, then it has reached the last element of your array.

由于您使用的是数组,因此数组的大小是在编译期间确定的。因此,如果您打算检查当前数组的索引是否已到达最后一个数组元素,您可以使用以下条件(可能在循环中)来检查您当前的数组索引是否是最后一个元素。如果为真,则它已到达数组的最后一个元素。

Example:

例子:

     int[] candy = new int[10];  //Array size is 10
     //first array: Index 0, last array index: 9. 
     for (int x=0; x < candy.length; x++)
           if (x == candy.length - 1)
                //Reached last element of array

You can check the sizeof the array by using:

您可以使用以下方法检查数组的大小

candy.length

You check whether it is last elementby using:

您可以使用以下方法检查它是否是最后一个元素

if (currentIndex == candy.length - 1) //Where candy is your array

Make sure you are using double equal==for comparison.

确保您使用双重相等==进行比较。

Single equal =is for assignment.

单个相等=用于赋值。

回答by Boris Brodski

Java creates an array with capacitycount of references to the Candyinstances initializing array with the nulls. So any array in java is full and

Java 创建一个数组,其中包含对使用s初始化数组capacityCandy实例的引用计数null。所以java中的任何数组都是满的

type.length == capacity

is always true.

永远是真的。

type = new Candy[capacity]is equivalent to

type = new Candy[capacity]相当于

type = new Candy[] {null, null, null, /* capacity count of nulls */, null};

回答by Ivaylo Strandjev

In the example you show type.lengthwill always be equal to capacity(after the initialization). Also your array will always have capacityelements but they will initially all be null.

在您显示的示例中,type.length将始终等于capacity(在初始化之后)。此外,您的数组将始终具有capacity元素,但它们最初都是null.

回答by stakahop

If you want to check if array is full (no null elements), use something like this:

如果要检查数组是否已满(没有空元素),请使用以下内容:

//1
boolean b = false;

//2
for(int i = 0; i < type.length; i++){
    if(type[i] == null){
        b = true;
    }
}

//3
if(b == false){
    System.out.println("Array is full");
}

What does it mean?

这是什么意思?

//1. At beginning you have one boolean (b) which is false.

//1. 一开始你有一个布尔值 (b) 是假的。

//2. If any value in your array is null, b will be true.

//2. 如果数组中的任何值为空,则 b 将为真。

//3. You check if value of b is still false. If it is, that means that b is never changed, so array is full (no null elements) .

//3. 您检查 b 的值是否仍然为假。如果是,那意味着 b 永远不会改变,所以数组已满(没有空元素)。