java 二维数组和字符串相互转化?
需求:将二维数组序列化存储
思路:由于Array类无法序列化,现相处曲线救国的思路,先将二维数组转化为字符串,什么形式不重要,String序列化保存
需要的时候String序列化提取,再由字符串转化为二维数组
这段代码我自己写了一部分,发现运行出错,忘朋友们帮忙,只要能可互相转化即可。
还有如果去知道里面复制粘贴就算了,我已经在网上查了,不找到可以用的案例,转换回来格式就变了。
package test;
public class ArrayToStringTest {
public static void main(String[] args) {
int[][] array = new int[4][4];
int count = 0;
ArrayToStringTest test = new ArrayToStringTest();
// 赋值一个int[][]
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
array[i][j] = count;
++count;
}
}
// 获取row,col
int row = test.getRow(array);
int col = test.getCol(array);
// 转为String
String str = test.convertToString(array, row, col);
System.out.println(str); // 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
//转为int[][]
int[][] arrayConvert = new int[row][col];
arrayConvert = test.convertToArray(str, row, col);
}
public int getRow(int[][] array) {
int row = 0;
if (array != null) {
row = array.length; // 行
}
return row;
}
public int getCol(int[][] array) {
int col = 0;
if (array != null) {
col = array[0].length; // 列
}
return col;
}
public String convertToString(int[][] array, int row, int col) {
String str = "";
String tempStr = null;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
tempStr = String.valueOf(array[i][j]);
str = str + tempStr + ",";
}
}
return str;
}
public int[][] convertToArray(String str, int row, int col){
int[][] arrayConvert = new int[row][col];
int count = 0;
String[] strArray = str.split(",");
for(int i = 0 ; i < row ; i ++){
for(int j = 0 ; j < col ; j ++){
arrayConvert[i][j] = Integer.parseInt(strArray[count]);
++ count ;
}
}
return arrayConvert;
}
}
public class ArrayToStringTest {
public static void main(String[] args) {
int[][] array = new int[4][4];
int count = 0;
ArrayToStringTest test = new ArrayToStringTest();
// 赋值一个int[][]
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
array[i][j] = count;
++count;
}
}
// 获取row,col
int row = test.getRow(array);
int col = test.getCol(array);
// 转为String
String str = test.convertToString(array, row, col);
System.out.println(str); // 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
//转为int[][]
int[][] arrayConvert = new int[row][col];
arrayConvert = test.convertToArray(str, row, col);
}
public int getRow(int[][] array) {
int row = 0;
if (array != null) {
row = array.length; // 行
}
return row;
}
public int getCol(int[][] array) {
int col = 0;
if (array != null) {
col = array[0].length; // 列
}
return col;
}
public String convertToString(int[][] array, int row, int col) {
String str = "";
String tempStr = null;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
tempStr = String.valueOf(array[i][j]);
str = str + tempStr + ",";
}
}
return str;
}
public int[][] convertToArray(String str, int row, int col){
int[][] arrayConvert = new int[row][col];
int count = 0;
String[] strArray = str.split(",");
for(int i = 0 ; i < row ; i ++){
for(int j = 0 ; j < col ; j ++){
arrayConvert[i][j] = Integer.parseInt(strArray[count]);
++ count ;
}
}
return arrayConvert;
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答 2015-05-29
学了集合,感觉数组就可以淘汰了。~~~
你的目的是将2维数组序列化,是吧。
好了,经过研究,2维数组是可以序列化的。
import java.io.Serializable;
public class Demo implements Serializable{
private static final long serialVersionUID = 1L;
private String name;
private int number;
public Demo(){
}
public Demo(String name,int number){
this.name= name;
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class MyMain {
public static void main(String args[]){
Demo[][] demos = {
{new Demo("张三",1),new Demo("李四",2)},
{new Demo("张五",3),new Demo("孙6",4)}
};
//Demo d = new Demo("张三",1);
//Array a = new Array
ObjectOutputStream os;
try {
os = new ObjectOutputStream(new FileOutputStream("abc.txt"));
os.writeObject(demos);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//上面是保存
//下面是读取
try {
ObjectInputStream input = new ObjectInputStream(new FileInputStream("abc.txt"));
Demo[][] demos1 = (Demo[][]) input.readObject();
System.out.println(demos1);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("over");
}
}追问
你的目的是将2维数组序列化,是吧。
好了,经过研究,2维数组是可以序列化的。
import java.io.Serializable;
public class Demo implements Serializable{
private static final long serialVersionUID = 1L;
private String name;
private int number;
public Demo(){
}
public Demo(String name,int number){
this.name= name;
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class MyMain {
public static void main(String args[]){
Demo[][] demos = {
{new Demo("张三",1),new Demo("李四",2)},
{new Demo("张五",3),new Demo("孙6",4)}
};
//Demo d = new Demo("张三",1);
//Array a = new Array
ObjectOutputStream os;
try {
os = new ObjectOutputStream(new FileOutputStream("abc.txt"));
os.writeObject(demos);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//上面是保存
//下面是读取
try {
ObjectInputStream input = new ObjectInputStream(new FileInputStream("abc.txt"));
Demo[][] demos1 = (Demo[][]) input.readObject();
System.out.println(demos1);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("over");
}
}追问
恩 谢谢 很有意思 学习了 再次表示十分感谢
追答采纳呀
追问恩 两个都很好,采纳最能满足我的那个了,你这个也很有启发性,正在学习你这个,抱歉没采纳你的
追答没事没事。