简体   繁体   English

在一个班级中从另一个班级设置ArrayList项目

[英]Setting ArrayList items in one Class from another Class

There is a foo class with an ArrayList of double msg called msgstoboo as well as a method setMsg(int index, double input) to alter individual messages in msgstoboo . 有一个foo类,带有一个名为msgstoboodouble msg的ArrayList以及一个用于更改msgstoboo各个消息的setMsg(int index, double input) msgstoboo

There is a networkoffoos class with an ArrayList of foo objects called listoffoos . 有一个networkoffoos类,其中包含一个名为listoffoos的foo对象的ArrayList。 There is an updatefoomsg method: 有一个updatefoomsg方法:

public void updatefoomsg (ArrayList<ArrayList<Foo>> Foonetwork)
   {
     for(int foolayer = 0; foolayer< foonetwork.size(); foolayer++)
       for(int fooinlayer = 0; fooinlayer< foonetwork.get(foolayer).size(); fooinlayer++)
         for(int msginfoo = 0; msginfoo < foonetwork.get(foolayer).get(fooinlayer).msgstoboo.size(); msginfoo++)
             Foonetwork.get(foolayer).get(fooinlayer).setMsg(msginfoo,somerandomvalue)
   }

The goal of updatefoomsg is to change the values of individual msgs in msgstooboo . 的目标updatefoomsg是改变个人的MSG的值msgstooboo However, no values in the foo class ArrayList `msgstoboo' are altered. 但是, foo类ArrayList'msgstoboo'中的值没有更改。 Why is this and how do I fix it? 为什么会这样,我该如何解决? Thank you in advance. 先感谢您。

UPDATE: Here are the whole foo and networkoffoos classes 更新:这是整个foonetworkoffoos

 public class foo
 {
  ArrayList<Double> msgstoboo = new ArrayList<Double>(Double);
  public foo(int numofmessages)
    {
     for (int i = 0; i < numofmessages; i++)
         {
         msgstoboo.add(1); 
         }
    }
  public void setMsg(int index, double input)
  {
      msgstoboo.set(index,input);
  }

&& &&

public class networkoffoos
{
    ArrayList<ArrayList<foo>> foonetwork = new ArrayList<ArrayList<foo>>();
    public void networkoffoos(int numoffoos)
    {
        for(int i = 0; i < numoffoos; i++)
        foonetwork.add(new foo(somenumberofmsgs))
    }
    //**AND THE "updatefoomsg" method included in this post**
}

The following works for me. 以下对我有用。 Made some small changes because your code wouldn't compile. 进行了一些小更改,因为您的代码无法编译。 Hope it helps. 希望能帮助到你。

import java.util.ArrayList;

public class test {
    private static ArrayList<ArrayList<Foo>> foonetwork = new ArrayList<ArrayList<Foo>>();

    public static void main(String[] args){
        networkoffoos(5);
        updatefoomsg(foonetwork);
    }

    public static void networkoffoos(int numoffoos) {
        for(int i = 0; i < numoffoos; i++) {
            ArrayList<Foo> fooArrayList = new ArrayList<Foo>();
            fooArrayList.add(new Foo(10));
            foonetwork.add(fooArrayList);
        }
    }

    public static void updatefoomsg (ArrayList<ArrayList<Foo>> foonetwork) {
        for(int foolayer = 0; foolayer< foonetwork.size(); foolayer++)
            for(int fooinlayer = 0; fooinlayer< foonetwork.get(foolayer).size(); fooinlayer++)
                for(int msginfoo = 0; msginfoo < foonetwork.get(foolayer).get(fooinlayer).msgstoboo.size(); msginfoo++)
                    foonetwork.get(foolayer).get(fooinlayer).setMsg(msginfoo,3);
    }
}

and your Foo class 和你的Foo

import java.util.ArrayList;

public class Foo {
    ArrayList<Double> msgstoboo = new ArrayList<Double>();

    public Foo(int numofmessages) {
        for (int i = 0; i < numofmessages; i++) {
            msgstoboo.add(Double.valueOf(1));
        }
    }

    public void setMsg(int index, double input) {
        msgstoboo.set(index, input);
    }
}

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

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