如何给整个数组赋值0

时间:2022-09-18 21:18:09

I have an array. int[] array = new int[10];. I want to assign '0' to whole array without using loop means that 0 is to be stored in all of the indexes. How would i do it.

我有一个数组。数组=新的int[10];我想要在不使用循环的情况下将“0”分配给整个数组,这意味着0将被存储在所有的索引中。我该怎么做。

2 个解决方案

#1


13  

After creation all items of array will have default values, which is 0 for int. So, you don't need to do anything here.

创建之后,数组的所有项都将具有默认值,即int为0,所以这里不需要做任何操作。

From Arrays (C# Programming Guide):

来自数组(c#编程指南):

The default values of numeric array elements are set to zero, and reference elements are set to null.

数值数组元素的默认值设置为0,引用元素设置为null。


Also from C# Specification 12.2 Array creation

同样来自c#规范12.2数组创建

Elements of arrays created by array-creation-expressions are always initialized to their default value.

由array-creationexpression创建的数组元素总是被初始化为默认值。

5.2 Default values

5.2默认值

For a variable of a value-type, the default value is the same as the value computed by the value-type's default constructor

对于值类型的变量,默认值与值类型的默认构造函数计算的值相同

4.1.2 Default constructors

4.1.2默认构造函数

For sbyte, byte, short, ushort, int, uint, long, and ulong, the default value is 0.

对于sbyte, byte, short, ushort, int, uint, long, ulong,默认值为0。


but after assigning other values i want all the indexes to be 0 again so then how would i do it?

但是在分配其他值之后,我希望所有的索引都是0,那么我怎么做呢?

UPDATE: You can use Array.Clear:

更新:你可以使用array。

Sets a range of elements in the Array to zero, to false, or to null, depending on the element type.

根据元素类型,将数组中的元素设置为0、false或null。

In your case:

在你的例子:

Array.Clear(array, 0, array.Length);

Consider also to use List<int> instead of array - it allows to add/remove items dynamically.

还可以考虑使用List 而不是数组,它允许动态地添加/删除项目。

#2


10  

You don't need to do anything at all.

你不需要做任何事情。

Since int is a value type, all elements are initilialized to 0 as a default.

由于int是一种值类型,所以所有元素都被初始化为0作为默认值。

From Arrays (C# Programming Guide)

来自数组(c#编程指南)

The default values of numeric array elements are set to zero, and reference elements are set to null.

数值数组元素的默认值设置为0,引用元素设置为null。


ok if i want it to be assigned to 3 then how would i do it?

如果我想把它赋值为3那么我该怎么做呢?

You can use for loop to assign them like;

可以使用for循环将它们赋值为;

int[] array = new int[10];
for(int i = 0; i < array.Length; i++)
    array[i] = 3;

If you want to give back their default values (which is 0 in this case), you can create a new array or you can use Array.Clear method like;

如果您想返回它们的默认值(在本例中为0),您可以创建一个新的数组或使用数组。明确的方法;

Array.Clear(array, 0, array.Length);

If you really don't want to use any loop, you might need to use List<int> instead an array. It has more functionality and it creates a clean list (without any default value).

如果您真的不想使用任何循环,您可能需要使用List 而不是数组。它有更多的功能,并且创建了一个干净的列表(没有任何默认值)。

#1


13  

After creation all items of array will have default values, which is 0 for int. So, you don't need to do anything here.

创建之后,数组的所有项都将具有默认值,即int为0,所以这里不需要做任何操作。

From Arrays (C# Programming Guide):

来自数组(c#编程指南):

The default values of numeric array elements are set to zero, and reference elements are set to null.

数值数组元素的默认值设置为0,引用元素设置为null。


Also from C# Specification 12.2 Array creation

同样来自c#规范12.2数组创建

Elements of arrays created by array-creation-expressions are always initialized to their default value.

由array-creationexpression创建的数组元素总是被初始化为默认值。

5.2 Default values

5.2默认值

For a variable of a value-type, the default value is the same as the value computed by the value-type's default constructor

对于值类型的变量,默认值与值类型的默认构造函数计算的值相同

4.1.2 Default constructors

4.1.2默认构造函数

For sbyte, byte, short, ushort, int, uint, long, and ulong, the default value is 0.

对于sbyte, byte, short, ushort, int, uint, long, ulong,默认值为0。


but after assigning other values i want all the indexes to be 0 again so then how would i do it?

但是在分配其他值之后,我希望所有的索引都是0,那么我怎么做呢?

UPDATE: You can use Array.Clear:

更新:你可以使用array。

Sets a range of elements in the Array to zero, to false, or to null, depending on the element type.

根据元素类型,将数组中的元素设置为0、false或null。

In your case:

在你的例子:

Array.Clear(array, 0, array.Length);

Consider also to use List<int> instead of array - it allows to add/remove items dynamically.

还可以考虑使用List 而不是数组,它允许动态地添加/删除项目。

#2


10  

You don't need to do anything at all.

你不需要做任何事情。

Since int is a value type, all elements are initilialized to 0 as a default.

由于int是一种值类型,所以所有元素都被初始化为0作为默认值。

From Arrays (C# Programming Guide)

来自数组(c#编程指南)

The default values of numeric array elements are set to zero, and reference elements are set to null.

数值数组元素的默认值设置为0,引用元素设置为null。


ok if i want it to be assigned to 3 then how would i do it?

如果我想把它赋值为3那么我该怎么做呢?

You can use for loop to assign them like;

可以使用for循环将它们赋值为;

int[] array = new int[10];
for(int i = 0; i < array.Length; i++)
    array[i] = 3;

If you want to give back their default values (which is 0 in this case), you can create a new array or you can use Array.Clear method like;

如果您想返回它们的默认值(在本例中为0),您可以创建一个新的数组或使用数组。明确的方法;

Array.Clear(array, 0, array.Length);

If you really don't want to use any loop, you might need to use List<int> instead an array. It has more functionality and it creates a clean list (without any default value).

如果您真的不想使用任何循环,您可能需要使用List 而不是数组。它有更多的功能,并且创建了一个干净的列表(没有任何默认值)。