1. pragma solidity ^0.4.0;
  2.  
  3. contract test {
  4. uint [5] T =[1,2,3,4,5] ;//固定长度的数组:可修改数组内值大小,不支持push,不可更改长度
  5.  
  6. /*
  7. contract test {
  8. uint [] T = new uint[](5); //ParserError: Expected identifier but got 'storage'
  9. //全局数组,默认创建在storage中,无法创建在memory中,长度可修改
  10. function setlength(uint aa){
  11. uint [] memory a = new uint[](5);
  12. bytes [] memory b = new bytes[](5);
  13. //TypeError: Type bytes memory[] memory is not implicitly
  14. //convertible to expected type bytes storage ref[] storage pointer.
  15. //函数内创建数组,需要指定存储在memory中,a.length不可更改
  16. //a.length = 10; // TypeError: Expression has to be an lvalue
  17. a[2] = 5;
  18. }
  19. }
  20. */
  21. function setValue(uint para){
  22. T[0] = para;
  23. }
  24. /*
  25. uint [] T =[1,2,3,4,5] ; //动态长度的数组:可修改数组内值大小,支持push,可更改长度
  26.  
  27. /*
  28. uint [] T1 = new uint[](5);
  29.  
  30. constructor() public{
  31. for (uint i;i<T1.length;i++){
  32. T1[i] = i;
  33. }
  34. }
  35. */
  36. function setlenth(uint para){
  37. T.length = 6;
  38. }
  39.  
  40. function addvalue(uint para){
  41. T.push(3);
  42. }
  43. */
  44.  
  45. function setValue1(){
  46. T[0] = 10;
  47. }
  48. function getValue() constant returns(uint){
  49. return T.length;
  50. }
  51.  
  52. function get1Value() constant returns(uint){
  53. return T[0];
  54. }
  55. }
  56.  
  57. pragma solidity ^0.4.0;
  58.  
  59. contract test {
  60. //二维数组
  61. uint [2][3] T = [[1,2],[2,3]];
  62. /*
  63. 1,2
  64. 2,3
  65. 0,0
  66. */
  67.  
  68. uint [2][] T1 = new uint[2][](5);
  69.  
  70. /*[i][j] T.length = j
  71. 0,0
  72. 0,0
  73. 0,0
  74. 0,0
  75. 0,0
  76. */
  77. function getlenth() constant returns(uint){
  78. return T.length; //
  79. }
  80.  
  81. function getlenth1() constant returns(uint){
  82. return T1.length; //
  83. }
  84.  
  85. function get1lenth() constant returns(uint[2][]){ //输出二维数组
  86. return T1; //
  87. }
  88. }
  89.  
  90. pragma solidity ^0.4.0;
  91.  
  92. contract test {
  93. function setValue() public{
  94.  
  95. g([1,2,3]); //uint8
  96. //TypeError: Invalid type for argument in function call.
  97. //Invalid implicit conversion from uint8[3] memory to uint256[3] memory requeste
  98.  
  99. g([uint(1),2,3]);
  100. }
  101. //uint 256
  102. function g(uint[3] data){
  103. }
  104.  
  105. uint [] x1 = [uint(1),2,3];
  106. //storage:可变数组 memory:固定数组
  107. function get1() public{
  108. uint [] memory x = [uint(1),2,3];
  109. //memory:可变数组 memory:固定数组
  110. //在函数内部,memory类型的固定长度的数组不可直接赋值给storge/memory类型的可变数组
  111. //TypeError: Invalid type for argument in function call.
  112. //Invalid implicit conversion from uint8[3] memory to uint256[3] memory requested
  113. }
  114. function get1() public constant returns(uint[]){
  115. return x1; //uint256[]: 1,2,3 == [1,2,3]
  116. //uint [] memory x = [uint(1),2,3];
  117. //TypeError: Invalid type for argument in function call.
  118. //Invalid implicit conversion from uint8[3] memory to uint256[3] memory requested
  119. }
  120.  
  121. pragma solidity ^0.4.0;
  122.  
  123. contract test {
  124.  
  125. bytes3 public b = 0x123456; //bytes3: 0x123456
  126.  
  127. byte[3] public b1; //b1=0x000000 可直接通过索引进行查询
  128.  
  129. bytes public b2= new bytes(3); //bytes: 0x000000 == byte[] public b3 = new byte[](3)
  130.  
  131. byte[] public b3 = new byte[](3); // 0x000000 可直接通过索引进行查询
  132.  
  133. /*
  134. function setb() public{
  135. b[0] =0x01;
  136. }
  137. */
  138.  
  139. function setb1() public{
  140. b1[0] =0x01;
  141. }
  142.  
  143. function setb2(bytes aa)public {
  144. for (uint i;i<aa.length;i++){
  145. b2.push(aa[i]);
  146. }
  147. }
  148. }
  149. /*
  150. 总结:创建固定大小字节数组/可变大小字节数组
  151. 固定大小字节数组:
  152. bytes0~bytes32:长度不可变,内容不可修改
  153. byte[len] b :长度不可变,内容可以修改
  154. 可变大小字节数组:可直接通过索引进行查询
  155. bytes b = new bytes(len) == byte[] b = new byte[](len)
  156. 特殊的可变字节数组:
  157. string :bytes() 通过bytes转换,length获取长度,通过索引修改相应的字节内容
  158. 固定大小字节数组 -> 可变大小字节数组
  159. bytes3 a;
  160. bytes [] b = new bytes[a.length]
  161. for (uint i;i<a.length;i++){
  162. b[i] = a[i]
  163. }
  164. return b;
  165.  
  166. uint [5] T =[1,2,3,4,5] ;//固定长度的数组:可修改数组内值大小,不支持push,不可更改长度
  167. uint [] T =[1,2,3,4,5] ; //动态长度的数组:可修改数组内值大小,支持push,可更改长度
  168.  
  169. uint [] T = new uint[](5); //ParserError: Expected identifier but got 'storage'
  170. //全局数组,默认创建在storage中,无法指定在memory中,长度可修改
  171.  
  172. //函数内创建数组,需要指定存储在memory中,a.length不可更改
  173.  
  174. uint [] x1 = [uint(1),2,3] //[uint(1),2,3] = [1,2,3];
  175. //storage:可变数组 memory:固定数组
  176. */
  177. }

ethereum(以太坊)(十一)--字节数组(二)的更多相关文章

  1. ethereum(以太坊)(十一)--字节数组(一)

    pragma solidity ^0.4.0; contract byte1{ /* 固定大小字节数组(Fixed-size byte arrays) 固定大小字节数组可以通过bytes1,bytes ...

  2. ethereum(以太坊)(一)

    从这周开始,开始学习以太坊开发--solidity,开始决定往区块链方向发展,毕竟区块链技术应用广泛.一开始接触solidity开发语言不太习惯,毕竟一直在学习python语法,有很多都不能接受.有难 ...

  3. 创建自己的加密货币MNC——以太坊代币(二)

    创建一个基于以太坊平台的分红币MNC,根据持有的代币数量,进行分红的算法.github地址: https://github.com/lxr1907/MNC 1.使用以太坊根据比例换购token MNC ...

  4. ethereum(以太坊)(四)--值传递与引用传递

    contract Person { string public _name; function Person() { _name = "liyuechun"; } function ...

  5. 以太坊开发教程(二) 利用truffle发布宠物商店 DAPP 到 以太坊测试环境Ropsten

    1.环境安装 1) node安装 设置镜像地址: curl --silent --location https://rpm.nodesource.com/setup_8.x | bash -下载安装 ...

  6. ethereum(以太坊)(基础)--容易忽略的坑(二)

    pragma solidity ^0.4.0; contract EMath{ string public _a="lin"; function f() public{ modif ...

  7. ethereum(以太坊)(十二)--应用(二)__投票(基础总和)

    编写应用合约之前,先弄清它的逻辑,有助于我们更好的部署合约 pragma solidity ^0.4.21; pragma experimental ABIEncoderV2; contract vo ...

  8. ethereum(以太坊)(十二)--应用(一)__集资(构造函数/映射)

    pragma solidity ^0.4.4; contract funder{ //0xca35b7d915458ef540ade6068dfe2f44e8fa733c //0x14723a09ac ...

  9. ethereum(以太坊)(二)--合约中属性和行为的访问权限

    pragma solidity ^0.4.0; contract Test{ /* 属性的访问权限 priveta public internal defualt internal interlnal ...

随机推荐

  1. BNU 26349——Cards——————【区间dp】

    题目大意:给你n张牌,排成一排放在桌子上,可以从左端拿也可以从右端拿.现在有A,B两人轮流取牌,A先取,两人足够聪明,即都想取最大的牌总和,问A能取到的最大值. 解题思路:定义dp[i][j][k]. ...

  2. .net 视图格式化

    昨天在做一个功能,要在界面上按照规定的格式显示一个时间,如果直接在expression那里格式化的话(如下:) @Html.DisplayFor(c => Convert.ToDateTime( ...

  3. C语言答案解析

    1.设整型变量 a=2,则执行下列语句后,浮点型变量b的值不为0.5的是(  B ) A) b=1.0/a            B) b=(float)(1/a) C) b=1/(float)a   ...

  4. C#开发微信公众号.NET平台MVC微信开发Demo解决收不到消息的问题

    不得不说现在微信非常火,微信开放平台可以自己写程序跟用户交互,节省了前台开发成本,免去用户装客户端的烦恼.于是今天兴致来潮,想做一个试试. 首先找到了开发者文档,看了看,蛮简单的.(公众号早已申请,有 ...

  5. 翻String.Format源码发现的新东西:StringBuilderCache

    起因: 记不清楚今天是为毛点想F12看String.Format的实现源码了,反正就看到了下图的鸟东西: 瞬间石化有没有,StringBuilder还能这么获取? 研究StringBuilderCac ...

  6. subclipse解决JavaHL不可用的问题

    最近在配置eclipse的svn插件,发现在部分机器上无法启用javaHL,很是奇怪,尤其是在windows环境下,网上搜索到的解决方案太复杂,居然还有说要安装slikSVN的,其实windows只需 ...

  7. RequestMapping的使用

    1.RequestMapping的作用就是 配置url 2.实现功能: 可以在不同的url访问同一个方法.

  8. IDEA学习中的参考资料

    下载安装破解:https://www.cnblogs.com/wang1024/p/7485758.html FIntelliJ-IDEA13基础教程: http://static.runoob.co ...

  9. Eclipse Infrastructure

    Everything is plug-ins running on or loaded by plug-ins loader called by a small kernal which is an ...

  10. Azure本月最新活动,速度Mark!!

    缤纷五月,翠色盈盈,风光如画,小编在这里给大家汇总了这个多彩五月最新的活动合集.我们一切都准备好了,就等你来参加了~ 首先最重磅的当然是新一届的全球微软开发者大会!   有吃有喝有 Build,5 月 ...