简体   繁体   English

如何在Java / Groovy中使用getAt()函数?

[英]How to use getAt() function in Java/Groovy?

Recently I encountered the usage of the method getAt() in Java code. 最近,我在Java代码中遇到了getAt()方法的用法。 It is used to get the data from a URL (which is sent via GET method by form submit). 它用于从URL获取数据(通过表单提交通过GET方法发送)。 The URL will be like: URL将如下:

http://192.168.27.55/flight/search?n=airchina

The method was used like name=params.getAt("n") . 使用该方法,如name=params.getAt("n") Then the data was passed to another function by search("n",name) . 然后,将数据通过search("n",name)传递给另一个函数。 Can any one please brief me how it works? 谁能告诉我它是如何工作的?

getAt() in Groovy has special meaning for collections. Groovy中的getAt()对集合有特殊意义。 It allows one to access elements of the collection using the subscript operator . 它允许用户使用下标运算符访问集合的元素。

Here's the documentation for Map and List : Map#getAt(key) List#getAt(index) 这是MapList的文档: Map#getAt(key) List#getAt(index)

Since it's defined to support some syntactic sugar, you don't really see it ever called directly, since it enables you to instead do something like: 由于它被定义为支持一些语法糖,你实际上并没有看到它直接调用,因为它使你能够做以下事情:

Map foo = [bar: 'baz']
assert foo['bar'] == 'baz'

In your particular case with params , you'd simply use: 在你的特殊情况下使用params ,你只需使用:

params['n']

...to take advantage of getAt() . ...利用getAt() Alternatively, you could use: 或者,您可以使用:

params.n
// or
params.get('n')
params.n

params的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM