Java ArrayList的(ArrayList).toString的相反方向是什么? [英] What is the reverse of (ArrayList).toString for a Java ArrayList?

查看:187
本文介绍了Java ArrayList的(ArrayList).toString的相反方向是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ArrayList toString 方法来存储 ArrayList 数据转换成String。我的问题是,我该怎么走?是否有一种现有的方法可以将 String 实例中的数据解析回 ArrayList

I am using the toString method of ArrayList to store ArrayList data into a String. My question is, how do I go the other way? Is there an existing method that will parse the data in the String instance back into an ArrayList?

推荐答案

简短的答案是否。由于某些类型信息在 toString()序列化中丢失,所以没有简单的方法从String重新导入Object。

The short answer is "No". There is no simple way to re-import an Object from a String, since certain type information is lost in the toString() serialization.

但是,对于特定格式和特定​​(已知)类型,您应该可以编写代码来手动解析字符串:

However, for specific formats, and specific (known) types, you should be able to write code to parse a String manually:

// Takes Strings like "[a, b, c]"
public List parse(String s) {
  List output = new ArrayList();
  String listString = s.substring(1, s.length() - 1); // chop off brackets
  for (String token : new StringTokenizer(listString, ",")) {
    output.add(token.trim());
  }
  return output;
}

从他们的序列化的表单重建对象通常被称为反序列化

Reconstituting objects from their serialized form is generally called deserialization

这篇关于Java ArrayList的(ArrayList).toString的相反方向是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆