gpt4 book ai didi

java - 将 ArrayList 序列化为 String 并将 String 反序列化为 ArrayList 约束是数组列表包含字符集中的任何字符

转载 作者:行者123 更新时间:2023-12-02 11:38:28 25 4
gpt4 key购买 nike

我在下面的代码中进行序列化,并更正序列化和反序列化的代码,如何做到这一点?我们不需要任何分割定界符,因为列表可能包含任何字符,因此不可能进行字符串分割

 public class ArratListSerializaationTest {

public static void main(String[] args) {
List<String> dataList = new ArrayList<>();
dataList.add("A");
dataList.add(",");
dataList.add("B");
String datStr = serialize(dataList);
List<String> receivedList = deserialize(datStr);
}

public static String serialize(List<String> dataList){

StringBuilder dataBuilder = new StringBuilder("[");
dataList.forEach((data) -> {
dataBuilder.append(data);
});
return dataBuilder.append("]").toString();
}

public static List<String> deserialize(String dataStr){

ArrayList<String> dataList = new ArrayList<>();
//TODO
return dataList;
}
}

最佳答案

最后我给出了这样的解决方案,这样我的问题就解决了

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class SerializeDeSerializeTest {

public static void main(String[] args) throws Exception{
List<String> dataList = new ArrayList<>();
dataList.add("A");
dataList.add(",");
dataList.add("B");
System.out.println("Before Serialization\n");
print(dataList);
String datStr = serialize(dataList);
List<String> receivedList = deserialize(datStr);
System.out.println("\nAfter DeSerialization\n");
print(receivedList);
}
public static void print(List<String> dataList){

dataList.forEach((data) -> {
System.out.println(data);
});
}
public static String serialize(List<String> dataList) {

StringBuilder xmlBuilder = new StringBuilder("<root>");
dataList.forEach((data) -> {
xmlBuilder.append("<data>").append(data).append("</data>");
});
return xmlBuilder.append("</root>").toString();
}

public static List<String> deserialize(String xml) throws SAXException, ParserConfigurationException, IOException, XPathExpressionException {

final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));
final XPathExpression xPathExpression = XPathFactory.newInstance().newXPath().compile("//data/text()");
final NodeList nodeList = (NodeList) xPathExpression.evaluate(document, XPathConstants.NODESET);
final List<String> dataList = new ArrayList<>();
for (int i = 0; i < nodeList.getLength(); ++i) {
dataList.add(nodeList.item(i).getNodeValue());
}
return dataList;
}
}

关于java - 将 ArrayList 序列化为 String 并将 String 反序列化为 ArrayList 约束是数组列表包含字符集中的任何字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48747603/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com