Solved

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

Posted on 2004-09-01
7
245 Views
Last Modified: 2010-03-31
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
Comment
Question by:technomania
  • 4
  • 3
7 Comments
 
LVL 35

Accepted Solution

by:
girionis earned 30 total points
ID: 11956553
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
 

Author Comment

by:technomania
ID: 11957494
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
 
LVL 35

Expert Comment

by:girionis
ID: 11957640
My example above does exactly this.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:technomania
ID: 11957707
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
 
LVL 35

Expert Comment

by:girionis
ID: 11957877
> 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
 

Author Comment

by:technomania
ID: 11958534
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
 
LVL 35

Expert Comment

by:girionis
ID: 11960833
>  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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now