String[]的序列化:如何将String值转换成String[]一个?
Serialization of String[]: how to convert String value to String[] one?
这里是主要的代码
public static void main(String[] args) {
Container container= new Container();
Serializator serializator = new Serializator();
container.setvalue("1st val");
serializator.serialization(container);
}
这里是容器的代码
public class Container implements Serializable {
private static final long serialVersionUID = 1L;
/**
* Holds the elements of a container.
*/
private String[] values;
public String[] getvalue() {
return values;
}
public void setvalue(String[] values) {
this.values=values;
}
}
这是序列化程序的代码
public class Serializator {
public boolean serialization(Container container) {
boolean flag=false;
File file= new File("C:/conatiner.data");
ObjectOutputStream oos = null;
try {
FileOutputStream fos= new FileOutputStream(file);
if(fos != null) {
oos= new ObjectOutputStream(fos);
oos.writeObject(container);
flag=true;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(oos != null) {
try {
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return flag;
}
}
好吧,程序应该按以下方式工作:您创建一个包含字符串数组的容器(您可以在其中设置值),然后程序必须对其进行序列化。但问题是教程使用的是 String 值,而不是 String[] 值。我怎样才能让它理解 String[] 值并插入它?
崩溃日志如下
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method setvalue(String[]) in the type Container is not applicable for the arguments (String)
at ua.khpi.oop.taradai06.program6.main(program6.java:7)
"container.setvalue("1st val");"
首先,让我们关注一下错误:
在容器中,您承诺将在函数 setValue
中发送一个字符串数组,但您发送的是单个字符串。
您可以做两件事;
- 保持
container
代码不变并从 main 发送 array of strings
- 更改
container
代码并让 setValue
获取单个字符串值并将其 add
到 values
数组
关于序列化的主要问题请查看thatpost
如果您承诺会使用 String[] 参数,那么您应该注意这一点。
使用 setvalue 的以下版本可以完美地完成这项工作
container.setvalue(new String[] {"1st val","2nd val","3rd val"});
为了接受String
和String[]
作为Container::setvalue
方法的输入参数,应该使用varargs,然后单个字符串被接受为由一个元素组成的数组。
此外,Java getters/setters of Java Beans 的 Java 命名约定指定在 get/set 动词之后将 属性 名称大写:Getter and setter method names are composed of the word get or set, respectively, plus the property name with the first character of each word capitalized , 因此方法的名称应为 getValues/setValues
:
// Container class
public void setValues(String... values) {
this.values = values;
}
那么这个方法可以在不额外重载的情况下按如下方式调用:
container.setValues(); // empty array new String[0]
container.setValues("a string"); // new String[1]{"a string"}
container.setValues("a", "b"); // new String[2]{"a", "b"}
container.setValues(new String[]{"1", "2", "3"}); // common array
好的,我希望我帮助你查看了“setvalue”函数中的class“容器”,你给了它参数“第一个值”(它是一个字符串)但它需要一个字符串数组。所以你可以用这个替换你的代码:
主要class:
public class main {
public static void main(String[] args) {
Container container= new Container();
Serializator serializator = new Serializator();
container.setvalue("1st value", 0);
serializator.serialization(container);
}
}
容器class:
import java.io.Serializable;
public class Container implements Serializable {
private static final long serialVersionUID = 1L;
/**
* Holds the elements of a container.
*/
int AnyNumberYouWant = 100;
private String[] values = new String[AnyNumberYouWant];
public String[] getvalue() {
return values;
}
public void setvalue(String value, int index) {
this.values[index]=value;
}
}
序列化器class
import java.io.*;
public class Serializator {
public boolean serialization(Container container) {
boolean flag=false;
File file= new File("C:/Container/container.data");
ObjectOutputStream oos = null;
try {
FileOutputStream fos= new FileOutputStream(file);
if(fos != null) {
oos= new ObjectOutputStream(fos);
oos.writeObject(container);
flag=true;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(oos != null) {
try {
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return flag;
}
}
我解决了代码中的另一个错误。所以我试了一下并且工作正常。希望对你有所帮助。
这里是主要的代码
public static void main(String[] args) {
Container container= new Container();
Serializator serializator = new Serializator();
container.setvalue("1st val");
serializator.serialization(container);
}
这里是容器的代码
public class Container implements Serializable {
private static final long serialVersionUID = 1L;
/**
* Holds the elements of a container.
*/
private String[] values;
public String[] getvalue() {
return values;
}
public void setvalue(String[] values) {
this.values=values;
}
}
这是序列化程序的代码
public class Serializator {
public boolean serialization(Container container) {
boolean flag=false;
File file= new File("C:/conatiner.data");
ObjectOutputStream oos = null;
try {
FileOutputStream fos= new FileOutputStream(file);
if(fos != null) {
oos= new ObjectOutputStream(fos);
oos.writeObject(container);
flag=true;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(oos != null) {
try {
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return flag;
}
}
好吧,程序应该按以下方式工作:您创建一个包含字符串数组的容器(您可以在其中设置值),然后程序必须对其进行序列化。但问题是教程使用的是 String 值,而不是 String[] 值。我怎样才能让它理解 String[] 值并插入它? 崩溃日志如下
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method setvalue(String[]) in the type Container is not applicable for the arguments (String)
at ua.khpi.oop.taradai06.program6.main(program6.java:7)
"container.setvalue("1st val");"
首先,让我们关注一下错误:
在容器中,您承诺将在函数 setValue
中发送一个字符串数组,但您发送的是单个字符串。
您可以做两件事;
- 保持
container
代码不变并从 main 发送 - 更改
container
代码并让setValue
获取单个字符串值并将其add
到values
数组
array of strings
关于序列化的主要问题请查看thatpost
如果您承诺会使用 String[] 参数,那么您应该注意这一点。 使用 setvalue 的以下版本可以完美地完成这项工作
container.setvalue(new String[] {"1st val","2nd val","3rd val"});
为了接受String
和String[]
作为Container::setvalue
方法的输入参数,应该使用varargs,然后单个字符串被接受为由一个元素组成的数组。
此外,Java getters/setters of Java Beans 的 Java 命名约定指定在 get/set 动词之后将 属性 名称大写:Getter and setter method names are composed of the word get or set, respectively, plus the property name with the first character of each word capitalized , 因此方法的名称应为 getValues/setValues
:
// Container class
public void setValues(String... values) {
this.values = values;
}
那么这个方法可以在不额外重载的情况下按如下方式调用:
container.setValues(); // empty array new String[0]
container.setValues("a string"); // new String[1]{"a string"}
container.setValues("a", "b"); // new String[2]{"a", "b"}
container.setValues(new String[]{"1", "2", "3"}); // common array
好的,我希望我帮助你查看了“setvalue”函数中的class“容器”,你给了它参数“第一个值”(它是一个字符串)但它需要一个字符串数组。所以你可以用这个替换你的代码:
主要class:
public class main {
public static void main(String[] args) {
Container container= new Container();
Serializator serializator = new Serializator();
container.setvalue("1st value", 0);
serializator.serialization(container);
}
}
容器class:
import java.io.Serializable;
public class Container implements Serializable {
private static final long serialVersionUID = 1L;
/**
* Holds the elements of a container.
*/
int AnyNumberYouWant = 100;
private String[] values = new String[AnyNumberYouWant];
public String[] getvalue() {
return values;
}
public void setvalue(String value, int index) {
this.values[index]=value;
}
}
序列化器class
import java.io.*;
public class Serializator {
public boolean serialization(Container container) {
boolean flag=false;
File file= new File("C:/Container/container.data");
ObjectOutputStream oos = null;
try {
FileOutputStream fos= new FileOutputStream(file);
if(fos != null) {
oos= new ObjectOutputStream(fos);
oos.writeObject(container);
flag=true;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if(oos != null) {
try {
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return flag;
}
}
我解决了代码中的另一个错误。所以我试了一下并且工作正常。希望对你有所帮助。