前言:这个问题已经被问到here ,但我特别想知道作者的具体含义。
我正在通读 Thinking in Java,第 3 版。修订版 4.0,Eckel 在第 4 章初始化和清理中展示了这个片段:
public class ArrayInit
{
public static void main(String[] args)
{
Integer[] a =
{
new Integer(1),
new Integer(2),
new Integer(3),
};
Integer[] b = new Integer[]
{
new Integer(1),
new Integer(2),
new Integer(3),
};
}
}
并声明如下:
The first form is useful at times, but it’s more limited since the size of the array is determined at compile time.
The second form provides a convenient syntax to create and call methods that can produce the same effect as C’s variable argument lists (known as “varargs” in C). These can include unknown quantities of arguments as well as unknown types.
我从来不知道这些与 Eckel 描述的不同。据我了解,它们都是静态大小的数组。我不明白第一个比第二个更“有限”。
他在说什么?
最佳答案
我认为这可能是作者所指的。
从 Java 5 开始,我们可以使用可变参数列表声明函数。
public static int newStyleSum(final int... numbers) {
int sum = 0;
for (final int number : numbers) {
sum += number;
}
return sum;
}
它们可以用于:
int s = newStyleSum(1, 2, 3, 4);
此功能只是语法糖。在内部,一个匿名数组被传递给函数。
在我们有这个语法之前,上面的例子必须写成:
public static int oldStyleSum(final int[] numbers) {
int sum = 0;
for (int i = 0; i < numbers.length; ++i) {
sum += numbers[i];
}
return sum;
}
并称为
int s = oldStyleSum(new int[]{1, 2, 3, 4}); // "second" form
但不像
int s = oldStyleSum({1, 2, 3, 4}); // "first" form (syntax error)
即使在今天,这仍然是一个语法错误。
这可能确实是他所说的。 Java 5 于 2004 年问世,所以对于 2002 年的书来说,它是有道理的。
新语法更灵活,而且——重要的是——向后兼容,所以我们仍然可以做
int s = newStyleSum(new int[]{1, 2, 3, 4});
或者,更重要的是,
int[] numbers = {1, 2, 3, 4};
int s = newStyleSum(numbers);
如果我们愿意的话。
关于java - 如果您在 Java 中显式初始化一个 Object 数组,包含 "new Object[]"与不包含它是否不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26168846/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
在我的gem中,我需要yaml并且在我的本地计算机上运行良好。但是在将我的gem推送到rubygems.org之后,当我尝试使用我的gem时,我收到一条错误消息=>"uninitializedconstantPsych::Syck(NameError)"谁能帮我解决这个问题?附言RubyVersion=>ruby1.9.2,GemVersion=>1.6.2,Bundlerversion=>1.0.15 最佳答案 经过几个小时的研究,我发现=>“YAML使用未维护的Syck库,而Psych使用现代的LibYAML”因此,为了解决
我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby数组,我们在StackOverflow上找到一
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar