How To Define Array In Programming Languages Java, JavaScript, PHP, C, C++, Python, C#, PowerShell? – POFTUT

How To Define Array In Programming Languages Java, JavaScript, PHP, C, C++, Python, C#, PowerShell?


Arrays are an important part of the Programming languages. Arrays are used to store multiple values in a single data structure. An array generally stores a collection of items at the contiguous memory location. Arrays can be also called List, Collection, etc. in programming languages like Python, C#, Java.

Simple Array Structure

Arrays are generally used in a simple way with a single level. We can just define an array providing the elements.

ARRAY_NAME=[ ITEM1, ITEM2 , ... , ITEMN ]
  • ARRAY_NAME is the name of the array.
  • ITEM defined each item where the limit is generally the memory size.

Nested Array

Arrays can be nested in a single array-like sub-arrays. This can be useful to describe complex data structures.

ARRAY_NAME=[[ ITEM1, ITEM2] ,[ ... , ITEMN] ]

We can see that ITEM1 and ITEM2 will create a sub-array for the ARRAY_NAME.

Java Array

Java array can be defined by specifying the type of the elements. Generally, String or Integer arrays are defined by using the following syntax.

ARRAY_TYPE[] ARRAY_NAME={ ITEM1 , ITEM2, ... , ITEMN};
  • ARRAY_TYPE is the data type of the items where it can be String, Integer, etc.
  • [] is used to define this is an array.
  • ARRAY_NAME is the name of the array where we will use an array with the variable name.
  • ITEM are items stored in an array.

Define Java String Array

We can define a string array by using double quotes for the elements like below. In this example, we will store some names in a string array named names.

String[] names={"Ahmet", "Ali", "Mehmet"};

Define Java Integer Array

We can also define an integer array where we will provide some numbers or integers like below. As we can see we do not use double quotes because integers in Java do not require double-quotes.

int[] myNum = {10, 20, 30, 40};

Access Java Array Item

After defining or set some elements in an array we may want to access it. We can use an index number that specifies the index of the given element. The index starts from 0 in Java. In this example, we will access the string Ahmet and print to the standard output with the index number 1.

String[] names={"Ahmet", "Ali", "Mehmet"};

System.out.println(names[1]);

JavaScritp Array

JavaScript arrays can be defined with the following syntax. JavaScript arrays do not have a specific value type where we can use a different type like String, Integer, Object, Date, etc in the same array.

var ARRAY_NAME=[ITEM1, ITEM2 , ... , ITEMN];

Create a JavaScript Array

We will create an array that stores names as an element like below. The array name will be names and the items will be surrounded by square brackets.

var names=["Ahmet", "Ali", "Mehmet"];

Access JavaScript Array Item

We can access an array item easily by providing its index number. In JavaScript, index numbers start from 0. In this example, we will get the item value Ali with the index number 1.

myname = names[1];

PHP Array

PHP arrays can be defined in different ways like key-value but in this part, we will learn regular arrays. PHP array uses the following syntax.

$ARRAY_NAME = array(ITEM1 , ITEM2 , ... , ITEMN);
  • ARRAY_NAME is prefixed with the sign `$`.
  • array is used to express this is an array
  • ITEMs are surrounded with brackets and separated with a comma.
LEARN MORE  Java ArrayList Get() Method Tutorial with Examples

Define PHP Array

In this example, we will an array named names which will store names as string variable type.

$names= array ("Ahmet" , "Ali" , "Mehmet");

Access PHP Array

We can access a PHP array by using the element index number in square brackets. In this example, we will access the element Ali like below.

$names= array ("Ahmet" , "Ali" , "Mehmet");

echo $names[1];

C and C++ Array

C and C++ programming languages provide very same array usage. So we will cover them in a single part. C and C++ array definitions will have the following syntax. It is the same with the Java programming language. We need to provide a type for the array and elements.

ARRAY_TYPE[] ARRAY_NAME={ ITEM1 , ITEM2, ... , ITEMN};

Define C and C++ String Array

We can define a string array by using double quotes for the elements like below. In this example, we will store some names in a string array named names.

char[] *names={"Ahmet", "Ali", "Mehmet"};

Define C and C++ Integer Array

We can also define an integer array where we will provide some numbers or integers like below. As we can see we do not use double quotes because integers in C and C++ do not require double-quotes.

int[] myNum = {10, 20, 30, 40};

Access C and C++ Array Item

After defining or set some elements in an array we may want to access it. We can use an index number that specifies the index of the given element. Index starts from 0 in C and C++. In this example, we will access the string Ahmet and print to the standard output with the index number 1.

char[] *names={"Ahmet", "Ali", "Mehmet"};

printf("%s",names[1]);

Python Array

Python provides different types for collections, arrays, lists, etc. Officially Python array is called a list. Here is the syntax of the Python array or list.

ARRAY_NAME=[ITEM1, ITEM2 , ... , ITEMN]

Define Python Array

We can define an array in python like below. Python array can store or hold different types of items. In this example, we will create an array named arr which holds names, numbers, etc.

arr = [ 1 , 2 , "Ahmet" , "Ali" ]

Access Python Array Item

We can access an array item by using index numbers. In this example, we will access the item Ahmet which has index number 2.

arr = [ 1 , 2 , "Ahmet" , "Ali" ]

print(arr[2])

C# Array

C# arrays can be defined like Java programming language. We can use the following syntax.

ARRAY_TYPE[] ARRAY_NAME= new ARRAY_TYPE[ITEM_COUNT]{ ITEM1 , ITEM2, ... , ITEMN};
  • ARRAY_TYPE is the item type of the array.
  • ARRAY_NAME is the name of the array.
  • ITEM_COUNT is the number of items where the array can be a store or hold.
LEARN MORE  Powershell Array Tutorial with Examples

Define C# Array

In this part, we will define an array named names. This array will store string values.

string[] names= new string[3]{"Ahmet", "Ali", "Mehmet"};

Access C# Item

We can access an array item in C# like below by using index value.

string myname= names[1];

PowerShell Array

PowerShell arrays can be defined with the following syntax. we will use , comma in order to separate items from each other.

Define PowerShell Array

In this part, we will define an array in Powershell with the name of names.  We will store string items in the array.

$names = "Ahmet","Ali","Baydan"
Define Array
Define Array

Access PowerShell Item

We can access an array element in PowerShell by using the index number. In this example, we will access the item named Ali with the index number 1.

$name=  $names[1]
Access Item
Access Item

Leave a Comment