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

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

问题描述

我使用的ArrayList 来存储的的toString 方法的ArrayList 数据转换成一个字符串。我的问题是,我怎么走另一条路?是否有将在字符串实例解析数据回现有方法的的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?

推荐答案

简短的回答是否。有没有简单的方法来重新导入从String对象,因为某些类型的信息在失去的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.

不过,对于特定的格式和特定​​(已知)类型的,你应该能够编写code解析手动的字符串:

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(0, 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

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

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