2013-05-13 37 views
0

上下文:我已经将数组转换为字符串,然后转换为字节,以便它可以作为Blob存储在数据库中。然后我想要检索数组。我检索blob并从字节中检索字符串。然后我想从字符串中检索数组。将字符串转换为多维数组

现在我一直在寻找各种选项来将我的字符串转换回数组。我曾经使用string.split()和某个正则表达式来获取我的数组,但是,这是多维的。然而,是否有一种简单的方法将字符串转换回数组,其中字符串仍然包含其原始“数组语法”?

例如

Array array = {1, 2, 3} 
String string = array.toString() 
[insert string back to array] 

干杯!

p.s.可以将数组存储在数据库(谷歌应用程序引擎)中,而不需要这种低效的复杂方法?

+0

我也想补充THA我明白,如果这不起作用,那么我总是可以将数组的每个部分作为单独的实体存储在数据库中 – 2013-05-13 12:23:10

+0

这里是类似的问题... http://stackoverflow.com/questions/2786777/convert-string -into-two-dimensional-string-array-in-java – Terafor 2013-05-13 12:26:10

+0

是的,他的替代方法是正确的答案,谢谢。对于那些想知道的我已经发布它作为对未来用户的自己问题的答案。 (我已经搜索,但没有遇到过这个问题) – 2013-05-13 13:52:04

回答

0

为什么要将它存储为blob?将数组转换为字符串:“1,2,3”。然后,在加载时,执行如下操作:loadedString.split(“,”)并遍历数组(split()方法的结果是String [])并将其添加到您正在使用的Array数据结构中。

+0

啊当然是当它是字符串格式我可以将它存储在数据库中没有blob。我刚刚使用了blob,因为我知道我不能将数组存储在数据库中,因为它们是! – 2013-05-13 13:43:23

0

使用split方法,该方法将一个字符串的accorign解析为一个给定的分隔符,并从中返回一个数组。

看看this exmaple

//split string example 

String assetClasses = "Gold:Stocks:Fixed Income:Commodity:Interest Rates"; 
String[] splits = asseltClasses.split(":"); 

System.out.println("splits.size: " + splits.length); 

for(String asset: assetClasses){ 
System.out.println(asset); 
} 

OutPut 
splits.size: 5 
Gold 
Stocks 
Fixed Income 
Commodity 
Interest Rates 
+0

由于该数组是多维的,因此不会有一个特定的分隔符,例如[[你好,世界],[再见,星球]]不会简单地分裂。因此不会将其返回到原始md数组。显然,我可以将原始md数组分割成行和列,以获得一维数组,以便分隔符起作用。我只是希望有人已经写了一些能够轻松拆分md-array的代码? – 2013-05-13 13:46:05

0

Convert string into two dimensional string array in Java给出了答案。

特别是:

如果你知道有多少行和列就会出现,你可以预先分配一个String [] [],并用扫描仪如下:

Scanner sc = new Scanner(data).useDelimiter("[,|]"); 
final int M = 3; 
final int N = 2; 
String[][] matrix = new String[M][N]; 
for (int r = 0; r < M; r++) { 
    for (int c = 0; c < N; c++) { 
     matrix[r][c] = sc.next(); 
    } 
} 
System.out.println(Arrays.deepToString(matrix)); 
// prints "[[1, apple], [2, ball], [3, cat]]" 
1

你可以将列表存储为EmbeddedProperty。

embeddedEntity.setUnindexedProperty("possibleQuestions", new ListTransformer<Long>().toEmbeddedList(turn.getPossibleQuestions())); 

例如,在这里你有我用这些类型的需求,一般的工具类...

import java.util.ArrayList; 
import java.util.List; 

import com.google.appengine.api.datastore.EmbeddedEntity; 
/** 
* Utility class for storing Lists of simple objects 
* @author toni.navarro 
* 
* 
* 
*/ 
@SuppressWarnings("unchecked") 
public class ListTransformer<T> { 
    public List<T> toList(List<EmbeddedEntity> embeddedList) {    
      List<T> list = new ArrayList<T>(); 
      if (embeddedList!=null) { 
        for (EmbeddedEntity embeddedEntity: embeddedList) { 
          list.add((T) embeddedEntity.getProperty("value")); 
        } 
      } 
      return list; 
    } 

    public List<EmbeddedEntity> toEmbeddedList(List<T> list) {    
      List<EmbeddedEntity> embeddedList = new ArrayList<EmbeddedEntity>(); 
      if (list!=null) { 
        for (T item: list) { 
          EmbeddedEntity embeddedItem = new EmbeddedEntity(); 
          embeddedItem.setUnindexedProperty("value", item); 
          embeddedList.add(embeddedItem); 
        } 
      } 
      return embeddedList; 
    } 
} 

...,然后用类似使用它。 ..和:

turn.setPossibleQuestions(new ListTransformer<Long>().toList((List<EmbeddedEntity>)embeddedEntity.getProperty("possibleQuestions"))); 
0
public static void main(String[] args) { 

    String s = "my name is abc"; 
    StringTokenizer strTokenizer = new StringTokenizer(s); 
    List<String> arrayList = new ArrayList<String>(); 
    while (strTokenizer.hasMoreTokens()) { 
     arrayList.add(strTokenizer.nextToken()); 
    } 
    List<List<String>> lst = new ArrayList<List<String>>(); 
    lst.add(arrayList); 

    for (int i = 0; i < lst.size(); i++) { 
     for (int j = 0; j < 1; j++) { 
      System.out.println("Element " + lst.get(j)); 
     } 
    } 
}