What happens Array is not initialized?
As the array is an important data structure for the coding perspective, there are some conditions that need to be careful. As in any language, there are two different methods to initialize the array:
- While declaring the array
- Any time after declaring the array
Shown below are the two ways of initializing an array
Initializing the array at the time of declaration:
Syntax
int arr[] = {1, 2, 3, 4, 5};
Initializing the array after declaration:
Syntax
int arr[5];
for (int i = 0; i < 5; i++)
arr[i] = i + 1;
Why is there a need to initialize an Array?
If the array is not initialized at the time of declaration or any time after that then it will contain some random values in each memory position. These random values can be of two types:
1. Default values.
1.1 If the array elements are of object types then the default value is null.
#include <iostream>
#include <vector>
int main() {
std::vector<int> array(5); // Creating a vector of size 5
// Iterate over the elements of the vector
for (int p : array) {
std::cout << p << " "; // Output each element
}
return 0;
}
//This code is contributed by Aman
import java.io.*;
class GFG {
public static void main(String[] args)
{
Integer[] array = new Integer[5];
for (Integer p : array) {
System.out.print(p + " ");
}
}
}
# code
arr = [None]*5
print(arr)
#this code is contributed by Akshay Tripathi(akshaytripathi630)
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
List<int> array = new List<int>(5); // Creating a list of size 5
// Iterate over the elements of the list
// no output would be generated in c# because array is not initaliazed with any value.
foreach (int p in array)
{
Console.Write(p + " "); // Output each element
}
// Ensure the console window stays open in debug mode
Console.ReadKey();
}
}
//This code is contributed by Prachi.
let array = new Array(5).fill(0); // Creating an array of size 5 and filling it with zeros
// Iterate over the elements of the array
array.forEach(function (p) {
process.stdout.write(p + " "); // Output each element on the same line
});
Output
0 0 0 0 0
1.2. If the array elements are of primitive data types then the default value is 0.
#include <iostream>
int main() {
int array[5] = {0}; // Initializing an array of size 5 with default values
for (int p : array) {
std::cout << p << " "; // Output each element
}
return 0;
}
import java.io.*;
class GFG {
public static void main(String[] args)
{
int[] array = new int[5];
for (int p : array) {
System.out.print(p + " ");
}
}
}
# code
arr = [0]*5
print(arr)
#this code is contributed by Akshay Tripathi(akshaytripathi630)
using System;
public class GFG
{
static public void Main (){
int[] array = new int[5];
foreach (int p in array)
{
Console.Write(p + " ");
}
}
}
// This code is contributed by akashish__
function main() {
let array = new Array(5).fill(0);
output = "";
for (let p of array) {
output += p + " ";
}
console.log(output);
}
// Run the main function
main();
Output
0 0 0 0 0
2. Garbage value:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int array[5];
for (int p : array)
cout << p << " ";
return 0;
}
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] array = new int[5];
for (int p : array)
System.out.print(p + " ");
}
}
//This code is contributed by Adarsh
if __name__ == "__main__":
array = [0] * 5
for p in array:
print p,
#this code is contributed by Adarsh
using System;
class Program
{
static void Main(string[] args)
{
int[] array = new int[5];
foreach (int p in array)
Console.Write(p + " ");
}
}
//This code is contributed by Monu.
// Main class
class Main {
// Main method
static main() {
let array = new Array(5); // Create an array of size 5
for (let p of array) // Iterate over each element in the array
console.log(p + " "); // Print the value of each element
}
}
// Call the main method
Main.main();
Output
17 0 0 0 4196464
Default values in Javascript
#include <iostream>
int main() {
int arr[5] = {0}; // Initialize array with zeros
// Iterate over the array elements
for (int x : arr) {
std::cout << x << std::endl; // Print each element
}
return 0;
}
//this code is contributed by Utkarsh.
public class Main {
public static void main(String[] args) {
int[] arr = new int[5];
// Iterate over the array elements
for (int x : arr) {
System.out.println(x); // Print each element
}
}
}
arr = [None] * 5 # Define array without initializing it
# Iterate over the array elements
for x in arr:
print(x) # Print each element
const arr = new Array(5); // Declare array without initializing it
// Iterate over the array elements
arr.forEach(x => {
console.log(x); // Print each element
});
Output:
undefined
undefined
undefined
undefined
undefined