Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Multi-Dimensional Arrays | Arrays and Slices
Introduction to GoLang
course content

Contenuti del Corso

Introduction to GoLang

Introduction to GoLang

1. Getting Started
2. Data Types
3. Control Structures
4. Functions
5. Arrays and Slices
6. Intro to Structs & Maps

book
Multi-Dimensional Arrays

A multidimensional array is an array that includes other arrays as its elements, forming a matrix or a higher-dimensional structure.

We can create 2D, 3D, and 4D arrays using the following format:

For example, we can create a 2x2 matrix using the following code:

index.go

index.go

copy
1
var matrix [2][2] int

We can also initialize it with some data using the following syntax:

index.go

index.go

copy
12
var numbers = [2][2] int { { 1, 2 }, { 3, 4} } fmt.Println(numbers) // Output: [[1 2] [3 4]]

We can access and modify the elements of a multidimensional array using indexing by specifying both the row and column's index of the element:

index.go

index.go

copy
1234
var numbers = [2][2] int { { 1, 2 }, { 3, 4} } fmt.Println(numbers) // Output: [[1 2] [3 4]] numbers[1][0] = 5 fmt.Println(numbers) // Output: [[1 2] [5 4]]

Following is an illustration of a 2D array with 9 elements:

An array can have as many dimensions as needed. Following is an example of a 5-dimensional array:

index.go

index.go

copy
1
var numbers [2][3][4][5][2] int

The above array will have 240 elements based on the calculation 2 x 3 x 4 x 5 x 2 = 240. The following code shows the declaration and initialization of a 3D array called `numbers:

index.go

index.go

copy
1234
var numbers = [2][3][2] int { { {1, 2}, {3, 4}, {5, 6} }, { {7, 8}, {9, 10}, {11, 12} }, }
question-icon

Click or drag`n`drop items and fill in the blanks

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 5. Capitolo 5

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

course content

Contenuti del Corso

Introduction to GoLang

Introduction to GoLang

1. Getting Started
2. Data Types
3. Control Structures
4. Functions
5. Arrays and Slices
6. Intro to Structs & Maps

book
Multi-Dimensional Arrays

A multidimensional array is an array that includes other arrays as its elements, forming a matrix or a higher-dimensional structure.

We can create 2D, 3D, and 4D arrays using the following format:

For example, we can create a 2x2 matrix using the following code:

index.go

index.go

copy
1
var matrix [2][2] int

We can also initialize it with some data using the following syntax:

index.go

index.go

copy
12
var numbers = [2][2] int { { 1, 2 }, { 3, 4} } fmt.Println(numbers) // Output: [[1 2] [3 4]]

We can access and modify the elements of a multidimensional array using indexing by specifying both the row and column's index of the element:

index.go

index.go

copy
1234
var numbers = [2][2] int { { 1, 2 }, { 3, 4} } fmt.Println(numbers) // Output: [[1 2] [3 4]] numbers[1][0] = 5 fmt.Println(numbers) // Output: [[1 2] [5 4]]

Following is an illustration of a 2D array with 9 elements:

An array can have as many dimensions as needed. Following is an example of a 5-dimensional array:

index.go

index.go

copy
1
var numbers [2][3][4][5][2] int

The above array will have 240 elements based on the calculation 2 x 3 x 4 x 5 x 2 = 240. The following code shows the declaration and initialization of a 3D array called `numbers:

index.go

index.go

copy
1234
var numbers = [2][3][2] int { { {1, 2}, {3, 4}, {5, 6} }, { {7, 8}, {9, 10}, {11, 12} }, }
question-icon

Click or drag`n`drop items and fill in the blanks

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 5. Capitolo 5
some-alt