serialize a list of string and deserialize it
来源:互联网 发布:淘宝金丝绒太极服 编辑:程序博客网 时间:2024/06/06 08:34
import java.util.*;public class combineStrings{ public static void main(String[] args) { String[] arr = {"abc%cde", "a#aa", "haha"}; for(String s : arr) { System.out.println(s); } String result = serialize(arr); System.out.println(result); String[] newArr = deserialize(result); for(String s : newArr) { System.out.println(s); } } public static String serialize(String[] arr) { StringBuilder sb = new StringBuilder(); sb.append(arr.length + "#"); for (String s : arr) { sb.append(s.length() + "%"); } for (String s : arr) { sb.append(s); } return sb.toString(); } public static String[] deserialize(String s) { String[] sizeAndContent = s.split("#"); //could be splitting to 2 int len = Integer.parseInt(sizeAndContent[0]); s = s.substring(sizeAndContent[0].length()+1); String[] eachSize = s.split("%"); int[] size = new int[len]; int total = 0; for (int i = 0; i < len; i++) { size[i] = Integer.parseInt(eachSize[i]); total += size[i]; } String content = s.substring(s.length() - total, s.length()); String[] result = new String[len]; for (int i = 0; i < len; i++) { result[i] = content.substring(0, size[i]); content = content.substring(size[i]); } return result; }}
0 0
- serialize a list of string and deserialize it
- C# Serialize and Deserialize XML string
- Serialize and deserialize a binary tree
- Serialize and Deserialize a binary tree
- Serialize and Deserialize
- Serialize and Deserialize tree
- Serialize and DeSerialize
- Serialize and Deserialize BST
- Java-Serialize and Deserialize
- [LinkedIn]Serialize (encode) and deserialize(decode) a binary tree
- Serialize and Deserialize Binary Tree & Serialize and Deserialize BST
- [LeetCode]Serialize and Deserialize Tree
- Serialize and Deserialize Binary Tree
- Serialize and Deserialize Binary Tree
- Serialize and Deserialize Binary Tree
- Serialize and Deserialize Binary Tree
- Serialize and Deserialize Binary Tree
- Serialize and Deserialize Binary Tree
- JQuery ajax请求一直返回Error(parsererror)
- 安卓开发——JNI——回调java中的方法
- 抽象类和接口的区别
- C/C++语言内存对齐
- 在做自动化测试之前你需要知道的
- serialize a list of string and deserialize it
- leetcode #14 LongestCommenPrefix
- groovy 把json串转化成对象
- find maximized count of 0 on left and 1 on right in binary array
- Java线程中的ThreadPoolExecutor
- 利用jdk.tar.gz部署在CentOS/RedHat Enterprise Linux上
- Android屏幕关闭时开机CPU运行(以传感器为例)
- leetcode #1 TwoSum
- 冲动是魔鬼,冷静也是魔鬼(草稿)