Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

sending and arraylist in one class as an element to an arraylist of another class.

I have an arraylist declared in my class X

===>     ArrayList arr = new Arraylist();
Im my actionPerformed method I've add'd an element to my arrayList everytime a button was clicked.
Ex:
===>     arr.add("Element 1"); ..etc..

now I have another class Y which contains another ArrayList -

im trying to send the arraylist in my class X as an element to the arraylist that i have created in my class Y.
- How can i go about doing that, i know its easy but i can't seem to get it.
Thanks.
0
technomania
Asked:
technomania
  • 4
  • 3
1 Solution
 
girionisCommented:
You need to obtain the reference of your class Y. Then do something like:

class Y
{
    private ArrayList listFromClassX = null;
    public void setArrayList(ArrayList list)
   {
       listFromClassX = list;
   }

   // use the listFromClassX here
}

class X
{
   Y y = new Y();
   ArrayList arr = new Arraylist();
   arr.add("Element 1"); ..etc..

   y.setArrayList(arr);

}
0
 
technomaniaAuthor Commented:
Let me rephrase this.

How do I send/set my ArrayList arr,  in class X to my ArrayList arrList,  in class Y.

withotu getting null an an error etc etc..
0
 
girionisCommented:
My example above does exactly this.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
technomaniaAuthor Commented:
when do i do:       y.setArrayList(arr);

I'm adding elements in my actionPerformed


and in my class Y how would i set the arraylist recieved from X



Notice: ill add an extra 10 point for all your help...  sorry for the repetitive questions im a new to programming
0
 
girionisCommented:
> when do i do:       y.setArrayList(arr);

Where you want to set the array. But you have to replace y and arr with the variables you are using.

> and in my class Y how would i set the arraylist recieved from X

You need to have a setter method in your Y class that will be called from X. Something like

Y class
------

private ArrayList arr;
public void setList(ArrayList list)
{
    arr = list;
}

X class
------
y.setList(<your list here>);
0
 
technomaniaAuthor Commented:
y is my class name - doesn't    y.setList(<your list here>);  have to be assigned to a variable


if i used the method y.setList() from my class Y in my class X,   how does the arraylist in Y know what its new value is if the operation is done in X.
0
 
girionisCommented:
>  how does the arraylist in Y know what its new value is if the operation is done in X.

It will know since you will pass the array list reference in your setList() method. The setList takes a parameter which is of type ArrayList.

:)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now