首页 \ 问答 \ Java中的通用函数(Generic functions in Java)

Java中的通用函数(Generic functions in Java)

我对Java中的一些通用语法不是很熟悉。
我遇到了这样的代码:

public static<T> T foo(T... a)

有人能以简洁的方式解释它的含义吗?
这是否意味着foo()需要一个T类型的数组并返回类型T?
为什么不是下面的语法?

public static T foo(T[] a)

我看了一下Oracle文档,但他们的例子似乎更容易理解: Oracle Generics


I am not very familiar with some of the generic syntax in Java.
I came across some code like this:

public static<T> T foo(T... a)

Can somebody explain what it means in a succinct way?
Does it mean foo() takes in an array of type T and returns type T?
Why isn't the syntax like this below?

public static T foo(T[] a)

I had a look at the Oracle docs but the example they have seems much easier to understand: Oracle Generics


原文:https://stackoverflow.com/questions/30700539
更新时间:2023-08-02 15:08

最满意答案

所以这就是stackoverflow错误,我没有在第一次得到它,但在从DFS列表中删除一些参数后,我遇到了stackoverflower错误。


So that was stackoverflow error, i didn't get it at the first time, but after removing some arguments from DFS list, i encountered stackoverflowerror.

相关问答

更多
  • 你有困难的部分! 想想从甲板底部发牌。 一旦它们组合在一个堆栈中,只需将整个堆栈移动到您需要底部磁盘的位置。 然后再次移动整个堆栈减去底部元素到底部磁盘需要的位置。 等等。也许有一种更聪明的方式,但这肯定有效。 (您也可以通过一次打开两个底部进行拆散,与堆叠方式相反。这可能会更有效率。) 这是一个带有简单文本图形的C版本。 注意我使用了一种稍微不同的方法来构建单个堆栈。 你的总动作效率更高一些。 我们用正面交换正面标记的磁盘: #include // Three pegs with v ...
  • 假设您使用长度为N的列表调用permutatePersons ,则应用以下递归: T(N) = T(N-1) + O(N^2) 这是因为在每个递归步骤中,您使用长度为N-1的列表调用函数(其中N为当前长度),并且您还要计算总复杂度O(N ^ 2)(外部循环O(N) - 正好遍历列表和内部循环遍历O(N)-O(1)中的每个元素和总N元素的散列映射,因此嵌套循环总体为O(N ^ 2))。 你可以很容易地看到: T(N) = T(N-1) + O(N^2) = T(N-2) + O(N^2) + O((N-1) ...
  • 它是O(n) 。 谁告诉过你,否则是错误的。 It is O(n). Whoever told you otherwise is mistaken.
  • 所以这就是stackoverflow错误,我没有在第一次得到它,但在从DFS列表中删除一些参数后,我遇到了stackoverflower错误。 So that was stackoverflow error, i didn't get it at the first time, but after removing some arguments from DFS list, i encountered stackoverflowerror.
  • 您可以应用以下几项改进: 1- cin对于大输入不是那么快的scanf :因为您的输入文件很大,所以最好使用scanf来读取数据。 2-通过值将大数据传递给函数不是一个好主意 :您的代码中有两个巨大的图形,您可以按值将它们传递给函数。 每次制作数据副本时都需要花费很多时间。 3-不需要使用iterator来遍历vector :因为你正在使用vector并且你可以通过[]运算符随机访问它,所以不需要使用iterator来访问数据。 4-你的DFS效率不高 :这是最重要的一个。 每次程序进入while的开头并检 ...
  • if selectRandomNode(nodes); 使用替换(同一个节点可以被选中两次),然后没有定义大o,因为你有一个可能无限循环(你可以一次又一次地选择同一个节点)。 如果它在没有替换的情况下工作,那么它是O(n^2) (在最坏的情况下,每个节点可以连接到每个其他节点)。 选择无需替换的注意事项: 考虑给你一个大小为n的数组的情况,比如A和一个空数组, B 。 A中的所有元素都是唯一的。 任务是用B随机选择的n元素填充B 期望B应该存在至少k独特元素。 可以证明,具有多于k唯一项的概率随着n增加而增 ...
  • 我通常使用队列做这种事情: // Make sure rootDir exists first... var files = new List(); var dirs = new Queue(); dirs.Enqueue(rootDir); while(dirs.Count > 0 ) { var dir = dirs.Dequeue(); foreach( var fileName in Directory.GetFiles(dir) ) { fi ...
  • 如何将递归调用更改为: else { int mid = (stop-start)/2; int x = search(data, start, mid, target); if (x == -1) return search(data, mid+1, stop, target); else return x; } How about changing the recursive call to: else { int mid = ...
  • 你提到的递归公式是: T(n)= T(n-1)+ O(n) 它意味着: T(n)= kn + k(n-1)+ k(n-2)+ .. + k,其等于k * n *(n + 1)/ 2。 因此,算法的复杂性为O(n 2 )。 The formula for the recursion mentioned by you is: T(n) = T(n-1) + O(n) It implies that: T(n) = kn + k(n-1) + k(n-2) + .. + k, which is equal to ...
  • 由于eddi的答案在您的情况下是最好的,更通用的解决方案是矢量化代码(意味着:一次操作所有行): counter <- rep(0, nrow(dataset)) for(j in 1:ncol(dataset)) { counter <- counter + !is.na(dataset[[j]]) } dataset$no_of_1s <- counter 一个注意事项:在您的代码中: dataset[i,ncol(dataset)+1]<-counter 您为每一行创建新列(导致每个 ...

相关文章

更多

最新问答

更多
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • linux的常用命令干什么用的
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • Java中的不可变类(Immutable class in Java)
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • 绑定属性设置器未被调用(Bound Property Setter not getting Called)
  • linux ubuntu14.04版没有那个文件或目录
  • 如何使用JSF EL表达式在param中迭代变量(How to iterate over variable in param using JSF EL expression)
  • 是否有可能在WPF中的一个单独的进程中隔离一些控件?(Is it possible to isolate some controls in a separate process in WPF?)
  • 使用Python 2.7的MSI安装的默认安装目录是什么?(What is the default installation directory with an MSI install of Python 2.7?)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • ckeditor config.protectedSource不适用于editor.insertHtml上的html元素属性(ckeditor config.protectedSource dont work for html element attributes on editor.insertHtml)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 将CouchDB与AJAX一起使用是否安全?(Is it safe to use CouchDB with AJAX?)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • 无法在新线程中从FREContext调用getActivity()?(Can't call getActivity() from FREContext in a new thread?)
  • 在Alpine上升级到postgres96(/ usr / bin / pg_dump:没有这样的文件或目录)(Upgrade to postgres96 on Alpine (/usr/bin/pg_dump: No such file or directory))
  • 如何按部门显示报告(How to display a report by Department wise)
  • Facebook墙贴在需要访问令牌密钥后无法正常工作(Facebook wall post not working after access token key required)
  • Javascript - 如何在不擦除输入的情况下更改标签的innerText(Javascript - how to change innerText of label while not wiping out the input)
  • WooCommerce / WordPress - 不显示具有特定标题的产品(WooCommerce/WordPress - Products with specific titles are not displayed)