Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 方法对象返回相同的值_Java_Arrays_Loops_Methods - Fatal编程技术网

Java 方法对象返回相同的值

Java 方法对象返回相同的值,java,arrays,loops,methods,Java,Arrays,Loops,Methods,我在循环内对象中的run方法存在问题,它保存相同的值,我的方法mutasi()在循环内调用它时给出相同的结果。当我试图打印方法本身内部的所有结果时,没有任何问题,但当我将其保存在数组子代中时,它返回相同的值。有什么建议吗 守则: public class Offspringset { Individu[] offspring; int lamdhapermiu; //recommend 7miu Individu tempmutan; public Offspringset(Parentse

我在循环内对象中的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("");


    }
}
mutasi()方法:


当您使用
Individu tempindidu=newindividu(parentset.parent[i])创建
Individu
实例时
,您使用的构造函数将
染色体
数组的引用从源
个体
复制到新创建的
个体

public Individu(Individu ind) {
    this.setId(ind.id);
    this.chromosome = ind.chromosome;
    this.parent = null;
}
这意味着使用此构造函数创建的所有
Individu
实例共享相同的
chrome
数组,更改其中一个实例将更改所有实例

您应该为每个实例创建不同的
数组。例如:

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;
}
public Individu(Individu ind){
这个.setId(ind.id);
this.chromose=新的双染色体[ind.chromose.length];
for(int i=0;i
它在哪里返回相同的结果(或者实际上返回的结果在哪里)?您在哪里将
染色体[n]
设置为零以外的值?@PeterLawrey当我尝试打印“后代”时,它返回相同的结果数组,但当我在方法内部打印染色体时,没有任何问题。你能告诉我a)第一组值在哪里b)在哪里打印的值是正确的,c)在哪里使用
返回结果d)这是一个问题。您描述的所有内容似乎都在谈论您没有包含的代码。@PeterLawrey plese请看编辑的版本!谢谢:D,顺便问一下,有没有关于java的推荐书包括这样的内容?通常我都有一些基本的书籍,但并没有对此做太多说明。@SatriaLoka调试器是找到这些bug的最简单的方法。顺便说一句
this.chromose=ind.chromose.clone()
也会做同样的事情。
  | 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
public Individu(Individu ind) {
    this.setId(ind.id);
    this.chromosome = ind.chromosome;
    this.parent = null;
}
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;
}