Computer Programming 简明教程
Computer Programming - Arrays
考虑一种情况,我们需要存储五个整数。如果我们使用编程的简单变量和数据类型概念,那么我们需要五个 int 数据类型的变量,且程序如下 −
Consider a situation where we need to store five integer numbers. If we use programming’s simple variable and data type concepts, then we need five variables of int data type and the program will be as follows −
#include <stdio.h>
int main() {
int number1;
int number2;
int number3;
int number4;
int number5;
number1 = 10;
number2 = 20;
number3 = 30;
number4 = 40;
number5 = 50;
printf( "number1: %d\n", number1);
printf( "number2: %d\n", number2);
printf( "number3: %d\n", number3);
printf( "number4: %d\n", number4);
printf( "number5: %d\n", number5);
}
很简单,因为我们只需要存储五个整数。现在让我们假设我们必须存储 5000 个整数。我们是否将使用 5000 个变量?
It was simple, because we had to store just five integer numbers. Now let’s assume we have to store 5000 integer numbers. Are we going to use 5000 variables?
为了处理这种情况,几乎所有编程语言都提供了一个称为 array 的概念。数组是一种数据结构,可以存储相同数据类型的固定大小元素集合。数组用于存储数据集合,但通常将数组视为相同类型变量的集合更为有用。
To handle such situations, almost all the programming languages provide a concept called array. An array is a data structure, which can store a fixed-size collection of elements of the same data type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
您无需像 number1、number2、……、number99 那样声明单个变量,只需声明一个 number 的整数类型数组变量,并使用 number1[0]、number1[1] 和……number1[99] 来表示单个变量。在这里,0、1、2、……99 是与 var 变量关联的 index ,并且它们用于表示数组中可用的单个元素。
Instead of declaring individual variables, such as number1, number2, …, number99, you just declare one array variable number of integer type and use number1[0], number1[1], and …, number1[99] to represent individual variables. Here, 0, 1, 2, …..99 are index associated with var variable and they are being used to represent individual elements available in the array.
所有阵列都包含连续的内存位置。最低地址对应于第一个元素,而最高地址对应于最后一个元素。
All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.
Create Arrays
要在 C 中创建数组变量,程序员应指定元素类型和存储在该数组中的元素数量。以下是创建 C 编程中数组的简单语法 −
To create an array variable in C, a programmer specifies the type of the elements and the number of elements to be stored in that array. Given below is a simple syntax to create an array in C programming −
type arrayName [ arraySize ];
这称为单维数组。 arraySize 必须是大于零的整数常量,而 type 可以是任何有效的 C 数据类型。例如,现在声明一个名为 number 的 10 元素数组,类型为 int ,使用此语句 −
This is called a single-dimensional array. The arraySize must be an integer constant greater than zero and type can be any valid C data type. For example, now to declare a 10-element array called number of type int, use this statement −
int number[10];
此处,number 是一个变量数组,它足以容纳多达 10 个整数。
Here, number is a variable array, which is sufficient to hold up to 10 integer numbers.
Initializing Arrays
你可以在 C 中逐个初始化一个数组或使用以下语句:
You can initialize an array in C either one by one or using a single statement as follows −
int number[5] = {10, 20, 30, 40, 50};
大括号 {} 之间的值的数量不能大于我们为方括号 [] 之间的数组声明的元素数量。
The number of values between braces { } cannot be larger than the number of elements that we declare for the array between square brackets [ ].
如果你省略数组的大小,就会创建一个仅足够容纳初始化大小的数组。因此,如果写入:
If you omit the size of the array, an array just big enough to hold the initialization is created. Therefore, if you write −
int number[] = {10, 20, 30, 40, 50};
您要创建的数组与上一个示例中完全相同。下面是一个分配数组的单个元素的示例:
You will create exactly the same array as you did in the previous example. Following is an example to assign a single element of the array −
number[4] = 50;
以上语句将数组中的第 5 个元素分配给值 50。所有数组都将 0 作为其第一个元素的索引,该索引也被称为基索引,数组的最后一个索引将是数组总大小减 1。以下图像显示了我们上面讨论的数组的图片表示:
The above statement assigns element number 5th in the array with a value of 50. All arrays have 0 as the index of their first element which is also called the base index and the last index of an array will be the total size of the array minus 1. The following image shows the pictorial representation of the array we discussed above −
Accessing Array Elements
通过给数组名称编制索引来访问元素。这是通过在数组名称后面方括号内放置元素的索引来完成的。例如:
An element is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array. For example −
int var = number[9];
以上语句将从数组中获取第 10 个元素,并将该值分配给 var 变量。以下示例使用了上面提到的所有三个概念,即创建、分配和访问数组:
The above statement will take the 10th element from the array and assign the value to var variable. The following example uses all the above-mentioned three concepts viz. creation, assignment, and accessing arrays −
#include <stdio.h>
int main () {
int number[10]; /* number is an array of 10 integers */
int i = 0;
/* Initialize elements of array n to 0 */
while( i < 10 ) {
/* Set element at location i to i + 100 */
number[ i ] = i + 100;
i = i + 1;
}
/* Output each array element's value */
i = 0;
while( i < 10 ) {
printf("number[%d] = %d\n", i, number[i] );
i = i + 1;
}
return 0;
}
编译并执行上述代码后,将产生以下结果 −
When the above code is compiled and executed, it produces the following result −
number[0] = 100
number[1] = 101
number[2] = 102
number[3] = 103
number[4] = 104
number[5] = 105
number[6] = 106
number[7] = 107
number[8] = 108
number[9] = 109
Arrays in Java
以下是用 Java 编写的等效程序。Java 支持数组,但它们使用 new 运算符在 Java 中创建的方式略有不同。
Following is the equivalent program written in Java. Java supports arrays, but there is a little difference in the way they are created in Java using the new operator.
您可以尝试执行以下程序以查看输出,其必须与上述 C 示例生成的输出相同。
You can try to execute the following program to see the output, which must be identical to the result generated by the above C example.
public class DemoJava {
public static void main(String []args) {
int[] number = new int[10];
int i = 0;
while( i < 10 ) {
number[ i ] = i + 100;
i = i + 1;
}
i = 0;
while( i < 10 ) {
System.out.format( "number[%d] = %d\n", i, number[i] );
i = i + 1;
}
}
}
当执行上述程序时,它将生成以下结果 −
When the above program is executed, it produces the following result −
number[0] = 100
number[1] = 101
number[2] = 102
number[3] = 103
number[4] = 104
number[5] = 105
number[6] = 106
number[7] = 107
number[8] = 108
number[9] = 109
Arrays (Lists) in Python
Python 没有数组的概念,而是提供了另一种称为 list 的数据结构,它提供类似于任何其他语言中数组的功能。
Python does not have a concept of Array, instead Python provides another data structure called list, which provides similar functionality as arrays in any other language.
以下是使用 Python 编写的等效程序−
Following is the equivalent program written in Python −
# Following defines an empty list.
number = []
i = 0
while i < 10:
# Appending elements in the list
number.append(i + 100)
i = i + 1
i = 0
while i < 10:
# Accessing elements from the list
print "number[", i, "] = ", number[ i ]
i = i + 1
当执行上述程序时,它将生成以下结果 −
When the above program is executed, it produces the following result −
number[ 0 ] = 100
number[ 1 ] = 101
number[ 2 ] = 102
number[ 3 ] = 103
number[ 4 ] = 104
number[ 5 ] = 105
number[ 6 ] = 106
number[ 7 ] = 107
number[ 8 ] = 108
number[ 9 ] = 109