我会将此视为序列化问题,并将其实现如下(完整且可用的Java代码):
import java.nio.ByteBuffer;
import java.util.ArrayList;
public class Serialization {
public static byte[] serialize(String[] strs) {
ArrayList byteList = new ArrayList();
for (String str: strs) {
int len = str.getBytes().length;
ByteBuffer bb = ByteBuffer.allocate(4);
bb.putInt(len);
byte[] lenArray = bb.array();
for (byte b: lenArray) {
byteList.add(b);
}
byte[] strArray = str.getBytes();
for (byte b: strArray) {
byteList.add(b);
}
}
byte[] result = new byte[byteList.size()];
for (int i=0; i
result[i] = byteList.get(i);
}
return result;
}
public static String[] unserialize(byte[] bytes) {
ArrayList strList = new ArrayList();
for (int i=0; i< bytes.length;) {
byte[] lenArray = new byte[4];
for (int j=i; j
lenArray[j-i] = bytes[j];
}
ByteBuffer wrapped = ByteBuffer.wrap(lenArray);
int len = wrapped.getInt();
byte[] strArray = new byte[len];
for (int k=i+4; k
strArray[k-i-4] = bytes[k];
}
strList.add(new String(strArray));
i += 4+len;
}
return strList.toArray(new String[strList.size()]);
}
public static void main(String[] args) {
String[] input = {"This is","a serialization problem;","string concatenation will do as well","in some cases."};
byte[] byteArray = serialize(input);
String[] output = unserialize(byteArray);
for (String str: output) {
System.out.println(str);
}
}
}我们的想法是,在生成的字节数组中,我们存储第一个字符串的长度(如果我们使用int类型,则总是4个字节),然后是第一个字符串的字节(其长度可以在后面的4个字符串中读取)字节),然后是第二个字符串的长度和第二个字符串的字节,依此类推。这样,可以从生成的字节数组中轻松恢复字符串数组,如上面的代码所示。这种序列化方法可以处理任何情况。
如果我们对输入字符串数组做出假设,代码可以更简单:
public class Concatenation {
public static byte[] concatenate(String[] strs) {
StringBuilder sb = new StringBuilder();
for (int i=0; i
sb.append(strs[i]);
if (i != strs.length-1) {
sb.append("*.*"); //concatenate by this splitter
}
}
return sb.toString().getBytes();
}
public static String[] split(byte[] bytes) {
String entire = new String(bytes);
return entire.split("\\*\\.\\*");
}
public static void main(String[] args) {
String[] input = {"This is","a serialization problem;","string concatenation will do as well","in some cases."};
byte[] byteArray = concatenate(input);
String[] output = split(byteArray);
for (String str: output) {
System.out.println(str);
}
}
}假设*.*不存在于输入数组的任何字符串中。换句话说,如果您事先知道某些特殊的符号序列将不会出现在输入数组的任何字符串中,您可以将该序列用作拆分器。