xxxxxxxxxx
contract Array {
uint[4] public fixedArray = [1,2,3,4];
uint[] public dynamicArray=[1];
uint[][2] public twoDArray=[[1,2][1,2,3]] //2D array with two element.
//add in array.Array is 0 based
function addNumber(uint _number) public {
dynamicArray.push(_number)
}
//find length of array
function arrayLength() public view returns(uint){
return dynamicArray.length;
}
}
xxxxxxxxxx
// Create array
address[] addresses
uint256[] myArray
// Add to array
function add() internal {
myArray.push(123);
}
// Remove from array
function removeUnordered() internal {
myArray[index] = myArray[myArray.length - 1];
myArray.pop();
}
function removeOrdered() internal {
// WARN: This unbounded for loop is an anti-pattern
for(uint256 i = index; i < myArray.length-1; i++){
myArray[i] = myArray[i+1];
}
myArray.pop();
}
xxxxxxxxxx
uint[5] arr; //fixed size array
string[] arr; //dynamic array, no fixed size
xxxxxxxxxx
uint32[3] fixedLengthArray = new uint[](3)
// initialise empty fixed length array
uint32[] dynamicLengthArray;
// initialised empty fixed length array
xxxxxxxxxx
pragma solidity ^0.6.0;
contract samplyArray {
uint[] public myArray; //this is a dynamic array of type uint
uint[] public myArray2 = [1, 2, 3]; //this is a dynamic array with 1, 2 and 3 as default values
uint[10] public myFixedSizeArray; //this is a fixed size array of type uint
}
xxxxxxxxxx
uint[] array;
function showArray() external view returns(uint[] memory ){
return array;
}
function addArray(uint _a) external ( ){
array.push(_a);
}