gpt4 book ai didi

java - 如何将非基本类型数组中的所有元素值设置为值/非空?

转载 作者:行者123 更新时间:2023-12-02 04:10:27 24 4
gpt4 key购买 nike

据我所知,当您创建指针(非原始类型)时,它在被分配之前为空。对于原始类型,它的默认值为零。例如,假设我有以下代码:

int value[numberOfDesiredElements]; //all values are zero, not null
value[] == null ? return true : return false; //returns false, is not null

现在假设我使用 BigInteger 这样的非原始类型来执行此操作:

BigInteger value = new BigInteger[numberOfDesiredElements]; //All values are null, not default value.
value[anyElement] == null ? return true : return false; //Returns true, is null.

所以我的问题是,有没有办法创建一个 BigInteger 数组(或任何非基本类型数组),其所有值都等于零,或者在字符串情况下等于“”?

最佳答案

是的,你可以使用数组的 fill 方法来做到这一点,例如:

String[] str = new String[10];
Arrays.fill(str, "");//set empty value as default to each element in array

它只是在内部循环数组并将每个值默认为空字符串。

关于java - 如何将非基本类型数组中的所有元素值设置为值/非空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33844952/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com