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 as 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 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.

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 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.

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.

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 which specifies the index of the given element. 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.

JavaScritp Array

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

Create a JavaScript Array

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

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.

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 is prefixed with the sign $.
  • array is used to express this is an array
  • ITEMs are surrounded with bracket and separated with a comma.

Define PHP Array

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

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.

LEARN MORE  Java Set Tutorial with Examples

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 definition 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.

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.

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.

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 which 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.

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.

LEARN MORE  Powershell Array Tutorial with Examples

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.

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.

C# Array

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

  • 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 store or holde.

Define C# Array

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

Access C# Item

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

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.

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.

Access Item

Access Item

Leave a Reply

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

Enjoy this blog? Please spread the word :)