ArraySerializer

package subArrays;

import java.util.Arrays;

public class ArraySerializer {
    
    public static int[] deserialize(String data) {
        String[] nums = data.substring(1,data.length() - 1).split(",");
        int[] list = new int[nums.length];
        for (int i = 0; i < nums.length; i++) {
            list[i] = Integer.valueOf(nums[i].trim());
        }
        
        return list;
    }
    
    public static String serialize(int[] data) {
        StringBuilder sb = new StringBuilder();
        sb.append('[');
        for (int i = 0; i < data.length; i++) {
            if (i != data.length - 1) {
                sb.append(String.valueOf(data[i])+", ");
            } else {
                sb.append(String.valueOf(data[i]));
            }
        }
        sb.append(']');
        
        return sb.toString();
    }
    
    public static void main(String[] args) {
        String str = "[1, 2, 3, 4, -5]";
        int[] deserialize = deserialize(str);
        String serialize = serialize(deserialize);
        
        System.out.println(Arrays.toString(deserialize));
        System.out.println(serialize);
        
    }
}

你可能感兴趣的:(ArraySerializer)