Find an element in the list of values ​​1c. Universal collection Structure

What is this article about?

This article continues the series of articles “First steps in 1C development.” It covers principles of working with universal collections. After reading the article, you will learn:

  • What are universal collections, and when and in what cases should they be used?
  • What do all universal collections have in common? What techniques can you use to work with all of them?
  • What is an array, how and when to use it? What methods does he have?
  • Why use a structure? How is it different from an array?
  • When should you use a list of values? How to display it on the form?
  • Compliance – what is it and when to use it? What are the advantages regarding structure?
  • What is the value table used for? How to describe its structure? How to add/remove lines? How to display it on the form?
  • Tree of values ​​- what is it used for? How to fill out and display the form? How to work with it?

Applicability

The article discusses the 1C:Enterprise 8.3 platform of the current edition.

How to work with universal collections in 1C

A collection of values ​​is a container that can usually contain any number of elements. However, there are often no strict restrictions on the data type.

You can add values ​​to a generic collection. All values ​​in the collection can be traversed. These collections are used mainly for some kind of processing in algorithms. Those. These are some dynamic structures that exist while the algorithm is running.

It is important to understand that collections are not stored in a database (we are not talking about the Value Store data type, which can store almost any type of data).

Exist different kinds universal collections: Array, Structure, Matching, Fixed Array, Value Table, Tabular part etc. But all collections have similar behavior.

A collection can be created as a result of the operation of a function (the function returns a universal collection as a value).

You can obtain a new collection manually by calling the constructor and creating an instance of the class.

For example: OurArray = New Array;

The constructors for many generic collections are parameterized.

So, in the constructor for You can specify the number of members in the corresponding dimensions. Those. you can immediately declare multidimensional .

The corresponding description of the constructor is in the syntax assistant.

Thus, using constructor parameters, you can immediately set the desired behavior of this object.

But the parameters are optional; the developer can not set them and further define the behavior of the Array as he sees fit.

Almost any universal collection can be created using a constructor (the exception is table parts, which act as configuration objects).

For universal collections, there are common concepts such as index and number. Each element of the collection has an index. In this case, the index starts from zero.

To access an element OurArray, you can use index access; for this, the index is indicated in square brackets.

For example, OurArray. Please note that in this case the system returns the Array element with index 3, and in order this is the fourth element of the Array.

For some collections there is also the concept of a line number. The line number starts with one. For example, for the tabular part there is such a property as row number. It is important to keep in mind that if we know the line number and want to access it by index, then we should use a value one less than the line number as the index.

The concept of a line number does not exist in all collections, but mainly in those that can be displayed in the user interface.

All collections use collection element traversal. Bypass is possible in two ways: cycle For And cycle For each of.

The methods that apply to most generic collections are Count, Index, Add, Insert, Delete, and Find.

Count is a function that returns the number of elements in a collection. It can be used before a cycle For, as shown in the figure.

The Index method does not exist for all collections, but only for those whose elements can be referenced. An example is Table of Values.

Table of Values– this is a specific collection of rows; rows can contain different columns with different types of values.

Each line represents an independent entity. You can get a link to it; through this line you can access the values ​​of the columns in this line.

The Index method allows you to determine which index corresponds to a given row (that is, the current position of the row in the table). Index values ​​start at zero.

Almost any universal collection has methods for adding new values ​​to a given collection. The figure shows how to fill an Array with values ​​from 0 to 10 in two ways.

To add an element to the Array we can use the method Add, indicate the value to be added in brackets. In this case, the value will be added to the end of the list, i.e. The array will constantly increase due to the last position.

Another method that allows you to add values ​​to a collection is the Insert. It is different from the method Add in that you can specify where to insert the added element.

Syntax: Insert (,)

The first parameter specifies the index into which the new value will be inserted. Those. for example, we can specify that each value should be inserted at the beginning of the list (second method in the figure above).

To remove elements from a collection, use the method Delete. The Delete method specifies by index which element we will delete.

Syntax: Delete()
Usage example: OurArray.Delete(5);

It should be noted that for those collections where strings represent an independent entity (for example, for Tables of Values), we can also use the method of getting the index in order to later delete this row.

Almost all collections have a method for searching for a value - Find. The value we want to find is passed to the method. Some collections allow you to set some restrictions.

For example, in Value Table you can specify the rows and columns in which you want to search.

If the value is found, this method returns the index or the specified string. If the value is not found, a value of type is returned Undefined. In relation to an Array, returns Index, or value Undefined.

Usage example: OurVariable = OurArray.Find(8);

Universal collections can be cleared very quickly, i.e. remove absolutely all elements. For this purpose the method is used Clear(), which removes Array elements, rows Tables of Values, or data from other collections.

Additional methods for Array

Method BBorder() returns the number of elements minus one. Those. if we use a loop For, then instead of the Quantity method we can immediately use the method Border().

In particular, the variable QuantityInArray could be defined differently:

QuantityInArray = OurArray.InBorder();
Then, when describing the cycle itself, one should not be subtracted from this variable.

The Set method allows you to assign a value to an Array element by index.

Syntax: Install(,)

Example: OurArray.Set(2,8);

Alternative option: OurArray = 8;

You can use the method for an Array Get, for reading a value at an index without resorting to using square brackets.

Syntax: Get()

Example: OurVariable = OurArray.Get(2);

Alternative option: OurVariable = OurArray;

Universal collection Structure

A Structure, like an Array, can have an unlimited number of elements, but the content of the element differs from the Array.

The structure is a collection, each value of which consists of a pair. The first element of the pair is called Key. The second element of the pair is Meaning.

Key is a strictly string data type that describes a value. For example, To the key"Code" can correspond to the value 113; To the key“Name” meaning “Vasya”. The Value itself is not subject to a data type restriction.

The structure is very convenient to use if we want to create a certain list of parameters. If this Structure called OurStructure, then we will refer to its two values ​​as follows: OurStructure.Code and OurStructure.Name.

This kind of access is much more convenient than if we defined all the parameters in an Array and accessed them by index.

Structure makes program code readable (understandable). Structure is used quite often, much more often than Array.

It is used to describe certain parameters, of which there are often quite a large number in all algorithms.

In addition, the Structure is used if the procedure and function contain a large number of passed parameters.

Then it is much more convenient to write all the parameters into the Structure and pass it on. Those. the parameters of procedures and functions are “packed”.

Separately, it should be noted that as Key Not absolutely any line can appear in the Structure. Certain restrictions apply.

Key should act as an identifier. This means that in Klyuche there must be no spaces and it cannot start with a number.

Acceptable start Key with a letter or underscore. Thus, Key must satisfy the requirements for creating identifiers.

Let us note how else a Structure differs from an Array. There is a method in the Structure Insert, there are two methods for insertion in Array: Insert(to a certain position) and Add(to the end of the list). In an Array, all elements are ordered.

Structure is a kind of disordered set. This is why there is only an insert method for a Structure.

The value is inserted not at a specific position, but into the specified set. A Structure cannot be accessed by index, like other generic collections.

Structure elements are accessed only by Key name. However, the For Each loop also works for the Structure, but you should not rely on the order of the Structure elements.

A structure is created in the same way as other generic collections by using the New constructor, specifying the data type Structure.

Like an Array, a Structure's constructor can have parameters. Those. it is possible to describe the very content of a Structure using a constructor.

Unlike an Array, where you can simply specify the number of elements for all dimensions, in a Structure you can specify the content itself.

For example: OurStructure = New Structure (“Code, Name”, 133, “Vasya”);

Separated by commas, the names of the Keys are first listed, and then, accordingly, in the same sequence, the values ​​of the parameters.

There is a method to add a new value to the Structure Insert, which inserts a new pair (Key and Value).

For example: OurStructure.Insert(“FamilyMembers”,3);

The Structure is characterized by another method that is used quite often. This is the method Property.

Using this method, you can understand whether there is an element in this Structure whose Key has such and such a name.

If such an element exists, the system will return the value True, otherwise – False.

For example, the expression OurStructure.Property (“Family Members”) will be equal to the value True. This method is used quite often when analyzing the Structure.

As with any universal collection, it is possible to access the properties of a Structure by index. But the index for the Structure is a string value.

For example: Report(OurStructure[“FamilyMembers”]);

However, we should not forget that a Structure is an unordered set of objects, which is why accessing by index 0, 1, 2 is unacceptable.

Generic collection List of values

ListValues is a linear list of elements of any data type.

Each element consists of several values. Schematically, a list of values ​​can be represented as a list with four columns.

First column - Mark. It has a Boolean data type and allows the user to either check or uncheck boxes.

The other column is a picture that can somehow visually represent this element, i.e. match this string with a picture.

The third column is the stored value itself, i.e. this is any type of data, and it can be different in different lines.

The fourth column is the presentation, i.e. this is a kind of string description of a given value. The view will be displayed to the user when he views this element. In this case, if the representation is not specified, the system will try to obtain representations for the element contained in this position.

ListValues– this is the object that the user can visually work with. Those. ListValues can be displayed on the form.

The user can perform some actions with it. Besides, ListValues can be inferred independently using methods, i.e. show on the screen in some branch of the algorithm (with the exception of server code) so that the user selects some line or checks some boxes.

We'll find ListValues in the sitax assistant. Constructor ListValues not parameterized (you cannot set any default values).

There are methods such as:

  • Insert(,) ;
  • Add(,);
  • Quantity();
  • Index().

There are also special methods, for example, UnloadValues(). This creates an Array into which the list of values ​​is copied. For example:

Array of Elements = List of PriceTypes.OutloadValues();

There is also a reverse method:
ListPriceTypes.LoadValues(ArrayItems);

There are search methods:
FindByValue(); FindByIdentifier().

There is a copy method:
ListCopy = PriceTypeList.Copy();
This method is intended to make some kind of modification to the copy.

There are methods:
SortByValue();
SortByView().

Methods SelectItem(,) And MarkItems() call a modal dialog box that stops execution of the algorithm until the user closes the window.

To use these methods in configuration properties Mode of using modality must be set to Use.

Example code called from a Managed Application module:

Display this code in user mode (modal dialog).

Below ListValues used as an available data type for form attributes. We create a new attribute for the processing form and define its type ListValues and display it on the form.

Creating a new team Fill inGifts, transfer it to the form and define an action handler for it.

In user mode, when you click the Fill in Gifts button in the processing form, a completed list will appear.

If desired, the list can be edited: some elements can be added, some can be removed.

Versatile Collection Compliance

This collection is very similar to Structure. Just like Structure, Mappings are sets of values ​​that consist of a key and the value itself.

The main difference is that any data type can be specified as the Key, as well as for the value. In view of this feature, it is necessary to access the match value by index; the key value is indicated as the index value.

The key can be a data type other than a string. The properties and methods of working with Compliance are almost the same as those of Structure.

The Compliance Constructor, unlike the Structure, does not contain the ability to specify parameters.

Usage example:

Correspondence is convenient to use when you need to connect any two structures. For example, each row in the tabular section must be matched with a row from the table of values.
In this case, the table section row is used as the Match key and the corresponding value is indicated.

When inserting elements into a collection Match in addition to the method Insert(,) Another way to insert a value is to use the regular assignment operator.

For example: OurMatch = NewMatch;
Match = 999;

Those. if an element was not present in the collection, then it will be added using the assignment operator, and if it was present, it will be updated.

This is in contrast to Structure.

Universal Collection Table of Values

Table of Values is a table with an arbitrary number of rows and an arbitrary number of columns. The intersection can store values ​​of any data type. If necessary, columns can be typed, i.e., you can determine in which column what type of data is stored.

You can leave the columns untyped, then values ​​can be stored in one column in different rows different types.

Differences Tables of Values from a two-dimensional Array:

  • this is an object that the user can work with (the table of values ​​can be displayed on the screen, the user can fill it out, and the entered data can then be read);
  • building indexes for quick search;
  • cloning, filling an entire column with a certain value, uploading all columns to an array.

Table of Values used as a kind of information storage buffer. Table of Values is returned and accepted as a parameter by many system methods. It is possible to build a query against the Table of Values.

So, Table of Values consists of a set of rows and a set of columns. Both rows and columns are collections.

Those. inside the collection Table of Values there are two more collections. Let's turn to the syntax assistant and find Table of Values.

Supported data types: itself Table of Values, which consists of strings. Each row is represented by a data type RowTableValues, which has its own properties and its own methods. Available Collection of Table ColumnsValues also has certain properties.

Important point! The procedure that creates Table of Values, must compile &OnServer.

Before you start working with Table of Values, you need to determine what columns it will contain (i.e. create them). Syntax:

Add(,)
(optional)
Type: String.
(optional)
Type: DescriptionTypes
(optional)
Type: String.
(optional)
Type: Number.

For example:

To call this procedure we will use the command.

In description Tables of Values the elements of the collection are precisely RowsTableValues.

Unlike columns, which consist only of properties (Name, Type, Heading, Width), in RowTableValues There are both properties (access by column name) and methods (you can get and set a value, work with owners).

To add a new row to the table you need to use the method either Add(), or Insert(). In the second case, you should indicate at what position the required line should be placed.

To assign a value to a column, we use a dot to access the column name or index (using square brackets).

For filling Tables of Values The following methods can be used:

Clear()– to remove all rows from Tables of Values.

FillValues(,)– allows you to fill all columns or selected columns with one value.
LoadColumn(,)– loads a column from the array.
UnloadColumn()– unloads the column into an array.

The last two methods are convenient to use when you need to transfer a column from one value table to another.

Copy(,)– allows you to create a new one based on an existing table Table of Values, and not all rows and all columns, but only some of them. Return value – Table of Values.

You can copy the structure Tables of Values. There is a corresponding method for this CopyColumns(). We'll get an empty one Table of Values with the required structure.

IN Value Table there is a method Total(). You can specify the column in which you want to sum the numerical values. In relation to the previously shown code in the Tableau, you can calculate the value: TK.Total (“Amount”).

IN Value Table it is possible to group (collapse) numerical values ​​by identical values ​​of certain columns using the method Collapse(,).

In relation to the previously shown code in the Tableau, you can calculate the value: TK.Collapse(“Day of the Week”, “Amount”).

Table of Values can be shown on user screen so that you can perform any actions with it. But unlike ListValues You can’t just call up a table on the screen from program code.

To display Table of Values on the screen, create a form attribute and assign a data type to it Table of Values.

After that, the resulting table should be displayed on the form.

In the form module at the end of the previously compiled algorithm (in the Procedure for Creating a Value Table), you should add:
ValueInFormData(TK, Table);

Universal collection Tree of values

a universal collection that is very similar to Table of Values. The difference from a table is that the rows of the tree can be subordinate to each other, i.e. some kind of hierarchy may be formed.

This can also be reflected on the screen. A value tree explicitly consists of a collection of rows and a collection of columns. There are two properties in the tree: Rows and Columns.

Since rows can be subordinate to each other, each row can have a Parent, as well as its subordinate rows.

Let's create the corresponding Tree command and its processing procedure.

Let's create in which there is one parent row and two subordinate rows.

Let's create the form attributes DerZn(data type – Value Tree).

For this attribute, we will create the Year and Month columns.

Move the corresponding element DerZn on the form.

At the end Procedures TreeOnServer() let's add:

ValueInFormData(TreeZn, DerZn);

Let's check what happened in user mode.

Using a button Add you can add new lines. They can also form a hierarchy.

To traverse all the elements of the value tree, we will need to use recursion, i.e. calling a procedure from itself. For example, processing a value tree might look like this:

This concludes our first acquaintance with universal collections.

In the next article we will look at what important mechanism a developer can use to simplify access to a directory element from program code.

Full syntax (click to expand)

ListValues

Description:

A list of values ​​is an object that is not saved in the database, which allows you to build dynamic sets of values ​​for solving interface problems and manipulate them (add, edit, delete elements, sort). It can be filled with values ​​of any type, i.e. In one list, the types of stored values ​​can be different. One example of using this object is organizing the selection of a specific document from a list of possible documents generated using a complex algorithm.

Collection items: ValueListElement

It is possible for an object to bypass the collection using the operator For each... From... Cycle. The traversal selects the elements of the collection.

It is possible to access a collection element using the [...] operator. The index of the element is passed as an argument ( numbering from 0).

Properties:

Methods:

Insert (Index, Value, Representation, Label, Image) Inserts a new element into the list of values ​​at the position at the specified index.SelectElement (Title, Element) Calls up a window for interactively selecting one of the elements included in the list of values. Element- the element to which the list of values ​​should initially be positioned during interactive selection. If the parameter value is not a value list element included in this list, positioning will not occur.UnloadValues()Creates an array and copies the values ​​of the list of values ​​elements into it.Add(Value, Representation, Label, Image)Adds a new element to the end of the list of values.LoadValues(ArrayValues)Loads a list of values ​​with the values ​​from the passed array. In this case, all previous elements of the list are deleted.Fill out Notes(Mark) Sets a mark for all elements of the list of values.Index(Element)Gets the index of an element in a list of values. If not found, then -1 is returned.Count() Gets the number of elements in the list of values.FindByValue(SearchValue) Searches for the value of an element in a list of values. If no element stores a value equal to the search value, then the value is returned Undefined. Find By ID(Identifier)Retrieves the list of values ​​element by identifier. If the element is not found, then it is returned Undefined. MarkItems(Header) Sets or unchecks (interactively) the items in the list of values. Returns True, if the "OK" button is pressed in the dialog, Lie- otherwise.Clear() Clears the list of values, removing all elements from it.ShowElementSelection(DescriptionNotificationsOnClosing, Title, Element) Calls up a window for interactively selecting one of the elements included in the list of values.ShowMark Items(Description of Closing Alerts, Header) Designed for interactively setting mark states for elements of the list of values.Get(Index)Retrieves the value by index. Works similar to the operator.Shift (Element, Offset) Shifts an element of the list of values ​​forward or backward by a specified number of positions.Copy() Creates a copy of a list of values.SortByValue(Direction) Sorts a list of values ​​in ascending or descending order of the values ​​stored by the elements. See example below.SortByView(Direction) Sorts the list of values ​​in ascending or descending order in alphabetical order of the representations of the elements included in the list of values. See example below.Remove (Element) Removes an element from a list of values, where Element- index of the element to be deleted, or the element itself.

Designers:

New List of Values
&OnClient Procedure ExecuteCode(Command) /// How to create a list of values ​​in 1s 8.3, 8.2 List = New ValueList; /// How to add an element to the list of values ​​in 1s 8.3, 8.2 // add method parameters:// - meaning // - performance // - mark (required) // - picture (required) List. Add( 1980, // element value "The year of Vasya's birth"// performance ) ; List. Add(1985, "The year of Yulia's birth") ; // values ​​can be of different types List. Add("Polina" , "Child's name" ) ; /// How to insert an element into a list of values ​​in 1s 8.3, 8.2 // insert into position No. 2 (elements are numbered starting from 0) // element with value 2010 and representation // "The year their joint daughter was born" List. Insert(2, 2010, "The year their joint daughter was born") ; /// How to bypass elements of the list of values ​​in 1s 8.3, 8.2 For Each Element From List Loop Report( Element. Representation + ": " + String(Element. Value) ) ; EndCycle ; /// How to clear the list of values ​​in 1s 8.3, 8.2 List. Clear() ; List. Add("Monday"); List. Add("Tuesday"); List. Add("Wednesday" ) ; /// How to find out the number of elements of a list of values, as well as /// get a list element by its index in 1s 8.3, 8.2 // numbering from scratch For Index = 0 By List. Quantity() - 1 Cycle Report(List[Index]) ; EndCycle ; /// How to find a list element by its value in 1s 8.3, 8.2 ValueTuesday = List. FindByValue("Tuesday"); /// How to find out the index of an element in a list in 1s 8.3, 8.2 Report(List.Index(ValueTuesday)) ; // 1, since numbering starts from zero /// How to sort a list by its values ​​in 1s 8.3, 8.2 // was: Monday, Tuesday, Wednesday List. SortByValue(SortDirection.Descending) ; // became: Wednesday, Monday, Tuesday /// How to remove an element from a list of values ​​in 1s 8.3, 8.2 // remove the first element // parameter: list element // or element index// you can do this List. Delete(List[ 0 ]) ; // or so // List.Delete(0); /// How to shift an element of a list of values ​​in 1s 8.3, 8.2 // move the zero element forward one position // was: Monday Tuesday List. Move(0, 1); // became: Tuesday Monday /// How to make a copy of a list in 1s 8 ListCopy = List. Copy() ; Colors = NewValueList; Colors. Add("Red" ) ; Colors. Add("Green" ) ; Colors. Add("Blue" ) ; /// How to upload list values ​​into an array in 1s 8.3, 8.2 ArrayColors = Colors. UnloadValues() ; /// How to load list values ​​from an array in 1s 8.3, 8.2 Colors. LoadValues(ArrayColors) ; End of Procedure /// How to make a modeless selection of a value from a list /// values ​​in 1s 8.3, 8.2&On the Client Procedure How to Make a Non-ModalValueSelection(Command) Colors = New ValueList; Colors. Add("Red" ) ; Colors. Add("Green" ) ; Colors. Add("Blue" ) ; // procedure AfterSelectingElement is defined just below AlertAfterElementSelection = New DescriptionAlerts( "AfterElementSelection", ThisObject ) ; Colors. ShowElementSelection( NotificationAfterElementSelection, "Choose your favorite color") ; End of Procedure &On Client Procedure After Element Selection(Element, Parameters) Export If Element<>Undefined Then Report(Element.Value) ; EndIf ; End of Procedure /// How to make a modeless marking of values ​​from a list /// values ​​in 1s 8.3, 8.2&On the Client Procedure How to Make a Non-Modal Marking of Values ​​(Command) Colors = New List of Values; Colors. Add("Red" ) ; Colors. Add("Green" ) ; Colors. Add("Blue" ) ; // procedure AfterMarkingElements is defined just below AlertAfterMarkingItems = New DescriptionAlerts( "AfterMarkingItems", ThisObject ) ; Colors. ShowTagItems( AlertAfterTagItems, "Choose your favorite colors") ; Colors. FillNotes(True) ; End of Procedure &On Client Procedure After Marking Elements(Elements, Parameters) Export If Elements<>Undefined Then For Each Color From Elements Cycle If Color. Mark Then Report(Color.Value) ; EndIf ; EndCycle ; EndIf ; End of Procedure /// How to make a modal selection of a value from a list in 1s 8.3, 8.2&On the Client Procedure How to Make a ModalValueSelection(Command) Colors = New ValueList; Colors. Add("Red" ) ; Colors. Add("Green" ) ; Colors. Add("Blue" ) ; SelectColor = Colors. SelectItem( "Choose your favorite color") ; If SelectColor<>Undefined Then Report(SelectColor.Value) ; EndIf ; End of Procedure /// How to modally mark values ​​from a list /// values ​​in 1s 8.3, 8.2&On the Client Procedure How to Make a Modal Mark of Values ​​(Command) Colors = New List of Values; Colors. Add("Red" ) ; Colors. Add("Green" ) ; Colors. Add("Blue" ) ; If Colors. MarkItems( "Choose your favorite colors") Then For Each Color From Color Cycle If Color. Mark Then Report(Color.Value) ; EndIf ; EndCycle ; EndIf ; // this is how you can set all the marks at once // list to a specific value Colors. FillNotes(True) ; End of Procedure /// Download and run these examples on your computer

In this unit we will get acquainted with a new data type of the 1C language, which is called ListValues.

List of values is an array, but with great capabilities. And if so, then it is also a collection of data and you can put a set of numbers in it:

// created a list of values

List.Add(23);

So far everything looks a lot like an array, doesn't it? But let's move on. To now display these numbers to the user, we write:

// loop through each element of the list For Each Element From List Loop Report(Element.Value);

// display the value of the element EndCycle;

And here is the first difference. The elements of a regular array are the numbers themselves. And access to the current value of an element List we get using the option Meaning, using the construction Element. Meaning.

Let's look at some additional features Values.

Can sort

For example, we can easily sort a list of values. The list can do this itself:

List.SortByValue(SortDirection.Age); In the case of a regular array, we would have to implement one of the sorting algorithms (for example, "bubble").

Can search

The list of values ​​can search for its elements on its own. Let's say we want to find out whether a number entered by the user is present in our list:

EnterNumber(Number); // entered a number from the user FoundElement = List. FindByValue(Number);

If FoundItem = Undefined Then OpenValue("Number " + Number + " not found in the list!"); Otherwise

OpenValue("Number " + Number + " is in the list. Here it is:" + FoundElement.Value);

endIf; ListValues

Supports insertion and deletion

A list of values, like an array, supports insertion and removal of elements:

List. Insert(0, 1000);

// insert a thousand at the very beginning of the list List.Delete(O); // and immediately remove it from the list List. Insert(List.Quantity(), 13);

// insert 13 at the end of the list Report(List[List.Quantity() - 1]);

// display the last element of the list (13)

Supports square brackets

As you already understood, the numbering of list elements also starts from zero, and we can access any element by its index using square brackets:

Report(List.Value); // display the second element of the list Thus, we can go through all the elements of the list like this:

For A = 0 By List.Quantity() - 1 Loop Report(List[A].Value);

EndCycle;

Can create a copy of itself

The list of values ​​has just a great feature - it can create a copy of itself:

ListCopy = List.Copy();

// made a copy of the list // now we can safely change the Copy of the List // while the original list will not change Copy of the List. Value = 123456;

Can turn into an array

And finally, you can easily dump all the elements of the list into an array at any time:

ArrayNumbers = List. UnloadValues();

// dumped into an array

For A = 0 By ArrayNumber.Quantity() - 1 Cycle

Report(ArrayNumbers[A]); // no need to specify // option Value

EndCycle;

To practice and consolidate the material of this unit, it is recommended to complete the following task.

It is necessary to enter 5 numbers from the user, find the largest and smallest of them and display them to the user. The solution must use a list of values.

Solution

List = New ValueList;

// created a list of values ​​For A = 1 Through 5 Cycle Number = 0;

EnterNumber(Number); // enter a number from the user List.Add(Number); // and add it to the list EndCycle; // and so on 5 times

// sort the list of numbers

List. Sort ByValue(SortDirection.Age);

// after sorting, the first element of the list is // the smallest element, and the last is the largest OpenValue("Smallest element" + List +

", and the largest " + List[List.Quantity() - 1]);




Top