/**
* 2015-9-24下午4:26:42
* wangzhongyuan
*/
/**
* test 字符串与整型数组的相互转换
* -----
* 2015-9-24 下午4:26:42
* wangzhongyuan
*/
public class test
{
/**
* @param args
*/
public static void main(String[] args)
{
// int[] N = { 1, 3, 4, 5, 6 };
// String str = toString(N);
// int[] M = toIntArray(str);
// System.out.print(str);
//
// int[][] N2 = { { 1, 3, 4, 5, 6 }, { 4, 5, 1, 3, 6 }, { 3, 4, 5, 1, 6 } };
// String str2 = toString(N2);
// int[][] M2 = toIntArray2(str2);
// System.out.print(str2);
int[][][] N3 = { { { 1, 3, 4 }, { 4, 5, 1}, { 3, 4, 5 } }, { { 1, 3, 4 }, { 4, 5, 1 }, { 3, 4, 5 } }, { { 1, 3, 4 }, { 4, 5, 1 }, { 3, 4, 5 } } } ;
String str3 = toString(N3);
int[][][] M3 = toIntArray3(str3);
System.out.print(str3);
}
//---------1维整型数组 与 字符串的相互转化---------
/** 1维整型数组转化为字符串,形如:"1,3,4,5,6" */
public static String toString(int[] Value)
{
String tmp = "";
for (int v : Value)
tmp += (tmp.equals("") ? "" : ",") + v;
return tmp;
}
/** 将data转化为1维整型数组, data形如:"1,3,4,5,6" */
public static int[] toIntArray(String data)
{
String[] datas = data.split(",");
int[] tmp = new int[datas.length];
for(int i=0; i<datas.length; i++)
tmp[i] = Integer.parseInt(datas[i]);
return tmp;
}
//---------2维整型数组 与 字符串的相互转化---------
/** 2维整型数组转化为字符串,形如:"1,3,4,5,6;4,5,1,3,6;3,4,5,1,6" */
public static String toString(int[][] Value)
{
String tmp = "";
for (int[] v : Value)
tmp += (tmp.equals("") ? "" : ";") + toString(v);
return tmp;
}
/** 将data转化为2维整型数组, data形如:"1,3,4,5,6;4,5,1,3,6;3,4,5,1,6" */
public static int[][] toIntArray2(String data)
{
String[] datas = data.split(";");
int[][] tmp = new int[datas.length][];
for(int i=0; i<datas.length; i++)
tmp[i] = toIntArray(datas[i]);
return tmp;
}
//---------3维整型数组 与 字符串的相互转化---------
/** 3维整型数组转化为字符串,形如:"1,3,4;4,5,1;3,4,5#1,3,4;4,5,1;3,4,5#1,3,4;4,5,1;3,4,5" */
public static String toString(int[][][] Value)
{
String tmp = "";
for (int[][] v : Value)
tmp += (tmp.equals("") ? "" : "#") + toString(v);
return tmp;
}
/** 将data转化为3维整型数组, data形如:"1,3,4;4,5,1;3,4,5#1,3,4;4,5,1;3,4,5#1,3,4;4,5,1;3,4,5" */
public static int[][][] toIntArray3(String data)
{
String[] datas = data.split("#");
int[][][] tmp = new int[datas.length][][];
for(int i=0; i<datas.length; i++)
tmp[i] = toIntArray2(datas[i]);
return tmp;
}
}
字符串与整型数组的相互转换
最新推荐文章于 2021-09-15 20:26:51 发布