Solved

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

Posted on 2004-09-01
7
257 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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!

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Apache Camel - Unable to propagate JMS Header Properties between Request - Response 3 19
servlet example 11 59
mysql jsp example issue 32 75
collection output issue 9 66
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
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 …

730 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