import java.util.*; public class TTest { public void test(String[] strarr) { for(int i = 0; i < strarr.length; i++) { strarr[i] = "HelloWorld" + i; } } public void test1(String[] strarr) { String[] strarrTemp = new String[3]; Arrays.fill(strarrTemp, "Helloworld"); strarr = strarrTemp; } public static void main(String[] args) { TTest tt = new TTest(); String[] strarr = new String[3]; tt.test(strarr);
int i; for(i = 0; i < strarr.length; i ++) { System.out.println(strarr[i]); }
String[] strarr1 = null; tt.test1(strarr1);
for(i = 0; i < strarr1.length; i ++) { System.out.println(strarr1[i]); } } }输出: HelloWorld0 HelloWorld1 HelloWorld2 Exception in thread "main" java.lang. at TTest.main(TTest.java:32)除了上个回复的情况外,形式参数确实不能载值,其实String也是被看作基本数据类型后才有不能改变值得结果
// 1) New a String array // 2) asign value to the array // 3) return the handle then // 4) It's OK now!public String[] getStringArray(){ String[] strings = new String[10]; for( int i=0;i<strings.length;i++){ strings[i]= i + " is in String Array"; } return strings;}
不过,我想你是受了其它语言的影响,只要是对象,传的都是指针
private int getRrkTtlInf(String[] strRefCnd, Object[][] oRrkTtl)我想用oRrkTtl 返回对象数组
怎么办?急。
利用参数中的句柄(也就是楼上兄台所说指针)可以改变对象的值,包括数组
例外的情况是String,应该用StringBuffer来改变字符串的值
public class TTest
{
public void test(String[] strarr)
{
for(int i = 0; i < strarr.length; i++)
{
strarr[i] = "HelloWorld" + i;
}
}
public void test1(String[] strarr)
{
String[] strarrTemp = new String[3];
Arrays.fill(strarrTemp, "Helloworld");
strarr = strarrTemp;
}
public static void main(String[] args)
{
TTest tt = new TTest();
String[] strarr = new String[3];
tt.test(strarr);
int i;
for(i = 0; i < strarr.length; i ++)
{
System.out.println(strarr[i]);
}
String[] strarr1 = null;
tt.test1(strarr1);
for(i = 0; i < strarr1.length; i ++)
{
System.out.println(strarr1[i]);
}
}
}输出:
HelloWorld0
HelloWorld1
HelloWorld2
Exception in thread "main" java.lang.
at TTest.main(TTest.java:32)除了上个回复的情况外,形式参数确实不能载值,其实String也是被看作基本数据类型后才有不能改变值得结果
// 2) asign value to the array
// 3) return the handle then
// 4) It's OK now!public String[] getStringArray(){
String[] strings = new String[10];
for( int i=0;i<strings.length;i++){
strings[i]= i + " is in String Array";
} return strings;}