一、string如何存储中文字符
二、固长数组
三、动态数组
四、二维数组
五、动态二维数组
先看一段代码:
pragma solidity >=0.4.22 <0.6.0;
contract Test {
string name = "张三";
function getLength() public view returns(uint256){
return bytes(name).length;
}
}
memory:临时变量,定义在函数内的变量默认为memory,一些版本的编译器要求定义在函数内的临时变量必须加上memory关键字。
storage:永久写入区块链的变量,就是定义在函数之外的状态变量。
pragma solidity ^0.7.0;
contract Test {
bytes6 name = 0x112233445566;
function toNe() view public returns(bytes memory) {
bytes memory newName = new bytes(name.length);
for(uint i = 0;i < name.length;i++){
newName[i] = name[i];
}
return newName;
}
}
pragma solidity ^0.7.0;
contract Test {
bytes6 name = 0x112233445566;
bytes2 name2 = 0x7a68;
function toNe() view public returns(bytes memory) {
bytes memory newName = new bytes(name.length);
for(uint i = 0;i < name.length;i++){
newName[i] = name[i];
}
return newName;
}
function BytestoString() view public returns(string memory){
bytes memory newName = new bytes(name2.length);
for(uint i = 0;i < name2.length;i++){
newName[i] = name2[i];
}
return string(newName);
}
}
pragma solidity ^0.4.0;
contract TestArray {
uint[5] arr;
function getArrayContent() view public returns(uint[5]){
return arr;
}
}
function Init() public {
arr[0] = 100;
arr[1] = 200;
}
pragma solidity ^0.4.0;
contract TestArray {
uint[5] arr = [1,2,3,4,5];
function Init() public {
arr[0] = 100;
arr[1] = 200;
}
function getArrayContent() view public returns(uint[5]){
return arr;
}
}
function getGrades() view public returns(uint) {
uint totall = 0;
for(uint i = 0;i < arr.length;i++){
totall = totall + arr[i];
}
return totall;
}
先看代码:
pragma solidity ^0.4.0;
contract TestArray {
uint[] arr;// 定义一个可变长度的数组
function getContent() view public returns(uint[]){
return arr;
}
function getLength() view public returns(uint){
return arr.length;
}
}
pragma solidity ^0.4.0;
contract TestArray {
uint[] arr = [1,2,3,4,5];// 定义一个可变长度的数组
function getContent() view public returns(uint[]){
return arr;
}
function getLength() view public returns(uint){
return arr.length;
}
function setArr() public{
arr[0] = 100;
arr[1] = 200;
}
function push() public{
arr.push(6);
arr.push(7);
arr.push(8);
}
}
function changeLength(uint length) public{
arr.length = length;
}
可以看到,之后添加的空间上都补上了0
超过给定大小之后的值全部都被抹去了。
在java中的二维数组,例如int[3][2] arr;表示arr有3个元素,每个元素都是一个长度为2的数组。
但是在solidity里,完全相反,例如uint[3][2] arr;表示arr有2个元素,每个元素都是一个长为3的数组。
给出二维数组有关代码:
pragma solidity ^0.4.0;
contract TestArray {
uint[2][3] arr = [[1,2],[3,4],[5,6]];
function getLength() view public returns(uint){// 获取数组的长度
return arr.length;
}
function getLength2() view public returns(uint){// 获取二维数组的长度
return arr[0].length;
}
function getContent() view public returns(uint[2][3]){// 获取二维数组中所有数的值
return arr;
}
function add() view public returns(uint){// 将二维数组中所有数相加
uint count = 0;
for(uint i = 0;i < getLength2();i++){
for(uint j = 0;j < getLength();j++){
count += arr[j][i];
}
}
return count;
}
}
先给出代码:
pragma solidity ^0.4.0;
contract TestArray {
uint[][] arr = [[1,2],[3,4],[5,6]];
function setLength(uint length) public{
arr.length = length;
}
function getLength() view public returns(uint){
return arr.length;
}
}