String数组初始化为构造函数的参数 [英] String array initialization as constructor parameter

查看:161
本文介绍了String数组初始化为构造函数的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,这是完全合法的按以下方式初始化字符串数组:

 的String [] S = {FOO,BAR};

然而,试图实例化一个类,需要一个String数组作为参数,下面的一段code是不允许时:

 测试T =新的测试({测试});

但是,这一次的作品:

 测试T =新的测试(新的String [] {测试});

有人能解释这是为什么?


解决方案

 的String [] S = {FOO,BAR};

这是允许在声明时仅

您不能

 的String [] S;
S = {FOO,BAR};

In Java, it is completely legal to initialize a String array in the following way:

String[] s = {"FOO", "BAR"};

However, when trying to instantiate a class that takes a String array as a parameter, the following piece of code is NOT allowed:

Test t = new Test({"test"});

But this works again:

Test t = new Test(new String[] {"test"});

Can someone explain why this is?

解决方案

String[] s = {"FOO", "BAR"};  

this is allowed at declaration time only

You can't

String[] s;
s={"FOO", "BAR"};  

这篇关于String数组初始化为构造函数的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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