xxxxxxxxxx
int[] arr = new int[5]; // integer array of size 5 you can also change data type
xxxxxxxxxx
int[] data = {10,20,30,40,50,60,71,80,90,91};
// or
int[] data;
data = new int[] {10,20,30,40,50,60,71,80,90,91};
xxxxxxxxxx
int[] numbers = new int[5]; /* size of the array is 5 */
int[] nums = new int[] {1, 2, 3, 4, 5}; /* Fill the array ny these numbers*/
String[] names = new String[10]; /* array of type string and size 10 */
String[] countries = new String[] {"Germany", "US"};
xxxxxxxxxx
The array will be initialized to 0 if we provide the empty initializer list or just specify 0 in the initializer list.
int arr[5] = {}; // results in [0, 0, 0, 0, 0]
int arr[5] = { 0 }; // results in [0, 0, 0, 0, 0]
xxxxxxxxxx
#include <string>
#include <iterator>
#include <iostream>
#include <algorithm>
#include <array>
int main()
{
// construction uses aggregate initialization
std::array<int, 3> a1{ {1, 2, 3} }; // double-braces required in C++11 prior to
// the CWG 1270 revision (not needed in C++11
// after the revision and in C++14 and beyond)
std::array<int, 3> a2 = {1, 2, 3}; // double braces never required after =
std::array<std::string, 2> a3 = { std::string("a"), "b" };
// container operations are supported
std::sort(a1.begin(), a1.end());
std::reverse_copy(a2.begin(), a2.end(),
std::ostream_iterator<int>(std::cout, " "));
std::cout << '\n';
// ranged for loop is supported
for(const auto& s: a3)
std::cout << s << ' ';
// deduction guide for array creation (since C++17)
[[maybe_unused]] std::array a4{3.0, 1.0, 4.0}; // -> std::array<double, 3>
}
xxxxxxxxxx
Array.from({length: 10}, (x, i) => i);
// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]