如何在实体中将多维数组中的元素推送到特定坐标?
How to push element in multiple dimension array at certain coordinate in solidity?
我有一个这样的数组:
Animal[3][2][] animalArray.
我有了一个新的元素后"dog",如何把它推到like
的某个地方
Animal[2][1][]?
我试过了
animalArray[2][1].push(dog);
它总是给我错误。
下面是有问题的代码:
pragma solidity ^0.4.0;
contract Zootest {
struct Zoo {
uint state;
Animal[][2][3] animalarray;
uint price;
}
struct Animal {
uint quantity;
address Address;
}
mapping (uint => Zoo) zoo;
function openZoo (uint index) {
Zoo memory newZoo = Zoo({
state: 1,
price: 0,
animalarray: new Animal[][2][3](0)
});
zoo[index] = newZoo;
}
function enterZoo (uint index, uint x, uint y, uint quantity) public {
Animal memory newAnimal = Animal({
Address:msg.sender,
quantity:quantity
});
zoo[index].price = zoo[index].price+msg.value;
zoo[index].animalarray[x][y].push(newAnimal);
}
}
也许您打算这样声明数组:
Animal[][2][3] animalArray;
来自https://solidity.readthedocs.io/en/v0.4.24/types.html#arrays(我强调):
An array of fixed size k
and element type T
is written as T[k]
, an array of dynamic size as T[]
. As an example, an array of 5 dynamic arrays of uint
is uint[][5]
(note that the notation is reversed when compared to some other languages). To access the second uint in the third dynamic array, you use x[2][1]
(indices are zero-based and access works in the opposite way of the declaration, i.e. x[2]
shaves off one level in the type from the right).
我有一个这样的数组:
Animal[3][2][] animalArray.
我有了一个新的元素后"dog",如何把它推到like
的某个地方 Animal[2][1][]?
我试过了
animalArray[2][1].push(dog);
它总是给我错误。
下面是有问题的代码:
pragma solidity ^0.4.0;
contract Zootest {
struct Zoo {
uint state;
Animal[][2][3] animalarray;
uint price;
}
struct Animal {
uint quantity;
address Address;
}
mapping (uint => Zoo) zoo;
function openZoo (uint index) {
Zoo memory newZoo = Zoo({
state: 1,
price: 0,
animalarray: new Animal[][2][3](0)
});
zoo[index] = newZoo;
}
function enterZoo (uint index, uint x, uint y, uint quantity) public {
Animal memory newAnimal = Animal({
Address:msg.sender,
quantity:quantity
});
zoo[index].price = zoo[index].price+msg.value;
zoo[index].animalarray[x][y].push(newAnimal);
}
}
也许您打算这样声明数组:
Animal[][2][3] animalArray;
来自https://solidity.readthedocs.io/en/v0.4.24/types.html#arrays(我强调):
An array of fixed size
k
and element typeT
is written asT[k]
, an array of dynamic size asT[]
. As an example, an array of 5 dynamic arrays ofuint
isuint[][5]
(note that the notation is reversed when compared to some other languages). To access the second uint in the third dynamic array, you usex[2][1]
(indices are zero-based and access works in the opposite way of the declaration, i.e.x[2]
shaves off one level in the type from the right).