Collections
Arrays, structures, maps, value lists and value tables of the built-in language.
18 questions
JuniorTheoryVery commonHow does an array differ from a structure, and when do you use each one?
How does an array differ from a structure, and when do you use each one?
An array is an ordered list read by index from 0, Товары[0], and fits many values of one kind. A structure stores values under keys that are names, read as Товар.Цена, and fits one record with named fields such as a price and a quantity.
Common mistakes
- ✗Counting array indexes from 1: the first element is
Товары[0] - ✗Keeping a list of similar items in a structure with keys such as
Товар1,Товар2: a list belongs in an array - ✗Expecting an array to hold values of only one type: its elements may be of any types
Follow-up questions
- →How do you check that a structure has a key before reading it?
- →What do you use when the keys are dates or references rather than names?
JuniorTheoryVery commonWhich universal collections does the 1C language have, and how do you walk them?
Which universal collections does the 1C language have, and how do you walk them?
Массив, Структура, Соответствие, СписокЗначений, ТаблицаЗначений, ДеревоЗначений and fixed variants. They are walked with Для Каждого … Из … Цикл, a tree through its Строки; a structure or map yields pairs with Ключ and Значение.
Common mistakes
- ✗Walking a structure with
Для Каждогоand expecting bare values: each element hasКлючandЗначение - ✗Counting a value table as a database table: it lives only in memory
- ✗Walking a value tree like a flat list: its rows are reached through
Строки, level by level
Follow-up questions
- →Why is it unsafe to add to or delete from a collection inside
Для Каждого? - →What do fixed collections such as
ФиксированныйМассивforbid?
JuniorTheoryCommonWhat does the program print (lines separated by spaces)?
What does the program print (lines separated by spaces)?
ручка Да
Open full question →Common mistakes
- ✗Expecting
Вставитьto replace the element at the index: it inserts a new one and shifts the rest right - ✗Comparing the result of
Найтиwith-1, as in other languages: a missing value givesНеопределено
Follow-up questions
- →Why does deleting elements in a forward
Дляloop run past the end? - →How do you clear the whole array in one call?
JuniorTheoryCommonWhat does the program print (lines separated by spaces)?
What does the program print (lines separated by spaces)?
3 40 2
Open full question →Common mistakes
- ✗Expecting array indexes to start at 1: the first element is
Цены[0], soЦены[1]is the second - ✗Mixing up
Количество()andВГраница(): the first is the number of elements, the second the last index, one less
Follow-up questions
- →What happens when the program reads
Цены[3]? - →What does
ВГраница()return for an empty array?
JuniorTheoryCommonWhat does the program print (lines separated by spaces)?
What does the program print (lines separated by spaces)?
2 300 200
Open full question →Common mistakes
- ✗Expecting a merged row to keep the first row's sum: the columns in the second parameter of
Свернутьare added up - ✗Expecting
Свернутьto merge the whole table into one row: rows are grouped by the columns in the first parameter
Follow-up questions
- →What happens to the columns named in neither parameter of
Свернуть? - →Why call
Скопировать()before collapsing a table someone passed in?
JuniorTheoryCommonWhat does the program print (lines separated by spaces)?
What does the program print (lines separated by spaces)?
2 клей
Open full question →Common mistakes
- ✗Expecting a change to a found row to leave the table alone:
НайтиСтрокиreturns the rows themselves, not copies - ✗Mixing up
НайтиСтрокиandНайти: the first returns an array of every matching row, the second only the first one
Follow-up questions
- →Why does an index on a column speed up
НайтиСтрокиon a large table? - →What does
Найтиreturn when no row matches?
JuniorTheoryCommonWhat does the program print (lines separated by spaces)?
What does the program print (lines separated by spaces)?
3 10 Да
Open full question →Common mistakes
- ✗Expecting map keys to ignore letter case, like structure keys: strings compare exactly, so
"Ручка"is a new key - ✗Expecting an error when a missing key is read: a map returns
Неопределено
Follow-up questions
- →How do you walk all pairs of a map with
Для Каждого? - →Why should code not depend on the order of a map walk?
JuniorTheoryCommonWhat does the program print (lines separated by spaces)?
What does the program print (lines separated by spaces)?
2 20 Нет
Open full question →Common mistakes
- ✗Expecting
ценаandЦенаto be two keys: a structure key ignores letter case, andВставитьreplaces the value - ✗Expecting
Свойствоto fail on a missing key: the method returnsЛожь, while readingТовар.Скидкаraises the error
Follow-up questions
- →What happens when the program reads
Товар.Скидкаdirectly? - →How does
Свойствоhand back the value of a key it finds?
JuniorTheoryCommonWith a date as the key, should stock by arrival date go in a structure or a map?
With a date as the key, should stock by arrival date go in a structure or a map?
In a map: its key can be a date, while a structure key is only a name
Common mistakes
- ✗Making a date a structure key through a string: a structure key is a name, and a string like
01.11.2024cannot be one - ✗Expecting a map to take only strings and numbers as keys: its key is a value of any type, a date included
Follow-up questions
- →When is a structure the better choice than a map?
- →What happens when a missing key is read from each of the two collections?
JuniorTheoryCommonWhat is a СписокЗначений for, and what does each of its items hold?
What is a СписокЗначений for, and what does each of its items hold?
A value list is an ordered list whose item holds a value plus its presentation and a check mark. It serves where a user sees or picks values, such as a choice list of a form field or a dialog for checking items, and it sorts by value or presentation.
Common mistakes
- ✗Using a value list as a key lookup: it has no keys, and
НайтиПоЗначениюwalks its items - ✗Expecting an item to hold only the value: it also keeps a presentation and a check mark
Follow-up questions
- →When is an array a better choice than a value list?
- →How do you find the item that holds a given value in a value list?
JuniorTheoryCommonWhat does the program print (lines separated by spaces)?
What does the program print (lines separated by spaces)?
2 Да 2
Open full question →Common mistakes
- ✗Expecting a row with no cells assigned not to be added:
Добавить()adds it at the moment of the call - ✗Expecting an unfilled cell to hold an empty string: it holds
Неопределено
Follow-up questions
- →How do you give a value table column a type when you add it?
- →How do you walk all rows of a value table?
MiddleTheoryCommonHow do you copy an array so that changing the copy leaves the original intact?
How do you copy an array so that changing the copy leaves the original intact?
Копия = Товары copies only the reference. A real copy is a new array: add each element in a Для Каждого loop, or write Новый Массив(Новый ФиксированныйМассив(Товары)). The copy is shallow: a structure inside stays shared by both arrays.
Common mistakes
- ✗Expecting
Копия = Товарыto duplicate the array: both names point to one array - ✗Looking for a
Скопироватьmethod on an array: a value table has one, an array does not - ✗Expecting a copied array to duplicate the structures inside it: the copy is shallow
Follow-up questions
- →How would you make a deep copy of an array that holds structures?
- →Why does
Значon a parameter not protect an array from changes?
MiddleDebuggingCommonWhy does deleting array elements in a forward Для loop break?
Why does deleting array elements in a forward Для loop break?
Удалить shifts the later elements one place left, so the loop skips the element that moved into the current index, and the bound computed once at the start runs past the shrunk array. Walk from ВГраница() down to 0 instead.
Common mistakes
- ✗Expecting the loop to see each element once after
Удалить: the remaining elements shift and one is skipped - ✗Thinking the
Поbound is re-read on every pass: it is computed once when the loop starts
Follow-up questions
- →How would you remove the zeros without changing the caller's array?
- →Why is deleting elements inside
Для Каждогоover the same array also unsafe?
MiddleTheoryCommonWhat does Свернуть do with the columns named in neither of its parameters?
What does Свернуть do with the columns named in neither of its parameters?
Свернуть deletes them: only the grouping columns and the summed ones remain, so a price or warehouse column disappears, and the table itself is changed. To keep the source intact, collapse a copy made with Скопировать().
Common mistakes
- ✗Expecting unnamed columns to keep the first row's values:
Свернутьdeletes them - ✗Collapsing a table a caller passed in:
Свернутьchanges it in place, so work on aСкопировать()copy - ✗Expecting
Свернутьto return a new table: it returns nothing and changes the table it is called on
Follow-up questions
- →How do you collapse by two columns and sum two others in one call?
- →Why might you add a column of ones before collapsing a table?
MiddleTheoryCommonHow does Найти differ from НайтиСтроки on a value table?
How does Найти differ from НайтиСтроки on a value table?
Найти(Значение, Колонки) returns the first row holding the value in any listed column, or Неопределено. НайтиСтроки(Отбор) takes a structure of column and value pairs and returns an array of all rows matching every pair, empty if none match.
Common mistakes
- ✗Checking the result of
НайтиСтрокиagainstНеопределено: it always returns an array, empty when nothing matches - ✗Expecting
Найтиto require a match in every listed column: one column holding the value is enough - ✗Treating found rows as copies: both methods return the table's own rows
Follow-up questions
- →How does an index on the filter columns change the speed of
НайтиСтроки? - →How do you find rows where a number column is greater than a given value?
MiddleTheoryOccasionalHow does a ФиксированнаяСтруктура differ from a structure, and where is it needed?
How does a ФиксированнаяСтруктура differ from a structure, and where is it needed?
Its keys and values are fixed at creation: Вставить and assigning a property fail. It protects shared values, such as what a common module with reused return values returns. The protection is shallow: an array inside still changes.
Common mistakes
- ✗Expecting a fixed structure to freeze the arrays inside it: only its own keys and values are fixed
- ✗Thinking the values of a fixed structure may still be reassigned: any property assignment fails
- ✗Returning a plain structure from a common module with reused return values: a caller could change the cached value
Follow-up questions
- →How do you get a changeable copy of a fixed structure?
- →Why is it dangerous to change a value cached by a common module with reused return values?
SeniorTheoryOccasionalHow do you speed up row lookups in a large value table searched many times?
How do you speed up row lookups in a large value table searched many times?
Add an index on the filter columns with Индексы.Добавить and search with НайтиСтроки instead of a loop; for one-to-one lookups build a map keyed by the value. An index costs memory and slows row additions, so it pays off on repeated searches.
Common mistakes
- ✗Scanning the rows in a loop on every lookup:
НайтиСтрокиwith an index avoids the full scan - ✗Indexing every column just in case: each index costs memory and slows row additions
- ✗Ignoring a map for one-to-one lookups: a map keyed by the value finds the row at once
Follow-up questions
- →When does building an index cost more than it saves?
- →How would you look up rows by two columns at once?