How To Zero An Array

Table of contents:

How To Zero An Array
How To Zero An Array

Video: How To Zero An Array

Video: How To Zero An Array
Video: Shift All Zero Elements to Right Side of the Array - Java interview Question 2024, December
Anonim

Arrays are one of the required attributes of any programming language. Therefore, there is hardly a programmer who has never used them in his software products. They make it easier to create them. Various operations with arrays sometimes take up most of the program code. One of such operations is its zeroing.

How to zero an array
How to zero an array

Instructions

Step 1

Zero array in C / C ++. When initializing an array in C and C ++, the array elements are assigned a random value, unlike, for example, languages such as C # or Java. Therefore, there is no reason to hope that all elements will be equal to a certain value. For C and C ++, there are several ways to zero out arrays. To do this, when creating it, use the following code: int array [10000]; memset (array, 0, 10000); This code will create an array of 10000 elements and assign each element the value 0. Also, to create an array of zeros during initialization, use the simpler code: int array [100] = {0}; This code will create an array of 100 elements and assign all elements to 0. To zero out the array, use the loops: i = 0; for (i; i

Zero array in Java. Unlike C / C ++ in Java, when initializing an array as a class variable, all elements are immediately assigned a value equal to: 0 - if it is an array of numbers, false - if it is an array of boolean variables, null - if it is an array of objects. Therefore, in Java, you should not manually zero an array during initialization. But, if you create an array not as a class variable, but declare it in the body of a function or loop, then the compiler does not guarantee that all values will be equal to 0 (false, null). In this case, use the following loop to zero out the array: int array = new int [10000]; // create an array with 10000 elements for (int i = 0; i

Step 2

Zero an array in Java. Unlike C / C ++ in Java, when initializing an array as a class variable, all elements are immediately assigned a value equal to: 0 - if it is an array of numbers, false - if it is an array of boolean variables, null - if it is an array of objects. Therefore, in Java, you should not manually zero an array during initialization. But, if you create an array not as a class variable, but declare it in the body of a function or loop, then the compiler does not guarantee that all values will be equal to 0 (false, null). In this case, use the following loop to zero out the array: int array = new int [10000]; // create an array with 10000 elements for (int i = 0; i

Recommended: