Error: "Array Index Out of Range" in Multidimensional Array

Error: array index out of range in multidimensional array

As the commentators @C_X & @MartinR say, your array is empty. Here's how to initialise it as you want...

var tile = [[Int]](count:4, repeatedValue: [Int](count: 4, repeatedValue: 0))

for index in 0 ..< 4 {
for sindex in 0 ..< 4 {
tile[index][sindex] = 0 // no error here now...
print("\(index) \(sindex)")
}
}

...of course, the for loops are now redundant, if you just want zeroes!

Index out of range for multi dimensional array

for (int i = 0; i < posAr_ItemManager.GetLength(0); i++)
{
for (int j = 0; j < posAr_ItemManager.GetLength(1); j++)
{
for (int z = 0; z < posAr_ItemManager.GetLength(2); z++)

look very carefully at the middle tests. Also, consider hoisting the GetLength tests so you only do them once per dimension. Perhaps:

int rows = posAr_ItemManager.GetLength(0), columns = posAr_ItemManager.GetLength(1),
categories = posAr_ItemManager.GetLength(2);

for (int row = 0; row < rows; row++)
{
for (int col = 0; col < columns; col++)
{
for (int cat = 0; cat < categories; cat++)

Python multidimensional array list index out of range

In case you just want to remove some elements from the list you could iterate it from end to beginning which would allow you to remove items without causing IndexError:

list2 = [["John Doe","1"],["Jane Smith","0"],["Firstname Lastname","1"]]

for i in range(len(list2) - 1, -1, -1):
list2[i][1] = int(list2[i][1])
if int(list2[i][1]) == 1:
list2.pop(i)

print(list2)

Output:

[['Jane Smith', 0]]

Of course you could just use list comprehension to create a new list:

list2 = [[x[0], int(x[1])] for x in list2 if int(x[1]) != 1]


Related Topics



Leave a reply



Submit