编程知识 cdmana.com

Java Architect of Turing phase IV

Array (Array) Is an ordered sequence of elements .[1] If you name a limited set of variables of the same type , So the name is array name . The variables that make up an array are called the components of the array , Also known as an array of elements , Sometimes called subscript variable . The number used to distinguish the elements of an array is called the subscript . Arrays are in programming , For convenience , A form in which elements of the same type are organized in an orderly manner .[1] These ordered sets of data elements of the same kind are called arrays .
An array is a collection of data of the same type .
Chinese name
Array
Foreign name
Array
classification
A one-dimensional 、 A two-dimensional ( matrix )、 Three dimensional array
Definition
A collection of data elements of the same kind
Fast
Navigation
Array element structure form related operation array type representation method assignment method use rule common function
Source of production
stay C In language , Array [2] Belongs to the construction data type . An array can be decomposed into multiple array elements , These array elements can be basic data types or construction types . So it's different according to the type of array elements , Array can be divided into numerical array 、 A character array 、 Pointer array 、 Structure array and other categories .
About variable length arrays (VLA) The problem of : The original C89 Variable length arrays are not allowed in the standard , But in C99 In the standard , Joined the right VLA Support for [3], But not many compilers are supported , And because of the security problem of stack overflow , Not many people dare to use this variable length array , So in C11 It is defined as an optional function in the standard [4].
If you have experience programming in other languages , Then I will be familiar with the concept of array . With arrays , You can refer to a series of variables with the same name , And use numbers ( Indexes ) To identify them . on many occasions , Using arrays can shorten and simplify programs , Because you can design a loop with index values , Handle a variety of situations efficiently . Arrays have upper and lower bounds , The elements of an array are continuous within the upper and lower bounds . because Visual Basic Allocate space for each index value , So don't unrealistically declare an array that's too large .
Here the array is an array of variables declared in the program . They are different from control arrays , Control array is designed by setting controls Index Attribute specified . Arrays of variables are always continuous ; Unlike control arrays , Array elements cannot be loaded or unloaded from the middle of an array .
All elements in an array have the same data type ( stay C、C++、Java、pascal It's the same in China . But it's not all about arrays , For example Visual Foxpro There is no such requirement for arrays in ). Of course , When the data type is Variant when , Each element can contain different kinds of data ( object 、 character string 、 Values, etc ). You can declare an array of any basic data type , Including user-defined types and object variables .
If you want the user to input an array , It's usually a cycle , But you also need to fix the size of the array before you input it .
compact It doesn't have much to do with variable length arrays , There should be no variable length arrays . Because the general pass array to the function is to pass the address of the array and the number of elements , That's just a hint , It's not a request .
Prototypes can be written like this ( Suppose the elements of an array are type):
int compact(type *Array,int Count)
Array type description stay C In the language, the array must be typed first .
The general form of array description is : Type specifier Array name [ Constant expression ],……; among , A type specifier is any basic or constructive data type . The array name is a user-defined array identifier . The constant expression in square brackets represents the number of data elements , Also known as the length of an array .
An array is a set of variable array definitions that define the same data type at one time .
give an example
Description integer array a, Yes 10 Elements . If you want to express the 10 Elements , Then use a[9]. The first is a[0].
int a[10];
Explain real arrays b, Yes 10 Elements , Real array c, Yes 20 Elements .
float b[10],c[20];
Description character array ch, Yes 20 Elements .
char ch[20];
characteristic
1. An array is a collection of elements of the same data type .
2. The elements in an array are stored in sequence , They are stored in memory in this order .
3. An array element is represented by the name of the entire array and its own ordinal position in the array . for example ,a[0] Indicates the name is a The first element in the array of ,a[1] Representative array a The second element of , And so on .
about VB Array of , When representing array elements, you should pay attention to :
1 The subscript should follow the array name , And enclosed in parentheses ( You can't use other brackets ).
2 The subscript can be a constant , Variable , Or expressions , But its value must be an integer ( If it's a decimal, round it to an integer ).
3 The subscript must be a continuous integer , The minimum value becomes the lower bound , Its maximum becomes the upper bound . If not specified, the default lower bound value is 1.
Elements in an array
The difference from a field in a structure or class
All elements in an array have the same type ( This is different from fields in structures or classes , They can be different types ). The elements in the array are stored in a contiguous block of memory , And access it through the index ( This is also different from fields in structures and classes , They access... By name ).[1]
type
Array elements are not only of primitive type , It can also be structure 、 Enumeration or class .[1]
Structure form
Stack memory
Some basic types of variables defined in a method and reference variables of objects are allocated in the stack memory of the method , When you define a variable in a piece of code ,java Just allocate memory space for this variable in stack memory , When a variable is out of scope ,java Will automatically free up the memory space allocated for the variable .
Heap memory
Heap memory is used to store the new Operators create objects and arrays , Memory allocated in the heap , from java Virtual machine's automatic garbage collector to manage . After creating an array or object in the heap , A special variable is also defined in stack memory . Let the value of this variable in stack memory be equal to the first address of array or object in heap memory , This variable in the stack becomes a reference variable of an array or object , Reference variables actually hold the addresses of arrays or objects in heap memory ( Also known as the handle to an object ), Later, you can use the stack reference variable in the program to access arrays or objects in the heap .[5]
The difference from a field in a structure or class
All elements in an array have the same type ( This is different from fields in structures or classes , They can be different types ). The elements in the array are stored in a contiguous block of memory , And access it through the index ( This is also different from fields in structures and classes , They access... By name ).[1]
The relevant operation
Declare a fixed size array
There are three ways to declare a fixed size array , Which method to use depends on the valid range the array should have :
(1) Create a common array , Use... In the declaration section of the module Public Statement to declare an array .
(2) Create a module level array , Use... In the declaration section of the module Private Statement to declare an array .
(3) Create a local array , Use... In the process Private Statement to declare an array .
Set the upper and lower bounds
When you declare an array , The array name is followed by an upper bound enclosed in brackets .
The upper bound must not exceed Long Range of data types (-2,147,483,648 To 2,147,483,647).
for example , The following array declaration can appear 、 In the declaration section of the module :
Dim Counters (14) As Integer '15 Elements .
Dim Sums (20) As Double '21 Elements .
To create a common array , Direct use Public replace Dim.
Public Counters (14) As Integer
Public Sums (20) As Double
In the process, the same declaration uses Dim:
Dim Counters (14) As Integer
Dim Sums (20) As Double
The first statement establishes one with 15 Array of elements , The index number is from 0 To 14.
The second statement establishes one with 21 Array of elements , The index number is from 0 To 20. The default lower bound is 0.
In order to define the lower bound , With keywords To Explicitly provide lower bounds ( by Long data type ):
Dim Counters (1 To 15) As Integer
Dim Sums (100 To 120) As String
In the foregoing statement ,Counters The index values range from 1 To 15, and Sums The index values range from 100 To 120.
Arrays containing other arrays
It's possible to build Variant Data type array , And live together with arrays of different data types .
The following code creates two arrays , Contains an integer , And the other one contains character string . And then declare the third Variant Array , And put an array of integers and strings in it :
Private Sub Command1_Click ()
Dim intX As Integer Variable declaration counter .
Declare and place an array of integers .
Dim countersA (5) As Integer
For intX = 0 To 4
countersA (intX) = 5
Next intX
Declare and place an array of strings .
Dim countersB (5) As String

版权声明
本文为[Vx_ itstudy01]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224084735405S.html

Scroll to Top