我将如何返回此“clubName”数组以确保使用“choice -1”

时间:2015-10-14 15:17:44

标签: java arrays serialization

我想将“You Chose:abc”的“结果”传递给返回类型,因此我可以将其传递给我的序列化方法,以便我可以序列化所选择的团队。我知道如何返回数组,但是如何返回数组-1?

代码段如下:

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();
      }

   }
}

对此的任何帮助将不胜感激:)

2 个答案:

答案 0 :(得分:0)

在此声明返回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; 
}

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

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中将对象的属性转换为String s,然后String String[]数组不需要序列化

  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
        }
    }
    

答案 1 :(得分:0)

你想要什么回报?俱乐部名称(String)或整个数组?

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