Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
Depending on what "the presence of an element in an array" means. If the array is static, then the element is always there. And if it is dynamic, then in D you can check that such indexes exist, like this for example:
auto array = new int[10][20];
/* ... */
writeln(array.length); // напишет 20
writeln(array[0].length); // напишет 10
// проверим, что есть элемент [3][4]
if ((array.length < 4) || (array[0].length < 3))
writeln("fail"); // а вот это не напишет, значит элемент [3][4] доступен.
auto array = new int[10][20];
/* ... */
bool isElementExist(int[10][] a, size_t indexA, size_t indexB)
{
try { auto i = a[indexA][indexB]; }
catch { return false; }
return true;
}
/* ... */
writeln(isElementExist(array, 300, 4)); // false
writeln(isElementExist(array, 3, 4)); // true
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question