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

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

问题描述

我正在使用 ArrayListtoString 方法将 ArrayList 数据存储到一个字符串中.我的问题是,我如何走另一条路?是否存在将 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() 序列化中丢失了某些类型信息.

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天全站免登陆