Defining Arrays

0
(0)

C

C++

C#

F#

Go

Java

JavaScript

Kotlin

Rust

 

C

int numbers1[4];
int numbers2[4] = { 1, 2, 3, 5 };   // with initialization
int numbers3[] = { 1, 2, 3, 5 };     // without specifying size

C++

int numbers1[4];
int numbers2[4] { 1, 2, 3, 5 }; // with initialization
int numbers3[] { 1, 2, 3, 5 };     // without specifying size

C#

int[] numbers1;                             // array without initialization
int[] numbers2 = new int[4];                // with size and default initialization
int[] numbers3 = new int[4] { 1, 2, 3, 5 }; // with explicit initialization
int[] numbers4 = new int[] { 1, 2, 3, 5 };
int[] numbers5 = new[] { 1, 2, 3, 5 }; 
int[] numbers6 = { 1, 2, 3, 5 };

int[] numbers7 = [1, 2, 3, 5 ];             // collection expressions (starting with C# 12)
int[] numbers8 = [];                        // empty array

F#

let numbers1 = [||]                 // empty array
let numbers2 = [|1; 2; 3; 4; 5|]    // with initialization

// initialization using an expression
let numbers3 = [| for i in 1..5 -> i * i |]  // [|1; 4; 9; 16; 25|]
// initialization using Array type functions
let numbers4 = Array.create 5 1     // [|1; 1; 1; 1; 1|]
let numbers5 = Array.init 5 (fun i -> i * i)     // [|1; 4; 9; 16; 25|]
let numbers6: int array = Array.zeroCreate 5    // [|0; 0; 0; 0; 0|]

Go

package main
 
import "fmt"
 
func main() {
     
    var numbers1 []int             // empty array (slice)
    var numbers2 [5]int             // array of five int elements
    var numbers3 = [5]int{1,2,3,4,5}   
    var numbers4 = [...]int{1,2,3,4,5}   // array length is calculated automatically

    fmt.Println(numbers1)       // []
    fmt.Println(numbers2)       // [0 0 0 0 0]
    fmt.Println(numbers3)       // [1 2 3 4 5]
    fmt.Println(numbers4)       // [1 2 3 4 5]
}

Java

int[] numbers1;                             // array without initialization
int numbers2[]; 

int[] numbers3 = new int[4];                // with size and default initialization
int numbers4[] = new int[4];

int[] numbers5 = new int[] { 1, 2, 3, 5 }; // with explicit initialization
int numbers6[] = new int[] { 1, 2, 3, 5 }; // with explicit initialization

int[] numbers7 = { 1, 2, 3, 5 };
int numbers8[] = { 1, 2, 3, 5 };

int[] numbers9 = {};                        // empty array
int numbers10[] = {}; 

JavaScript

const numbers1 = [];                // empty array
const numbers2 = [1, 2, 3, 4, 5 ];
const numbers3 = new Array(1, 2, 3, 4, 5);  // using the Array constructor

Kotlin

val number1: Array<Int>   // array without initialization
val numbers2 = arrayOfNulls<Int>(4)    // with size and default initialization
val numbers3  = arrayOf(1, 2, 3, 5);   // with explicit initialization
val numbers4: Array<Int>  = arrayOf(1, 2, 3, 5);   // with explicit initialization and typing

var i = 0;
val numbers5 = Array(3, { i++ * 2}) // generation of elements based on an expression

Rust

let numbers1: [i32; 5] = [0;5];     // with type and size, initialized with a single value
let numbers2 = [1, 2, 3, 4, 5 ];    // with explicit initialization
let numbers3: [i32; 5] = [1, 2, 3, 4, 5 ];  // with explicit initialization and size


Next

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?


Explore More IT Terms


Share this term: Facebook X LinkedIn WhatsApp Email
CONTINUE LEARNING Next: Cycles →

Leave a Reply

Your email address will not be published. Required fields are marked *

For me, coding and music production are two sides of the same coin : it’s all about creating something impactful from scratch.