【问题标题】:method object return same value java方法对象返回相同的值java
【发布时间】:2014-12-27 14:17:55
【问题描述】:

我在循环内的对象中的 run 方法存在问题,它保存相同的值,我的方法 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

【问题讨论】:

  • 它在哪里返回相同的结果(或者实际上在哪里返回结果)?
  • 您在哪里将chromosome[n] 设置为零以外的值?
  • @PeterLawrey 当我尝试打印“后代”数组时它返回相同的结果但是当我在方法本身内打印染色体时没有任何问题。
  • 你能告诉我 a) 第一次设置的值在哪里 b) 在哪里打印的值是正确的,c) 你在哪里使用 return result; d) 这是一个问题。您描述的所有内容似乎都在谈论您未包含的代码。
  • @PeterLawrey 请查看编辑后的版本

标签: java arrays loops methods


【解决方案1】:

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

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

这意味着使用此构造函数创建的所有 Individu 实例共享同一个 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;
}

【讨论】:

  • 它有效!谢谢 :D,顺便说一下,有什么推荐的关于 java 的书籍涵盖这样的内容吗?通常我有一些非常基本的书,对此并没有多说。
  • @SatriaLoka 您的调试器是查找这些错误的最简单方法。
  • 顺便说一句 this.chromosome = ind.chromosome.clone(); 会做同样的事情。
猜你喜欢
  • 2017-03-26
  • 1970-01-01
  • 2017-07-17
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
  • 2016-04-10
  • 2014-11-27
  • 1970-01-01
相关资源
最近更新 更多