Skip to content

Latest commit

 

History

History
75 lines (63 loc) · 1.06 KB

File metadata and controls

75 lines (63 loc) · 1.06 KB

Array

固定長度

uint[8] array1;
bytes1[5] array2;
address[100] array3;

動態長度

uint[] array4;
bytes1[] array5;
address[] array6;
bytes array7;

memory動態數組

uint[] memory array8 = new uint[](5);
bytes memory array9 = new bytes(9);

array8[0] = 1;
array8[1] = 3;
array8[2] = 4;

Array函數

length: Array長度 push: 在最後加0 push(x): 在最後加x pop: 移除Array最後一個字

Struct

struct Student{
    uint256 id;
    uint256 score; 
}

給Struct賦值有4種方法

  1. 在function中創建一個storage的struct引用
function initStudent1() external{
    Student storage _student = student; // assign a copy of student
    _student.id = 11;
    _student.score = 100;
}
  1. 直接引用
function initStudent2() external{
    student.id = 1;
    student.score = 80;
}
  1. Constructor
// 方法3:构造函数式
function initStudent3() external {
    student = Student(3, 90);
}
  1. Key Value
function initStudent4() external {
    student = Student({id: 4, score: 60});
}