怎么给一个参数为数组的函数传递参数
本人刚学JAVA,写了个练习的小程序如下:
package java1.lesson1.dest;
import java.util.*;
public class HelloWorld{
static Date p=new Date();
public static void main(String[] args)
{
System.out.println(p);
System.out.println( "Hello World! ");
PrintAgument test = new PrintAgument(20);
}
}
package java1.lesson1.dest;
public class PrintAgument {
PrintAgument(String numArg[]){
int len = numArg.length();
String s;
s= " ";
if (len!=0){
for (int i=0;i <len ;i++ ){
s=s+numArg[i];
}
}
System.out.println( "你的参数为: "+s);
}
}
但是遇到个问题
E:\train> javac -d e:\train HelloWorld.java
HelloWorld.java:13: cannot resolve symbol
symbol : constructor PrintAgument (int)
location: class java1.lesson1.dest.PrintAgument
PrintAgument test = new PrintAgument(20);
怎么给一个参数为数组的函数传递参数??请教高手
------解决方案--------------------既然需要一个数组,那就造一个传进去呀,你传个20算什么呀?
------解决方案--------------------PrintAgument test = new PrintAgument({ "a ", "b ", "c ", "hello world "});
------解决方案--------------------改了一下你的程序:
import java.util.*;
public class testLongValue{
public static void main(String[] args)
{
System.out.println(new Date());
System.out.println( "Hello World! ");
String[] parameter = new String[20];
PrintAgument test = new PrintAgument(parameter);
}
}
class PrintAgument {
PrintAgument(String numArg[]){
int len = numArg.length;
System.out.println( "你的长度为: " + len);
}
}
数组长度是属性,不是方法
------解决方案--------------------建议你这么写.
public class PrintAgument {
public PrintAgument(String...numArg){
int[] len = numArg;
String s;
if (len.leng> 0){
for (int i=0;i <len.length ;i++ ){
s=s+len[i];
}
}
System.out.println( "你的参数为: "+s);
}
}
------解决方案--------------------顶,变长参数public PrintAgument(String ...numArg)
------解决方案--------------------传数组的地址。