Solved

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

Posted on 2004-09-01
7
242 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

743 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

15 Experts available now in Live!

Get 1:1 Help Now