CodenameOne 中是否可以保存多维数组?

我知道我只能在 CodenameOne 中使用 Storage 保存少量类,但我想知道是否可以保存多维数组。



白衣非少年
浏览 156回答 1
1回答

宝慕林4294392

没有检测到多维数组,但您可以使用手动代码保存它们,例如在您的Externalizable界面中,您可以使用诸如此类的东西来编写:&nbsp;if(myMultiArr == null) {&nbsp; &nbsp; &nbsp;out.writeInt(0);&nbsp;} else {&nbsp; &nbsp; &nbsp;out.writeInt(myMultiArr.length);&nbsp; &nbsp; &nbsp;for(int iter = 0 ; iter < myMultiArr.length ; iter++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(myMultiArr[iter] == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;out.writeInt(0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;out.writeInt(myMultiArr[iter].length);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(int i = 0 ; i < myMultiArr[iter].length ; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;out.writeInt(myMultiArr[iter][i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;}&nbsp;}然后你可以阅读它:&nbsp;myMultiArr = new int[in.readInt()][];&nbsp;for(int iter = 0 ; iter < myMultiArr.length ; iter++) {&nbsp; &nbsp; &nbsp; myMultiArr[iter] = new int[in.readInt()];&nbsp; &nbsp; &nbsp; for(int i = 0 ; i < myMultiArr[iter].length ; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myMultiArr[iter][i] = in.readInt();&nbsp; &nbsp; &nbsp; }&nbsp;}请注意,此代码会将 null 值转换为长度为 0 的数组,并假定一个多维整数数组。不过,它很容易适应任何东西。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java