class Person { char[] name; char[] course; public Person(char[] names,char[] courses) { name = new char[names.length]; course = new char[courses.length]; System.arraycopy(names,0,name,0,name.length); System.arraycopy(courses,0,course,0,course.length); } void who() { System.out.println("My name is " + new String(name)); System.out.println("My course is " + new String(course)); }
} 你的输入是数组地址,另外arraycopy方法参数有误
System.arraycopy(names,0,name,0,names.length); System.arraycopy(courses,0,course,0,courses.length); } void who() { System.out.println("My name is " + new String(name)); System.out.println("My course is " + new String(course)); } 1。source 应该是 names和sources. 2。object会显示地址,应该做一对象显示其值。new string(..)
to: gaoxt1983(刑天战士) void who() { System.out.println("name's hashCode =" +name.hashCode()); System.out.println("name's address = " +name); System.out.println("My name is " + new String(name)); System.out.println("My course is " + new String(course)); }输出: name's hashCode =19175605 name's address = [C@12498b5 My name is ee My course is ee name's hashCode =27634497 name's address = [C@1a5ab41 My name is ee My course is ee name's hashCode =26099296 name's address = [C@18e3e60 My name is ee My course is ee
一楼和二楼都不对,我试过了, 我将楼主的代码改一下,并经过编译,运行,达到了楼主的所要求的结果!代码如下: class Person { char[] name; char[] course; public Person(char[] names,char[] courses) { name = new char[names.length]; course = new char[courses.length]; name=names; course=courses; //楼主,不好意思,我将你的字符数组复制语句屏闭了 // 既然是等长的数组为什么不可以直接赋值呢? /*System.arraycopy(name,0,name,0,name.length); System.arraycopy(course,0,course,0,course.length);*/ } void who() { System.out.println("My name is " + new String(name)); System.out.println("My course is " + new String(course)); //之所以要转化成一个String,原因就是字符数组不能和String相加,同时它也 //是不能直接输出的哦 }} public class ObjectArray { public static void main(String[] args) { Person[] persons = new Person[3]; char[] str1 = {'e','e'}; char[] str11 = {'e','e'}; char[] str2 = {'e','e'}; char[] str22 = {'e','e'}; char[] str3 = {'e','e'}; char[] str33 = {'e','e'}; persons[0] = new Person(str1,str11); persons[1] = new Person(str2,str22); persons[2] = new Person(str3,str33); for(int i = 0;i<3;++i) { persons[i].who(); } }}楼主,去试试吧, 祝你成功
{
char[] name;
char[] course; public Person(char[] names,char[] courses)
{
name = new char[names.length];
course = new char[courses.length]; System.arraycopy(names,0,name,0,name.length);
System.arraycopy(courses,0,course,0,course.length);
}
void who()
{
System.out.println("My name is " + new String(name));
System.out.println("My course is " + new String(course));
}
}
你的输入是数组地址,另外arraycopy方法参数有误
System.arraycopy(courses,0,course,0,courses.length);
}
void who()
{
System.out.println("My name is " + new String(name));
System.out.println("My course is " + new String(course));
}
1。source 应该是 names和sources.
2。object会显示地址,应该做一对象显示其值。new string(..)
对楼上的补充,数组本身也是一个Oject
void who()
{
System.out.println("name's hashCode =" +name.hashCode());
System.out.println("name's address = " +name);
System.out.println("My name is " + new String(name));
System.out.println("My course is " + new String(course));
}输出:
name's hashCode =19175605
name's address = [C@12498b5
My name is ee
My course is ee
name's hashCode =27634497
name's address = [C@1a5ab41
My name is ee
My course is ee
name's hashCode =26099296
name's address = [C@18e3e60
My name is ee
My course is ee
我将楼主的代码改一下,并经过编译,运行,达到了楼主的所要求的结果!代码如下:
class Person
{
char[] name;
char[] course;
public Person(char[] names,char[] courses)
{
name = new char[names.length];
course = new char[courses.length]; name=names;
course=courses;
//楼主,不好意思,我将你的字符数组复制语句屏闭了
// 既然是等长的数组为什么不可以直接赋值呢?
/*System.arraycopy(name,0,name,0,name.length);
System.arraycopy(course,0,course,0,course.length);*/
}
void who()
{
System.out.println("My name is " + new String(name));
System.out.println("My course is " + new String(course));
//之所以要转化成一个String,原因就是字符数组不能和String相加,同时它也
//是不能直接输出的哦
}}
public class ObjectArray
{
public static void main(String[] args)
{
Person[] persons = new Person[3]; char[] str1 = {'e','e'};
char[] str11 = {'e','e'};
char[] str2 = {'e','e'};
char[] str22 = {'e','e'};
char[] str3 = {'e','e'};
char[] str33 = {'e','e'}; persons[0] = new Person(str1,str11);
persons[1] = new Person(str2,str22);
persons[2] = new Person(str3,str33); for(int i = 0;i<3;++i)
{
persons[i].who();
} }}楼主,去试试吧,
祝你成功
我们大家是不是应该好好地讨论呢?
System.out.println("My name is " + name);
因为你输出"My name is ",编译器认为你要输出字符串,所以调用name.toString
显示出C@12498b5
pintln(字符数组名)这样将打印出数组的首地址,如跟在字符串后则首地址转化为字符串new String(字符数组名)这样将字符数组所指的内容转化为字符串对不?