ReasonJun

Solidity : array example 본문

Blockchain/Solidity

Solidity : array example

ReasonJun 2023. 10. 16. 17:39
728x90
// SPDX-License-Identifier: MIT
pragma solidity >= 0.7.0 < 0.9.0;

contract Array {
    uint[] public arr;
    uint[] public arr2 = [1, 2, 3]; 

    uint[10] public myFixedSizeArr; // 0

    function get(uint i) public view returns (uint) {
        return  arr[i];
    }

    function get() public view returns (uint[] memory) {
        return  arr;
    }

    function push(uint i) public {
        arr.push(i);
    }

    function pop() public {
        arr.pop();
    }

    function getLength() public view returns (uint) {
        return arr.length;
    }

    function remove(uint index) public {
        // The length of the array does not change.
        // Reset the index value to the default value.
        // 0
        
        delete arr[index];
    }

    function examples() external pure {
        uint[] memory a = new uint[](5); // size is 5
    }
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

// struct Todo {
//     string text;
//     bool completed;
// }

import "./1.sol";

contract Todos {
    Todo[] public todos;


    function create(string calldata _text) public {
        todos.push(Todo(_text, false));

        // todos.push(Todo({text:_text, completed: false}));

        // Todo memory todo;

        // todo.text = _text;

        // todos.push(todo);
    }

    function get(uint _index) public view returns (string memory text, bool completed) {
        Todo storage todo = todos[_index];
        return (todo.text, todo.completed);
    }


    function updateText(uint _index, string calldata _text) public {
        Todo storage todo = todos[_index];
        todo.text = _text;
    }

     function toggleCompleted(uint _index) public {
        Todo storage todo = todos[_index];
        todo.completed = !todo.completed;
    }


}
728x90

'Blockchain > Solidity' 카테고리의 다른 글

Solidity : constructor  (0) 2023.10.16
Solidity : Event  (0) 2023.10.16
Solidity : Difference between memory and calldata  (0) 2023.10.16
Solidity : mapping  (0) 2023.10.16
Solidity : keccak256  (0) 2023.10.16
Comments