繁体   English   中英

方法对象返回相同的值java

[英]method object return same value java

我在循环内部的对象中运行方法存在问题,它保存相同的值,当我在循环内部调用它时,我的方法mutasi()给出相同的结果。 当我试图在方法本身中打印所有结果时,没有任何问题,但是当我将其保存在数组后代中时,它返回相同的值。 有什么建议吗?

编码 :

public class Offspringset {

Individu[] offspring;
int lamdhapermiu; //recommend 7miu
Individu tempmutan;

public Offspringset(Parentset parentset, int lamdhapermiu,int miu) {
    this.lamdhapermiu = lamdhapermiu;
    this.offspring = new Individu[lamdhapermiu*miu];
    int lamdhacount=0;

    for(int i=0;i<miu;i++){

        for(int j=0;j<lamdhapermiu;j++){
            Individu tempindividu=new Individu(parentset.parent[i]);

            //tempindividu=parentset.parent[i];
            tempindividu.setId("C"+(lamdhacount+1));
            tempindividu.parent=parentset.parent[i].parent;
            Individu temp=new Individu(tempindividu);
            temp.mutasi();
            tempindividu=temp;
            this.offspring[lamdhacount]=tempindividu;


            lamdhacount++;
        }


    }

    System.out.println("--------------child--------------------");
    for (int i = 0; i < lamdhapermiu*miu; i++) {
        System.out.printf(" "+this.offspring[i].getId());
        for (int j = 0; j < 18; j++) {
            System.out.printf("| %.4f", this.offspring[i].chromosome[j]); //it returns wrong

        }
        System.out.println("");


    }
}

结果:

 C1| 1.6606| 3.9865| 6.5703| 1.4477| 3.5736| 7.2361| 0.9644| 4.1887| 3.4248| 0.9027| 0.5009| 0.9281| 0.4051| 0.9037| 0.5211| 0.0462| 0.8732| 0.9393
 C2| 1.6606| 3.9865| 6.5703| 1.4477| 3.5736| 7.2361| 0.9644| 4.1887| 3.4248| 0.9027| 0.5009| 0.9281| 0.4051| 0.9037| 0.5211| 0.0462| 0.8732| 0.9393
 C3| 1.6606| 3.9865| 6.5703| 1.4477| 3.5736| 7.2361| 0.9644| 4.1887| 3.4248| 0.9027| 0.5009| 0.9281| 0.4051| 0.9037| 0.5211| 0.0462| 0.8732| 0.9393
 C4| 1.5136| 4.6995| 4.0775| 1.5884| 3.5905| 5.7202| 1.7121| 2.9318| 7.1751| 0.3659| 0.8514| 0.7796| 0.4205| 0.3340| 0.2582| 0.3790| 0.2445| 0.6477
 C5| 1.5136| 4.6995| 4.0775| 1.5884| 3.5905| 5.7202| 1.7121| 2.9318| 7.1751| 0.3659| 0.8514| 0.7796| 0.4205| 0.3340| 0.2582| 0.3790| 0.2445| 0.6477
 C6| 1.5136| 4.6995| 4.0775| 1.5884| 3.5905| 5.7202| 1.7121| 2.9318| 7.1751| 0.3659| 0.8514| 0.7796| 0.4205| 0.3340| 0.2582| 0.3790| 0.2445| 0.6477
 C7| 1.5548| 3.3704| 1.9704| 1.2909| 3.2797| 6.5624| 1.8093| 3.4153| 4.0156| 0.8202| 0.3824| 0.2515| 0.5027| 0.5620| 0.9876| 0.8433| 0.4102| 0.7697
 C8| 1.5548| 3.3704| 1.9704| 1.2909| 3.2797| 6.5624| 1.8093| 3.4153| 4.0156| 0.8202| 0.3824| 0.2515| 0.5027| 0.5620| 0.9876| 0.8433| 0.4102| 0.7697
 C9| 1.5548| 3.3704| 1.9704| 1.2909| 3.2797| 6.5624| 1.8093| 3.4153| 4.0156| 0.8202| 0.3824| 0.2515| 0.5027| 0.5620| 0.9876| 0.8433| 0.4102| 0.7697
 C10| 1.5419| 2.8423| 2.9715| 1.8464| 3.0915| 6.4773| 1.5050| 3.6399| 1.6165| 0.4277| 0.0204| 0.2619| 0.6590| 0.2884| 0.3991| 0.7292| 0.7240| 0.7339
 C11| 1.5419| 2.8423| 2.9715| 1.8464| 3.0915| 6.4773| 1.5050| 3.6399| 1.6165| 0.4277| 0.0204| 0.2619| 0.6590| 0.2884| 0.3991| 0.7292| 0.7240| 0.7339
 C12| 1.5419| 2.8423| 2.9715| 1.8464| 3.0915| 6.4773| 1.5050| 3.6399| 1.6165| 0.4277| 0.0204| 0.2619| 0.6590| 0.2884| 0.3991| 0.7292| 0.7240| 0.7339
 C13| 1.5365| 3.7929| 6.8675| 2.0287| 4.0303| 4.4369| 2.4485| 3.4018| 4.1677| 0.4773| 0.4532| 0.3529| 0.8249| 0.5309| 0.9743| 0.9907| 0.3117| 0.0450
 C14| 1.5365| 3.7929| 6.8675| 2.0287| 4.0303| 4.4369| 2.4485| 3.4018| 4.1677| 0.4773| 0.4532| 0.3529| 0.8249| 0.5309| 0.9743| 0.9907| 0.3117| 0.0450
 C15| 1.5365| 3.7929| 6.8675| 2.0287| 4.0303| 4.4369| 2.4485| 3.4018| 4.1677| 0.4773| 0.4532| 0.3529| 0.8249| 0.5309| 0.9743| 0.9907| 0.3117| 0.0450

mutasi()方法:

public void mutasi() {

    for (int i = 0; i < 9; i++) {
        double N = Math.random();
        double temp;

        temp = this.chromosome[i] + this.chromosome[i + 9] * N;
        this.chromosome[i] = temp;
    }
    this.printchromosome(); //this print result right

    //System.out.printf("+(");
}

结果:

  | 1.3537| 3.4028| 5.8037| 1.1074| 2.9021| 7.0337| 0.9290| 3.4556| 2.9172| 0.9027| 0.5009| 0.9281| 0.4051| 0.9037| 0.5211| 0.0462| 0.8732| 0.9393
| 1.4021| 3.5828| 6.3855| 1.1941| 3.5688| 7.0893| 0.9464| 3.8476| 3.0895| 0.9027| 0.5009| 0.9281| 0.4051| 0.9037| 0.5211| 0.0462| 0.8732| 0.9393
| 1.6606| 3.9865| 6.5703| 1.4477| 3.5736| 7.2361| 0.9644| 4.1887| 3.4248| 0.9027| 0.5009| 0.9281| 0.4051| 0.9037| 0.5211| 0.0462| 0.8732| 0.9393
| 0.9910| 3.5737| 3.2226| 1.0655| 3.3229| 5.4214| 1.2509| 2.6832| 6.6942| 0.3659| 0.8514| 0.7796| 0.4205| 0.3340| 0.2582| 0.3790| 0.2445| 0.6477
| 1.2665| 3.9124| 3.7090| 1.3724| 3.5358| 5.6115| 1.4296| 2.7904| 7.0733| 0.3659| 0.8514| 0.7796| 0.4205| 0.3340| 0.2582| 0.3790| 0.2445| 0.6477
| 1.5136| 4.6995| 4.0775| 1.5884| 3.5905| 5.7202| 1.7121| 2.9318| 7.1751| 0.3659| 0.8514| 0.7796| 0.4205| 0.3340| 0.2582| 0.3790| 0.2445| 0.6477
| 0.9256| 3.0435| 1.6178| 1.1447| 3.1867| 5.9332| 0.9231| 2.9944| 3.2427| 0.8202| 0.3824| 0.2515| 0.5027| 0.5620| 0.9876| 0.8433| 0.4102| 0.7697
| 1.2763| 3.0614| 1.7924| 1.2444| 3.2046| 5.9892| 1.2278| 3.2749| 3.6367| 0.8202| 0.3824| 0.2515| 0.5027| 0.5620| 0.9876| 0.8433| 0.4102| 0.7697
| 1.5548| 3.3704| 1.9704| 1.2909| 3.2797| 6.5624| 1.8093| 3.4153| 4.0156| 0.8202| 0.3824| 0.2515| 0.5027| 0.5620| 0.9876| 0.8433| 0.4102| 0.7697
| 1.2030| 2.8134| 2.7439| 1.4461| 2.7947| 6.0430| 1.3756| 3.1954| 0.9816| 0.4277| 0.0204| 0.2619| 0.6590| 0.2884| 0.3991| 0.7292| 0.7240| 0.7339
| 1.4196| 2.8308| 2.8237| 1.7483| 2.9691| 6.1715| 1.4428| 3.6352| 1.2668| 0.4277| 0.0204| 0.2619| 0.6590| 0.2884| 0.3991| 0.7292| 0.7240| 0.7339
| 1.5419| 2.8423| 2.9715| 1.8464| 3.0915| 6.4773| 1.5050| 3.6399| 1.6165| 0.4277| 0.0204| 0.2619| 0.6590| 0.2884| 0.3991| 0.7292| 0.7240| 0.7339
| 1.0095| 3.0884| 6.5031| 1.0802| 3.4282| 2.8938| 1.4719| 3.0121| 4.1108| 0.4773| 0.4532| 0.3529| 0.8249| 0.5309| 0.9743| 0.9907| 0.3117| 0.0450
| 1.0731| 3.3825| 6.8218| 1.6030| 3.5273| 3.7966| 1.4901| 3.3131| 4.1389| 0.4773| 0.4532| 0.3529| 0.8249| 0.5309| 0.9743| 0.9907| 0.3117| 0.0450
| 1.5365| 3.7929| 6.8675| 2.0287| 4.0303| 4.4369| 2.4485| 3.4018| 4.1677| 0.4773| 0.4532| 0.3529| 0.8249| 0.5309| 0.9743| 0.9907| 0.3117| 0.0450

当您使用Individu tempindividu=new Individu(parentset.parent[i]);创建您的Individu实例时, Individu tempindividu=new Individu(parentset.parent[i]); ,您使用的是一个构造函数,该函数会将chromosome数组的引用从源Individu复制到新创建的Individu

public Individu(Individu ind) {
    this.setId(ind.id);
    this.chromosome = ind.chromosome;
    this.parent = null;
}

这意味着使用此构造函数创建的所有Individu实例共享相同的chromosome阵列,更改其中一个将会更改所有chromosome

您应该为每个实例创建一个不同的chromosome阵列。 例如 :

public Individu(Individu ind) {
    this.setId(ind.id);
    this.chromosome = new double[ind.chromosome.length];
    for (int i = 0; i < ind.chromosome.length; i++)
        this.chromosome[i] = ind.chromosome[i];
    this.parent = null;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM