【发布时间】:2015-10-14 15:17:44
【问题描述】:
我想将“You Chose : abc”的“结果”传递给一个返回类型,然后我可以将它传递给我的序列化方法,这样我就可以序列化那个选择的团队。我知道如何返回一个数组,但是我将如何返回一个数组 -1 呢?
代码sn-ps如下:
public class Display{
public String[] printGreeting(int choice, String[] clubName) {
result = clubName;
System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
if (choice >= 1 && choice <= 20) {
System.out.println("You chose: " + clubName[choice - 1]); // return the clubName -1
}
return result; // how to declare return statement ?
}
}
这是我的序列化代码,不知道如何通过别名或使用对象传递数组?
public class Serialize
{
public void Serialize() // receive return type from printGreeting();
{
// how to put object info into files, rather than declare here ?
try
{
FileOutputStream fileOut = new FileOutputStream("/home/cg/root/club.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(club);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in C:/tmp/club.ser");
}catch(IOException i)
{
i.printStackTrace();
}
}
}
对此的任何帮助将不胜感激:)
【问题讨论】:
-
您可以将数组传递给 Serialize 构造函数,然后在函数内引用 this.club。
-
至于返回array-1,我不确定你的意思。
-
@JackRyan 数组位置从 0 开始,但人类将第一个位置称为 1...这就是 OP 的意思
标签: java arrays serialization