Search code examples
javaooparraylistgetter-setter

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.

There is a networkoffoos class with an ArrayList of foo objects called listoffoos. There is an updatefoomsg method:

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. However, no values in the foo class ArrayList `msgstoboo' are altered. Why is this and how do I fix it? Thank you in advance.

UPDATE: Here are the whole foo and networkoffoos classes

 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**
}

Solution

  • 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

    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);
        }
    }