java List<> 的问题

发布于 2024-11-05 05:41:23 字数 2704 浏览 5 评论 0 原文

我对 java List 和 arrayList 不太熟悉..我只需要一些东西可以顺利地进行追加和排序。

我的算法很简单:

set a father string 
add father to speciesList
    mutate father to some new child
    make this new child the future father
    go to step 2

这里给出了 ga_ga_struct 的定义

public class ga_struct {

    public String gene;
    public int fitness;

}


public class ga_{

    public List<ga_struct> vector= new ArrayList<ga_struct>();

        public void sortspecies()
        {
        Collections.sort(vector,new Comparator<ga_struct>() {
        @Override
        public int compare(ga_struct o1, ga_struct o2) {
            int res;
            if(o1.fitness<o2.fitness)
                res=-1;
            else if(o1.fitness>o2.fitness)
                res=1;
            else 
                res=0;
            return res;
                 }
              }
                  );

     }


    public ga_struct mutate(ga_struct parent)
    {
        Random r= new Random();
        ......     do some modification to the parent
        return parent;
    }
}

我一直在这样做

        ga_ newSpecies = new ga_();
        Random r= new Random(10);
        ga_struct father= new ga_struct();
        father.gene="123";
        newSpecies.vector.add(father);

        for (int i = 1; i < 10; i++) {
            ga_struct ng = new ga_struct();        
            ng=newSpecies.mutate(father);
            ng.fitness=i;
            newSpecies.vector.add(ng);
            father=ng;          
            System.out.println(newSpecies.vector.get(i).gene+" with fitness factor "+newSpecies.vector.get(i).fitness);

        }

        newSpecies.sortspecies();
        System.out.println("\ncurrent population\n");

        for (int i = 0; i < 10; i++) {
            System.out.println(newSpecies.vector.get(i).gene+" with fitness factor "+newSpecies.vector.get(i).fitness);
        }

mutator 函数只是改变 String(gene)一次一个字符。我刚刚在第一个循环中从“父亲”那里突变了 9 个新物种。但是..我不知道为什么代码的输出给了我这个-

133 with fitness factor 1
433 with fitness factor 2
433 with fitness factor 3
443 with fitness factor 4
453 with fitness factor 5
553 with fitness factor 6
563 with fitness factor 7
563 with fitness factor 8
573 with fitness factor 9

current population

573 with fitness factor 9
573 with fitness factor 9
573 with fitness factor 9
573 with fitness factor 9
573 with fitness factor 9
573 with fitness factor 9
573 with fitness factor 9
573 with fitness factor 9
573 with fitness factor 9
573 with fitness factor 9

第一个循环证明突变正在缓慢进行..而且我还在突变后立即添加了,那么为什么后来所有这些都被覆盖了按最新版本?

I am not familiar much with the java List and arrayList .. i just need something to work smoothly to append and sort.

My algorithm is simple:

set a father string 
add father to speciesList
    mutate father to some new child
    make this new child the future father
    go to step 2

The definitions of ga_ and ga_struct is given here

public class ga_struct {

    public String gene;
    public int fitness;

}


public class ga_{

    public List<ga_struct> vector= new ArrayList<ga_struct>();

        public void sortspecies()
        {
        Collections.sort(vector,new Comparator<ga_struct>() {
        @Override
        public int compare(ga_struct o1, ga_struct o2) {
            int res;
            if(o1.fitness<o2.fitness)
                res=-1;
            else if(o1.fitness>o2.fitness)
                res=1;
            else 
                res=0;
            return res;
                 }
              }
                  );

     }


    public ga_struct mutate(ga_struct parent)
    {
        Random r= new Random();
        ......     do some modification to the parent
        return parent;
    }
}

I have been doing this

        ga_ newSpecies = new ga_();
        Random r= new Random(10);
        ga_struct father= new ga_struct();
        father.gene="123";
        newSpecies.vector.add(father);

        for (int i = 1; i < 10; i++) {
            ga_struct ng = new ga_struct();        
            ng=newSpecies.mutate(father);
            ng.fitness=i;
            newSpecies.vector.add(ng);
            father=ng;          
            System.out.println(newSpecies.vector.get(i).gene+" with fitness factor "+newSpecies.vector.get(i).fitness);

        }

        newSpecies.sortspecies();
        System.out.println("\ncurrent population\n");

        for (int i = 0; i < 10; i++) {
            System.out.println(newSpecies.vector.get(i).gene+" with fitness factor "+newSpecies.vector.get(i).fitness);
        }

The mutator function just alter the String(gene) one character at a time. I just mutated 9 new species from the "father" in the first loop. But.. I dont know why the output of the code is giving me this-

133 with fitness factor 1
433 with fitness factor 2
433 with fitness factor 3
443 with fitness factor 4
453 with fitness factor 5
553 with fitness factor 6
563 with fitness factor 7
563 with fitness factor 8
573 with fitness factor 9

current population

573 with fitness factor 9
573 with fitness factor 9
573 with fitness factor 9
573 with fitness factor 9
573 with fitness factor 9
573 with fitness factor 9
573 with fitness factor 9
573 with fitness factor 9
573 with fitness factor 9
573 with fitness factor 9

The first loop is proof that mutation is going slowly.. And i also added immediately after a mutation, then why is that later on all of them are just overwritten by the latest edition?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一抹苦笑 2024-11-12 05:41:23

首先,你的对象使用有点奇怪。

在 mutate 中,你似乎正在改变并返回父亲。

这意味着您的列表将包含对同一实例的多个引用。

澄清一下:

public ga_struct mutate(ga_struct parent) //takes in reference to parent
{
    Random r= new Random(); //modifies parent
    ......     do some modification to the parent
    return parent; //return reference to parent
}

在您的主要内容中:

    ga_ newSpecies = new ga_();
    Random r= new Random(10);
    ga_struct father= new ga_struct();//instantiate father
    father.gene="123";
    newSpecies.vector.add(father);

    for (int i = 1; i < 10; i++) {
        ga_struct ng = new ga_struct();//create new instance for child
        ng=newSpecies.mutate(father);//set ng as reference to same instance as father, instance instantiated on previous line is discarded
        ng.fitness=i;
        newSpecies.vector.add(ng);
        father=ng;          
        System.out.println(newSpecies.vector.get(i).gene+" with fitness factor "+newSpecies.vector.get(i).fitness);

    }

尝试更多类似这样的内容:

    public ga_struct mutate(ga_struct parent)
{
    ga_struct ng = new ga_struct();
    ng.gene = father.gene;
    Random r= new Random();
    //do some modification to ng
    return ng;
}

在您的主要内容中:

a_ newSpecies = new ga_();
    Random r= new Random(10);
    ga_struct father= new ga_struct();
    father.gene="123";
    newSpecies.vector.add(father);

    for (int i = 1; i < 10; i++) {    
        ga_struct ng=newSpecies.mutate(father);
        ng.fitness=i;
        newSpecies.vector.add(ng);
        father=ng;          
        System.out.println(newSpecies.vector.get(i).gene+" with fitness factor "+newSpecies.vector.get(i).fitness);

    }

    newSpecies.sortspecies();
    System.out.println("\ncurrent population\n");

    for (int i = 0; i < 10; i++) {
        System.out.println(newSpecies.vector.get(i).gene+" with fitness factor "+newSpecies.vector.get(i).fitness);
    }

First off, your object usage is a bit weird.

In mutate, you seem to be changing and returning the father.

This means your list will contain multiple references to the same instance.

to clarify:

public ga_struct mutate(ga_struct parent) //takes in reference to parent
{
    Random r= new Random(); //modifies parent
    ......     do some modification to the parent
    return parent; //return reference to parent
}

And in your main:

    ga_ newSpecies = new ga_();
    Random r= new Random(10);
    ga_struct father= new ga_struct();//instantiate father
    father.gene="123";
    newSpecies.vector.add(father);

    for (int i = 1; i < 10; i++) {
        ga_struct ng = new ga_struct();//create new instance for child
        ng=newSpecies.mutate(father);//set ng as reference to same instance as father, instance instantiated on previous line is discarded
        ng.fitness=i;
        newSpecies.vector.add(ng);
        father=ng;          
        System.out.println(newSpecies.vector.get(i).gene+" with fitness factor "+newSpecies.vector.get(i).fitness);

    }

Try Something more like this:

    public ga_struct mutate(ga_struct parent)
{
    ga_struct ng = new ga_struct();
    ng.gene = father.gene;
    Random r= new Random();
    //do some modification to ng
    return ng;
}

and in your main:

a_ newSpecies = new ga_();
    Random r= new Random(10);
    ga_struct father= new ga_struct();
    father.gene="123";
    newSpecies.vector.add(father);

    for (int i = 1; i < 10; i++) {    
        ga_struct ng=newSpecies.mutate(father);
        ng.fitness=i;
        newSpecies.vector.add(ng);
        father=ng;          
        System.out.println(newSpecies.vector.get(i).gene+" with fitness factor "+newSpecies.vector.get(i).fitness);

    }

    newSpecies.sortspecies();
    System.out.println("\ncurrent population\n");

    for (int i = 0; i < 10; i++) {
        System.out.println(newSpecies.vector.get(i).gene+" with fitness factor "+newSpecies.vector.get(i).fitness);
    }
电影里的梦 2024-11-12 05:41:23

您没有创建新对象,而是将父对象添加到向量中 9 次。

本质上你所拥有的是

父亲 -> obj@123

你的 List 对象看起来像这样
[ obj@123, obj@123, obj@123, ... ]

您将需要创建新实例来记录这一点。我建议实施“clone()”方法来执行此操作。

You're not creating a new object, you've added the father object 9 times to the vector.

Essentially what you've got is

father -> obj@123

What your List object looks like is
[ obj@123, obj@123, obj@123, ... ]

You're going to need to create new instances to record this. I would recommend implementing the "clone()" method to do this.

娇柔作态 2024-11-12 05:41:23

您到处都在使用单个对象,您永远不会向列表中添加新的 ga_struct 实例。您的 mutate() 方法似乎只是修改了 parent 参数并返回它 - 它仍然是同一个对象,只是被修改了,这意味着它在任何地方都被修改了。

public ga_struct mutate(ga_struct parent)
{
    Random r= new Random();
    ......     do some modification to the parent
    return parent;
}

您确实创建了一个新的 ga_struct 实例,但您立即通过设置对变异的父亲的引用来覆盖它(这仍然是同一个实例,只是修改了)

for (int i = 1; i < 10; i++) {
        ga_struct ng = new ga_struct();        
        ng=newSpecies.mutate(father); //the new ga_struct is overwritten
        ng.fitness=i;
        newSpecies.vector.add(ng);
        father=ng;          
        System.out.println(newSpecies.vector.get(i).gene+" with fitness factor "+newSpecies.vector.get(i).fitness);

    }

:这个循环似乎有效,因为您可以按照发生的顺序看到对 father 的修改。 但是,您实际上所做的只是一遍又一遍地将对同一(已修改)对象的引用添加到List中。

因此,当您最终将它们全部打印出来时,您会在 List 中看到 10 个重复条目。


我的建议是更改 mutate() 以返回 ga_struct 的新实例 - 您可以创建一个新对象并将其 gene 字段设置为是来自 parent 的突变 gene 字段。或者您可以 克隆 parentthen 更改克隆的基因字符串。无论哪种情况,您最终都会返回一个新的 ga_struct 实例,这应该可以解决问题。

public ga_struct mutate(ga_struct parent)
{
    Random r= new Random();
    ga_struct mutant = parent.clone(); 
   //or 
   //ga_struct mutant = new ga_struct();
   //mutant.gene = parent.gene;

    ......     do some modification to the mutant
    return mutant; //now you'll be returning a new object not just a modified one
}

You're working with a single object everywhere, you never add a new ga_struct instance to the list. Your mutate() method appears to simply modify the parent parameter and returns it - it's still the same object, just modified, which means it's modified everywhere.

public ga_struct mutate(ga_struct parent)
{
    Random r= new Random();
    ......     do some modification to the parent
    return parent;
}

You do create a new instance of ga_struct but you immediately overwrite it by setting the reference to the mutated father (which is still the same instance, just modified):

for (int i = 1; i < 10; i++) {
        ga_struct ng = new ga_struct();        
        ng=newSpecies.mutate(father); //the new ga_struct is overwritten
        ng.fitness=i;
        newSpecies.vector.add(ng);
        father=ng;          
        System.out.println(newSpecies.vector.get(i).gene+" with fitness factor "+newSpecies.vector.get(i).fitness);

    }

Your output in this loop seems to work because you see the modifications to father in the order they happen. However, what you're actually doing is just adding references to the same (modified) object over and over into the List.

Thus, when you finally print them all out, you see 10 duplicate entries in the List.


My suggestion is to change mutate() to return a new instance of ga_struct - you could either create a new object and set it's gene field to be the mutated gene field from parent. Or you could clone parent and then change the clone's gene string. In either case, you will end up returning a new instance of ga_struct which should fix the problem.

public ga_struct mutate(ga_struct parent)
{
    Random r= new Random();
    ga_struct mutant = parent.clone(); 
   //or 
   //ga_struct mutant = new ga_struct();
   //mutant.gene = parent.gene;

    ......     do some modification to the mutant
    return mutant; //now you'll be returning a new object not just a modified one
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文