【问题标题】:How would I return this "clubName" array to ensure the "choice -1" is used我将如何返回这个“clubName”数组以确保使用“choice -1”
【发布时间】: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


【解决方案1】:

在这里你声明返回一个String[]的数组:

public String[] printGreeting(int choice, String[] clubName) {
    // ↑ here you say this method MUST return an array if Strings

你需要的是

  • 将用户的选择分配给返回的变量
  • 只返回一个String

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) {
        // assign choice to result
        result = clubName[choice - 1]; 
        // print choice
        System.out.println("You chose: " + result); // return the clubName -1
    } 

    // return the chosen club name
    return result; 
}

其实我不知道为什么result是一个类属性(但是我看不到声明),如果你想返回它没有多大意义,我将方法编码为:

public String printGreeting(int choice, String[] clubName) {
    System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); // ??

    if (choice >= 1 && choice <= 20) {
        choice --; // if choice is valid, get the array position.
        // print choice
        System.out.println("You chose: " + clubName[choice]); 
        return clubName[choice]; 
    } 

    // if the choice is not correct, return null or "" as you want
    return null;
}

更新

谁能告诉我如何将返回的String 传递给我的serialize 方法,我想我知道如何serialize 它,但不能100% 确定参数传递。

  1. 我不完全了解您想要达到的目标,也许最好用明确的目标和尝试重新表述问题。

  2. 序列化(很快),在Java 中使对象的属性可转换为Strings,然后,StringString[] 数组不需要序列化强>.

  3. 只要Display方法不是static,你必须创建一个Display的实例来执行如下:

    public class Main {
        public static void main(String [] args) {
    
            // create an instance of Display class
            Display d = new Display();
    
            // get the needed values to pass to printGreeting method:
            String[] clubs = {"club one", "club two" // put 20 clubs
    
            // get the index from the user
            Scanner sc = new Scanner(System.in);
            System.out.print("Enter number 1: ");
            while (!sc.hasNextInt()) sc.next();
            int choice = sc.nextInt();
    
            // call the method and get the return:
            String result = d.printGreeting(choice, clubs)
    
            // then get a serializer and execute method:
            Serialize s = new Serialize();
            s.serialize(result);
        }  
    }
    
  4. 将方法Serialize.serialize()改为Serialize.serialize(String)如下:

    public class Serialize
    {
        public void serialize(String club) 
        //                    ↑ receive return type from printGreeting();
        {
             // your serialize code
        }
    }
    

【讨论】:

  • 非常感谢,除了它有效之外,它将提高这个新手的理解。
  • 谁能告诉我如何将返回的字符串传递给我的序列化方法,我想我知道如何序列化它,但不能 100% 确定参数传递。
  • @DarrenEstcourt ,请查看我的更新,另外,请回答更新的第 1 点,也许你更容易解释你想要做什么...... ;)
  • 我想序列化已经选择的团队,这样当我的 De-serialize 方法被调用时,我可以加载选择的团队名称。我已经知道如何序列化一个对象,但是我不想在我的 Serialized 方法中声明一个对象,我想将它作为参数传递。目前,我通过实现 Serializable 的类在我的 Serialized 方法中创建了一个新对象。
  • 你知道如何声明静态方法以及如何使用它们吗?如果您不想实例化类,那就必须这样做
【解决方案2】:

你想返回什么?俱乐部名称(字符串)还是整个数组?

您的代码中不清楚结果是数组还是字符串,您只需说result = clubName。如果是数组应该是String[] result = clubName;,如果你想返回一个String应该是String result = clubName[choice -1];,这种情况下你必须把方法改成public String printGreeting(int choice, String[] clubName),你可以return result;

【讨论】:

    猜你喜欢
    • 2020-12-23
    • 2018-07-17
    • 1970-01-01
    • 2013-05-20
    • 2017-09-06
    • 1970-01-01
    • 2020-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多