爪哇 - 单个变量分配数组值的快捷方式 [英] Java - quick way of assigning array values to individual variables

查看:108
本文介绍了爪哇 - 单个变量分配数组值的快捷方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将返回两个字符串数组中,拆分的方法(STR,:,2)是precise

I have a method which will return two strings in an array, split(str, ":", 2) to be precise.

有没有在Java中更快的方式分配数组字符串变量中的两个值比

Is there a quicker way in java to assign the two values in the array to string variables than

String[] strings = str.split(":", 2);
String string1 = strings[0];
String string2 = strings[1];

例如是否有像

String<string1, string2> = str.split(":", 2);

先谢谢了。

推荐答案

没有,没有在Java中没有这样的语法。

No, there is no such syntax in Java.

然而,其他一些语言有这样的语法。例子包括Python的元组拆包,并在许多函数式编程语言模式匹配。例如,在Python中你可以写

However, some other languages have such syntax. Examples include Python's tuple unpacking, and pattern matching in many functional languages. For example, in Python you can write

 string1, string2 = text.split(':', 2)
 # Use string1 and string2

或F#你可以写

 match text.Split([| ':' |], 2) with
 | [string1, string2] -> (* Some code that uses string1 and string2 *)
 | _ -> (* Throw an exception or otherwise handle the case of text having no colon *)

这篇关于爪哇 - 单个变量分配数组值的快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆