Javatpoint Logo
Javatpoint Logo

How to store value in array

An array is a group of similar type of elements which has contiguous memory location. An array is a collection of like-typed variables that are stated to by a common name.

This tutorial will briefly discover how to store value in an array in the commonly used languages.

1. C Language

All arrays are the contiguous block of memory locations. By default, the lowest position of the array stores the first element, and the highest position stored the last data. In C, the array is declared by specifying the element's type and the total length of array required to store the data.

Syntax for declaring array

Syntax for initializing for storing array values

Example

Output

Element stored at position [0] = 10
Element stored at position [1] = 11
Element stored at position [2] = 12
Element stored at position [3] = 13
Element stored at position [4] = 14
Element stored at position [5] = 15
Element stored at position [6] = 16
Element stored at position [7] = 17
Element stored at position [8] = 18
Element stored at position [9] = 19
Element stored at position [10] = 20

Multidimensional Array in C

In C language, the elements of a 2 D (two-dimensional) array are accessed with the help of subscripts, i.e., the row index number and the column index number of the array.

Syntax for declaring Array

Syntax for Initializing Two-Dimensional Arrays

Output

Data stored in 2D array[0][0] = 1
Data stored in 2D array[0][1] = 0
Data stored in 2D array[1][0] = 1
Data stored in 2D array[1][1] = 2
Data stored in 2D array[2][0] = 2
Data stored in 2D array[2][1] = 4
Data stored in 2D array[3][0] = 3
Data stored in 2D array[3][1] = 6

2. C++ Language

In C++ language the user needs to specify the element type and total length of array.

Syntax to Declare Array

Syntax to initialize array

Example

Output

array at position[0]: 1
array at position[1]: 7
array at position[2]: 50
array at position[3]: 6

Multidimensional Array

C++ language also enables the Multidimensional arrays.

Syntax for initializing 2D array

Example

Output

array at position[0][0]: 1
array at position[0][1]: 0
array at position[1][0]: 0
array at position[1][1]: 2
array at position[2][0]: 2
array at position[2][1]: 3
array at position[3][0]: 5
array at position[3][1]: 6

3. Java

In Java language, Arrays work differently than what they used to do in C or C++ language.

One-Dimensional Arrays:

To declare an array, the user needs to have two primary components: the type and the array's name.

The 'Type' refers to the elementary type of a specific array. It determines the data type of all elements that are included in the array. It comprises the array of primitive data types, unlike integers, char, float, double, etc., or it could include the user-defined data types (objects of a class) as well. Therefore, the element type for the array concludes what kind of data the array will contain.

Syntax

Store values in one-dimensional array

Assigning values to an element in an array is similar to assigning values to scalar variables.

NOTE: It the array element is not assigned any value, by default it has a Null (empty) value.

Example

Output

20
40
60
80
100

Arrays of Objects

An array of objects is constructed in the same way as an array of primitive type data elements.

Example

Output

Element at 0 : 111 Varun
Element at 1 : 121 Sukla
Element at 2 : 131 Virat
Element at 3 : 141 Anuskha
Element at 4 : 151 Mohit

Multidimensional Arrays

Multidimensional arrays are termed 'arrays of arrays' as they can hold each element of an array with the reference of another array. These are also known as Jagged Arrays. A multidimensional array is constructed by adding a set of square brackets ([]) per dimension.

Syntax

Example to store values in a Multidimensional Array

Example of Multidimensional Array

Output

12 17 19 
32 62 12 
37 34 32 

4. PHP

PHP array is an ordered map (holds elements on the base of the key-value). It is utilized to hold multiple values of a similar data type in a single variable.

PHP contains 3 kinds of array that are as follows:

  1. Indexed Array
  2. Associative Array
  3. Multidimensional Array

1. Indexed Array

PHP index is described by an integer number that begins with 0 (default value). The PHP array can store any data type, such as numbers, characters, strings, and objects. All PHP array data are allocated an index number by default.

Syntax to store values

Or

Example

Output

Colours are: Red, White, Black, Yellow

2. Associative Array

In PHP, the user can associate any specific name with each array elements using the '=>' symbol.

Syntax

Or

Example

Output

Reema's Marks: 95
John's Marks: 45
Rahul's Marks: 20

3. Multidimensional Array

Multidimensional arrays in PHP are also termed as "array of arrays". It enabled the user to store array data in a tabular format. PHP multidimensional array can be expressed in the form of a matrix which is denoted by row * column.

Syntax

Example

Output

1 Reema 95 
2 john 45 
3 rahul 20 

5. Python

Python uses a module named 'Array' to handle all the functions of Arrays in Python. It is helpful when the user wants to manipulate only particular data values. Given below are the keywords that are important to learn the concept of an array in Python:

  • Element - Any data stored in an array is known an element.
  • Index - Whenever an array stores any data, it has some numerical location known as index that is beneficial to identify the location of the element.

Syntax

Example

Output

First array value: 20
Second array value: 40
Second last array value: 80






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA