0

Context: I have converted an array into a string, then into bytes so that it can be stored as a blob in a database. I then want to retrieve the array. I retrieve the blob and retrieve the string from the bytes. I then want to retrieve the array from the string.

Now I had been looking at various options to convert my string back into an array. I had looked at using string.split() with certain regex to get my array back, however, this gets a bit complicated with it being multidimensional. However is there a simple way to convert a string back to array where the string still contains its original "array syntax"?

e.g.

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

Cheers!

p.s. can an array be stored in a database (google app engine) without the need for this inefficient convoluted method?

  • I would also like to add that I understand that if this doesn't work then I can always just store each part of the array as seperate entities in the database – Robert Peach May 13 '13 at 12:23
  • Here is similar question... http://stackoverflow.com/questions/2786777/convert-string-into-two-dimensional-string-array-in-java – terafor May 13 '13 at 12:26
  • yes his alternative method was the correct answer, thank you. For those wondering I have posted it as an answer to my own question for future users. ( I had searched but had not come across that question) – Robert Peach May 13 '13 at 13:52

5 Answers5

1

You can store the List as a EmbeddedProperty.

For example, here you have the general utility class I use for those kind of needs...

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;
    }
}

... and then use it with something like:

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

... and:

turn.setPossibleQuestions(new ListTransformer<Long>().toList((List<EmbeddedEntity>)embeddedEntity.getProperty("possibleQuestions")));
Lord Khizir
  • 304
  • 1
  • 7
0

Why would you store it as blob? Convert the array to a string: "1,2,3". Then, when loading, do something like: loadedString.split(",") and iterate over the array (the result of the split() method is String[]) and add it to the Array data structure that you are using.

darijan
  • 9,725
  • 25
  • 38
  • ahh of course yes when it is in string format I can store it in the database without a blob. I just caught up using blob because I knew that I couldn't store arrays in the database as they are! – Robert Peach May 13 '13 at 13:43
0

Use the split method which parses a string accorign tot a given delimiter and returns an array out of it.

Take a look at 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
CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
  • Since the array is multidimensional then there won't be just one specific delimiter e.g. [[hello, world], [Goodbye, planet]] won't give a simple split. And therefore won't return it to the original md-array. I can obviously break the original md-array up into its rows and columns to get 1-d arrays so that the delimiter works. I just hoped there might be some code that someone had already written that easily split an md-array? – Robert Peach May 13 '13 at 13:46
0

Convert string into two dimensional string array in Java gives the answer.

In particular:

If you know how many rows and columns there will be, you can pre-allocate a String[][] and use a Scanner as follows:

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]]"
Community
  • 1
  • 1
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));
        }
    }
}
Code
  • 184
  • 1
  • 8