java 如何在一个方法中返回 2 个字符串:String example(String firstName, String secondName)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29914957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:07:40  来源:igfitidea点击:

How to return 2 Strings in one method: String example(String firstName, String secondName)

javastringmethodsreturn

提问by Vikjan

Public class Example1
{

    String fullname(String firstName, String secondName)
    {
        return firstName,secondName;
        //return fullname(firstName,secondName); doesn't work either :(
    }

    public static void main(String[] args) 
    {
        Example1 myname=new Example();
        System.out.println(myname.fullname("John", "Wick");
    }
}

I got an error though, when I run my program. Can you help me out please guys? thanks a lot

但是,当我运行我的程序时出现错误。你能帮我吗?多谢

回答by HJK

A method could return only one value at a time. Either concatenate the both the strings and return or you could use a class. set the details and return the instance of that class

一个方法一次只能返回一个值。连接两个字符串并返回,或者您可以使用一个类。设置详细信息并返回该类的实例

Approach 1:

方法一:

 return firstname+"|"+secondname;

If you want those separate in calling method just use return_value.split("|"). Now you get a array of strings with 2 elements, firstname and secondname

如果您希望在调用方法中将它们分开,只需使用 return_value.split("|")。现在你得到一个包含 2 个元素的字符串数组,firstname 和 secondname

Approach 2:

方法二:

Create a class like

创建一个类

 public class Person{
   private String firstName;
   private String secondName;
   public Person(String fname, String sname){
      firstname = fname; 
      secondname = sname;
    }
   // getter setters
 }

in the method do

在方法中做

 Person p = new Person(firstname, secondname);
 return p;

回答by Danyal Sandeelo

You cannot return 2 strings at time. You can use String array though

您不能一次返回 2 个字符串。你可以使用 String 数组

 public String[] fullname(String firstName, String secondName)
 {
      String[] names= new String[2];
      names[0]=firstName;
      names[1]=secondName;
      return names;
   }

Another logical way to solve this.. concatenate both of them and split them.

解决此问题的另一种合乎逻辑的方法.. 将它们连接起来并拆分它们。

You can use

您可以使用

 List<String> nameList =  new ArrayList<String>();
 nameList.add(firstName);

You can use

您可以使用

 Map<String,String> nameMap = new HashMap<String,String>();
 nameMap.put("firstName",firstName);
 nameMap.put("secondName",secondName);

There are a number of ways to use data structures.

有多种方法可以使用数据结构。

回答by Eran

A method has a single return type, so unless you package the two Strings in some class and return that class (or put the two Strings in a String array or some other container of Strings), you can only return one String.

一个方法只有一个返回类型,所以除非你将两个字符串打包在某个类中并返回那个类(或者将两个字符串放在一个字符串数组或其他字符串容器中),否则你只能返回一个字符串。

If your only requirement is to display the full name as a single String, you can simply concatenate the two Strings :

如果您的唯一要求是将全名显示为单个字符串,则可以简单地连接两个字符串:

String fullname (String firstName, String secondName) 
{
    return firstName + " " + secondName;
}

回答by Raj

Well you have three options

那么你有三个选择

  1. concatenate the strings and return as a single string. Split the string at the receiving end.

  2. Return String array

  3. Return an object . ie set the two srings to two member variables and return the object.

  1. 连接字符串并作为单个字符串返回。在接收端拆分字符串。

  2. 返回字符串数组

  3. 返回一个对象。即将两个srings设置为两个成员变量并返回对象。