Solved

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

Posted on 2004-09-01
7
249 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 95
recursion example 16 124
HSSFWorkbook cannot be resolved error 10 65
print map entry 34 67
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

815 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

12 Experts available now in Live!

Get 1:1 Help Now