我在写一个程序,测试过程中发现对象的值传递有错误,由于我对对象的理解不是很深刻,一直没有有效的改正错误,希望有高人能指点一二。现在我写了一个测试用的程序,一共由两个类组成Company和ReaderTest,其中main函数在ReaderTest中,源代码如下:/********************Company类开始***********************************/
public class Company {
public String name;
public Company[] next=new Company[5];//
public Company[] previous=new Company[5];//im Feld gespeichert
public Company(){//constructor without parameter
name="";
for(int i=0;i<this.next.length;i++){
this.next[i]=null;
this.previous[i]=null;
}
}
public Company(String name){
this.name=name;
for(int i=0;i<this.next.length;i++){
this.next[i]=new Company();
this.previous[i]=new Company();
}
}
public Company(String name, Company next, Company previous){
this.name=name;
this.next[0]=next;
this.previous[0]=previous;
for(int i=1;i<this.next.length;i++){
this.next[i]=new Company();
this.previous[i]=new Company();
}
} //constructor with parameter
public Company[] getNext(){
return next;
}
public void setNext(Company[] c){
this.next=c;
}
public void addNext(Company c){
int add=0;
for(int i=0;i<5;i++){
if(this.next[i].getName().compareTo("")==1)
//if((this.next[i].getName()).compareTo(new String())!=1)
//if(this.next[i].equals(null))
add=i;
}
this.next[add]=c;
}
/*public void delNext(){
not necessary for now!
}*/
public Company[] getPrevious(){
return previous;
}
public void setPrevious(Company[] c){
this.previous=c;
}
public void addPrevious(Company c){
int add=0;
for(int i=0;i<5;i++){
if(this.previous[i]==null)
add=i;
}
this.previous[add]=c;
}
// public void delPrevious(){}
public String getName(){
return this.name;
}
public void setName(String s){
this.name=s;
}
//this method return the number of the hinder companies
public int howManyNext(){
int a = 0;
//String empty=" ";
for(int r=0;r<this.next.length;r++){
//if(this.next[r]!=null)
if(!this.next[r].getName().equals(" "))
a=r;}
return a;
}
public void setDuplicate(Company c){
this.setName(c.getName());
this.setNext(c.getNext());
this.setPrevious(c.getPrevious());
}
}
/******************************company类结束************************//**************************ReaderTest类开始*************************/
public class ReaderTest {
public Company c1;
/** Creates a new instance of ReaderTest */
public ReaderTest() {
c1=new Company("Company1");
}
public static void main(String[] args){
Company c2=new Company();
Company c3=new Company("Company2");
Company c4=new Company("COMPANY4");
rt.c1.addNext(c3);
//c2=rt.c1;
c2.setDuplicate(rt.c1);
c2.addNext(c4);
System.out.println("the rt.c1 has the name: "+rt.c1.getName());
System.out.println("the c2 has the name: "+c2.getName());
System.out.println("rt.c1 has next company "+rt.c1.howManyNext()+".And its name is"+rt.c1.next[0].getName());
System.out.println("c2 has next company "+c2.howManyNext()+".And its name is"+c2.next[0].getName());
}
}
/***********************************ReaderTest类结束****************************/问题1:在ReaderTest的构造函数里我生成了一个Company c1,并且该Company有个string name属性为Company1,然后在后面我在c1的next数组中加入了另一个company c3;接下来我通过company类中的setDuplicate函数将c1的属性赋给了另一个company c2(这里c2的name是空字符串),并且在c2的next数组中我加入了第四个company c4 (c4的name属性值是COMPANY3).然后通过下面的System.out输出,然后可以看到c2的name属性被改为了Company1,这是我想得到的,但是在最后输出c1.next[0](这里应当是company2,也就是c3的name属性才对)时,却变成了"COMPANY3",也就是c4的属性值.请问,我该怎么改,才能在输出c1.next[0]时正确输出呢?
public class Company {
public String name;
public Company[] next=new Company[5];//
public Company[] previous=new Company[5];//im Feld gespeichert
public Company(){//constructor without parameter
name="";
for(int i=0;i<this.next.length;i++){
this.next[i]=null;
this.previous[i]=null;
}
}
public Company(String name){
this.name=name;
for(int i=0;i<this.next.length;i++){
this.next[i]=new Company();
this.previous[i]=new Company();
}
}
public Company(String name, Company next, Company previous){
this.name=name;
this.next[0]=next;
this.previous[0]=previous;
for(int i=1;i<this.next.length;i++){
this.next[i]=new Company();
this.previous[i]=new Company();
}
} //constructor with parameter
public Company[] getNext(){
return next;
}
public void setNext(Company[] c){
this.next=c;
}
public void addNext(Company c){
int add=0;
for(int i=0;i<5;i++){
if(this.next[i].getName().compareTo("")==1)
//if((this.next[i].getName()).compareTo(new String())!=1)
//if(this.next[i].equals(null))
add=i;
}
this.next[add]=c;
}
/*public void delNext(){
not necessary for now!
}*/
public Company[] getPrevious(){
return previous;
}
public void setPrevious(Company[] c){
this.previous=c;
}
public void addPrevious(Company c){
int add=0;
for(int i=0;i<5;i++){
if(this.previous[i]==null)
add=i;
}
this.previous[add]=c;
}
// public void delPrevious(){}
public String getName(){
return this.name;
}
public void setName(String s){
this.name=s;
}
//this method return the number of the hinder companies
public int howManyNext(){
int a = 0;
//String empty=" ";
for(int r=0;r<this.next.length;r++){
//if(this.next[r]!=null)
if(!this.next[r].getName().equals(" "))
a=r;}
return a;
}
public void setDuplicate(Company c){
this.setName(c.getName());
this.setNext(c.getNext());
this.setPrevious(c.getPrevious());
}
}
/******************************company类结束************************//**************************ReaderTest类开始*************************/
public class ReaderTest {
public Company c1;
/** Creates a new instance of ReaderTest */
public ReaderTest() {
c1=new Company("Company1");
}
public static void main(String[] args){
Company c2=new Company();
Company c3=new Company("Company2");
Company c4=new Company("COMPANY4");
rt.c1.addNext(c3);
//c2=rt.c1;
c2.setDuplicate(rt.c1);
c2.addNext(c4);
System.out.println("the rt.c1 has the name: "+rt.c1.getName());
System.out.println("the c2 has the name: "+c2.getName());
System.out.println("rt.c1 has next company "+rt.c1.howManyNext()+".And its name is"+rt.c1.next[0].getName());
System.out.println("c2 has next company "+c2.howManyNext()+".And its name is"+c2.next[0].getName());
}
}
/***********************************ReaderTest类结束****************************/问题1:在ReaderTest的构造函数里我生成了一个Company c1,并且该Company有个string name属性为Company1,然后在后面我在c1的next数组中加入了另一个company c3;接下来我通过company类中的setDuplicate函数将c1的属性赋给了另一个company c2(这里c2的name是空字符串),并且在c2的next数组中我加入了第四个company c4 (c4的name属性值是COMPANY3).然后通过下面的System.out输出,然后可以看到c2的name属性被改为了Company1,这是我想得到的,但是在最后输出c1.next[0](这里应当是company2,也就是c3的name属性才对)时,却变成了"COMPANY3",也就是c4的属性值.请问,我该怎么改,才能在输出c1.next[0]时正确输出呢?
解决方案 »
- 杭电2072用java做
- 看此段JAVA代码,为什么不完整显示?
- help me .....about JNDI............
- 请教java问题
- JTextField[] txtfldName=new JTextField[100]初始化时占内存么!??
- 能不能用自己的类的方法控制面板中paint方法?
- 错误:找不到对象
- 谁知道或者作过类似于微软资源管理器的东西?能否给我源代码?jTree.
- 请问哪里有加密算法的源代码(md5等)(40分,找到即送)
- JDK
- windows上怎么设置默认的swing的LAF?(J2SE1.6)
- 请问高手一个方法,得到文件夹(dictionary)里的每一个文件(file)
public String name;
public Company[] next=new Company[5];//
public Company[] previous=new Company[5];//im Feld gespeichert
public Company(){//constructor without parameter
name="";
for(int i=0;i<this.next.length;i++){
this.next[i]=null;
this.previous[i]=null;
}
}
public Company(String name){
this.name=name;
for(int i=0;i<this.next.length;i++){
this.next[i]=new Company();
this.previous[i]=new Company();
}
}
public Company(String name, Company next, Company previous){
this.name=name;
this.next[0]=next;
this.previous[0]=previous;
for(int i=1;i<this.next.length;i++){
this.next[i]=new Company();
this.previous[i]=new Company();
}
} //constructor with parameter
public Company[] getNext(){
return next;
}
public void setNext(Company[] c){
this.next=c;
}
public void addNext(Company c){
int add=0;
for(int i=0;i<5;i++){
if(this.next[i].getName().compareTo("")==1)
//if((this.next[i].getName()).compareTo(new String())!=1)
//if(this.next[i].equals(null))
add=i;
}
this.next[add]=c;
}
public Company[] getPrevious(){
return previous;
}
public void setPrevious(Company[] c){
this.previous=c;
}
public void addPrevious(Company c){
int add=0;
for(int i=0;i<5;i++){
if(this.previous[i]==null)
add=i;
}
this.previous[add]=c;
}
public String getName(){
return this.name;
}
public void setName(String s){
this.name=s;
} //this method return the number of the hinder companies
public int howManyNext(){
int a = 0;
//String empty=" ";
for(int r=0;r<this.next.length;r++){
//if(this.next[r]!=null)
if(!this.next[r].getName().equals(" "))
a=r;}
return a;
}
public void setDuplicate(Company c){
this.setName(c.getName());
this.setNext(c.getNext());
this.setPrevious(c.getPrevious());
}
}
另一个问题就是,我通过company中的howManyNext()方法来计算在每个company实例中的next数组中有多少company.(next的定义为
public Company[] next=new Company[5];).howManyNext方法如下:
public int howManyNext(){
int a = 0;
for(int r=0;r <this.next.length;r++){
//if(this.next[r]!=null)
if(!this.next[r].getName().equals(" "))
a=r;}
return a;
} 在ReaderTest的main方法中,我给c1和c2的next数组中都只添加了一个company,可是为什么根据我的howManyNext方法计算出来的值却总是4呢?无论我试用任何在howManyNext方法中的if判断,结果都是4,可是只应该返回1才对啊.请高手指点.谢谢.
// Company.java
public class Company {
private static final int CAPACITY = 5;
private String name;
private Company[] next = new Company[CAPACITY];
private Company[] previous = new Company[CAPACITY]; public Company() { } // keep a default constructor
public Company(String comName) {
name = comName;
} public Company(String comName, Company firstNext, Company firstPrevious) {
this(comName);
next[0] = firstNext;
previous[0] = firstPrevious;
}
public Company[] getNext() {
return next;
}
public void setNext(Company[] c) {
next = c;
} public void addNext(Company c) {
for (int i = 0; i < CAPACITY; i++)
if (next[i] == null) {
next[i] = c;
return;
}
} public Company[] getPrevious() {
return previous;
}
public void setPrevious(Company[] c) {
previous = c;
} public void addPrevious(Company c) {
for (int i = 0; i < CAPACITY; i++)
if (previous[i] == null) {
previous[i] = c;
return;
}
} public String getName() {
return name;
}
public void setName(String s) {
name = s;
} public int howManyNext() {
int result = 0;
for (int i = 0; i < CAPACITY; i++)
if (next[i] != null) result++;
return result;
} public void setDuplicate(Company c) {
setName(c.getName());
setNext(c.getNext());
setPrevious(c.getPrevious());
}
}
RT.c11 has next company 1.And ITS name isCompany2
the rt.c11 has the name: Company1
the c21 has the name: Company1
rt.c11 has next company 2.And its name isCompany2
c21 has next company 2.And its name isCompany2
当我在c21(一个company)的next数组中再加入一个company后,为什么c11的next数组中也变成了两个company对象呢?因为开始我只加入了一个company啊?
public class ReaderTest { public Company c1; /** Creates a new instance of ReaderTest */
public ReaderTest() {
c1 = new Company("Company1");
} public static void main(String[] args) { Company c2 = new Company();
Company c3 = new Company("Company2");
Company c4 = new Company("COMPANY4");
rt.c1.addNext(c3);
//c2=rt.c1;
c2.setDuplicate(rt.c1);
c2.addNext(c4);
System.out.println("the rt.c1 has the name: " + rt.c1.getName());
System.out.println("the c2 has the name: " + c2.getName());
System.out.println("rt.c1 has next company " + rt.c1.howManyNext() + ".And its name is" + rt.c1.next[0].getName());
System.out.println("c2 has next company " + c2.howManyNext() + ".And its name is" + c2.next[0].getName()); }
}
[/java]
你rt 哪来的,你都能运行的好好的,佩服你
程序相当的有问题
public void addNext(Company c) {
int add = 0;
for (int i = 0; i < 5; i++) {
if (this.next[i].getName().compareTo("") == 1)
{
add = i;
}
}
System.out.println("add:"+add);
this.next[add] = c;
}执行 c2.addNext(c4); 这时候,就将原先的c3 替换成 c4
你这个this.next[add] 这个add 永远都是0,
public void setNext(Company[] c) {
System.arraycopy(c, 0, next, 0, c.length < CAPACITY ? c.length : CAPACITY);
}
当然,setPrevious()也需作类似更改。