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.
===>     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.
Who is Participating?

Improve company productivity with a Business Account.Sign Up

girionisConnect With a Mentor Commented:
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..


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..
My example above does exactly this.
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.