如何使用编程语言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.

数组是编程语言的重要组成部分。 数组用于在单个数据结构中存储多个值。 阵列通常将项目的集合存储在连续的内存位置。 在诸如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.

    ARRAY_NAME是数组的名称。

  • ITEM defined each item where the limit is generally the memory size.

    ITEM定义了每个项目,其中限制通常是内存大小。

嵌套数组 (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.

我们可以看到, ITEM1ITEM2将为ARRAY_NAME创建一个子数组。

Java数组 (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.

可以通过指定元素的类型来定义Java数组。 通常,使用以下语法定义String或Integer数组。

ARRAY_TYPE[] ARRAY_NAME={ ITEM1 , ITEM2, ... , ITEMN};
  • ARRAY_TYPE is the data type of the items where it can be String, Integer, etc.

    ARRAY_TYPE是项目的数据类型,可以是String,Integer等。

  • [] 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.

    ARRAY_NAME是数组的名称,我们将在其中使用带有变量名称的数组。

  • ITEM are items stored in an array.

    ITEM是存储在数组中的项目。

定义Java字符串数组 (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.

我们可以通过对以下元素使用双引号来定义字符串数组。 在此示例中,我们将一些名称存储在名为names的字符串数组中。

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

定义Java整数数组 (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.

我们还可以定义一个整数数组,在其中我们将提供一些数字或整数,如下所示。 如我们所见,我们不使用双引号,因为Java中的整数不需要双引号。

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

访问Java数组项 (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.

在定义或设置数组中的某些元素后,我们可能要访问它。 我们可以使用一个索引号来指定给定元素的索引。 在Java中,索引从0开始。 在此示例中,我们将访问字符串Ahmet并使用索引号1打印到标准输出。

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

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

JavaScritp数组 (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.

可以使用以下语法定义JavaScript数组。 JavaScript数组没有特定的值类型,因此我们可以在同一数组中使用其他类型,例如String,Integer,Object,Date等。

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

创建一个JavaScript数组 (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"];

访问JavaScript数组项 (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.

通过提供其索引号,我们可以轻松访问数组项。 在JavaScript中,索引号从0开始。在本示例中,我们将获得索引值为1的项目值Ali

myname = names[1];

PHP数组 (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.

PHP数组可以以不同的方式(例如键值)进行定义,但是在这一部分中,我们将学习常规数组。 PHP数组使用以下语法。

$ARRAY_NAME = array(ITEM1 , ITEM2 , ... , ITEMN);
  • ARRAY_NAME is prefixed with the sign `$`.

    ARRAY_NAME的前缀为$。

  • array is used to express this is an array

    array用来表示这是一个数组

  • ITEMs are surrounded with brackets and separated with a comma.

    ITEM用括号括起来,并用逗号分隔。

LEARN MORE  Python Array or List Tutorial
了解更多Python数组或列表教程

定义PHP数组(Define PHP Array)

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

在此示例中,我们将创建一个名为names的数组,该数组会将名称存储为字符串变量类型。

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

访问PHP数组 (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.

我们可以通过使用方括号中的元素索引号来访问PHP数组。 在此示例中,我们将如下访问元素Ali

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

echo $names[1];

C和C ++数组 (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.

C和C ++编程语言提供了非常相同的数组用法。 因此,我们将在一个部分中介绍它们。 C和C ++数组定义将具有以下语法。 Java编程语言也是如此。 我们需要为数组和元素提供一种类型。

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

定义C和C ++字符串数组 (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.

我们可以通过对以下元素使用双引号来定义字符串数组。 在此示例中,我们将一些名称存储在名为names的字符串数组中。

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

定义C和C ++整数数组 (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.

我们还可以定义一个整数数组,在其中我们将提供一些数字或整数,如下所示。 如我们所见,我们不使用双引号,因为C和C ++中的整数不需要双引号。

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

访问C和C ++数组项 (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.

在定义或设置数组中的某些元素后,我们可能要访问它。 我们可以使用一个索引号来指定给定元素的索引。 在C和C ++中,索引从0开始。 在此示例中,我们将访问字符串Ahmet并使用索引号1打印到标准输出。

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

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

Python数组 (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.

Python为集合,数组,列表等提供了不同的类型。Python数组正式称为列表。 这是Python数组或列表的语法。

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

定义Python数组 (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.

我们可以在python中定义一个数组,如下所示。 Python数组可以存储或保存不同类型的项目。 在此示例中,我们将创建一个名为arr的数组,其中包含名称,数字等。

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

访问Python数组项 (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.

我们可以使用索引号访问数组项。 在此示例中,我们将访问索引为2的项Ahmet

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

print(arr[2])

C#数组 (C# Array)

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

可以像Java编程语言一样定义C#数组。 我们可以使用以下语法。

ARRAY_TYPE[] ARRAY_NAME= new ARRAY_TYPE[ITEM_COUNT]{ ITEM1 , ITEM2, ... , ITEMN};
  • ARRAY_TYPE is the item type of the array.

    ARRAY_TYPE是数组的项目类型。

  • ARRAY_NAME is the name of the array.

    ARRAY_NAME是数组的名称。

  • ITEM_COUNT is the number of items where the array can be a store or hold.

    ITEM_COUNT是数组可以存储或保留的项目数。

LEARN MORE  Python Array or List Tutorial
了解更多Python数组或列表教程

定义C#数组(Define C# Array)

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

在这一部分中,我们将定义一个名为names的数组。 该数组将存储字符串值。

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

访问C#项 (Access C# Item)

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

我们可以通过使用索引值访问C#中的数组项,如下所示。

string myname= names[1];

PowerShell阵列 (PowerShell Array)

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

可以使用以下语法定义PowerShell数组。 我们将使用,逗号以将项目彼此分开。

定义PowerShell数组 (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.

在这一部分,我们将定义PowerShell和名称的数组names 。 我们将字符串项目存储在数组中。

$names = "Ahmet","Ali","Baydan"
Define Array
Define Array
定义数组

访问PowerShell项目(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.

我们可以使用索引号在PowerShell中访问数组元素。 在此示例中,我们将访问索引编号为1名为Ali的项目。

$name=  $names[1]
Access Item
Access Item
存取项目

翻译自: https://www.poftut.com/how-to-define-array-in-programming-languages-java-javascript-php-c-c-python-c-powershell/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值