?
Solved

Passing an array between classes

Posted on 2003-03-19
5
Medium Priority
?
220 Views
Last Modified: 2010-08-05
Hi there,

Just wondering if there's anyone out there that could help me with my problem. I have declared an array in a class called "Screen". I would like to pass the array into a class called "Lead". I want to do this, as i want to be able to search the array in the Lead class. I would appreciate any help on the subject.

Thanks in advance
0
Comment
Question by:rebel_vb
[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
5 Comments
 
LVL 35

Accepted Solution

by:
girionis earned 200 total points
ID: 8168620
 There are two ways you can do it. Either in the constructor of "Lead" like (from your "Screen" - lets say it si a String array):

String [] arrayFromScreen = {"1"};
Lead lead = new Lead(arrayFromScreen);

  Now the "Lead" holds the array refeence and inside the "Lead" you could do (lets say it is a String array):

String [] array = null;

// Constructor
public Lead(String [] arrayFromScreen)
{
  this.array = arrayFromScreen;
}

  The second way is to have a setter method in your "Lead" class.

  / Constructor
  public Lead()
  {
  }

  public void setArray(String [] arrayFromScreen)
  {
    this.array = arrayFromScreen;
  }

  and you can call it from "Sceen" like:

  Lead lead = new Lead();
  lead.setArray(arrayFromScreen);
0
 
LVL 3

Assisted Solution

by:wide_awake
wide_awake earned 200 total points
ID: 8169683
Another option is to expose the array in Screen to searching by providing one or more public methods.

So within Screen, you'd have functions like

public boolean searchArray(Object needle) { ... }
public int findIndexOf(Object needle) { ... }
public Object[] getArray() { return screenArray; }

etc.  For any other search functionality you might want.  

Then, from the Lead class, you can do things like:

Screen s = new Screen();
if(s.searchArray(new Integer(3)))
{
  // screen contains 3
  int index = s.findIndexOf(new Integer(3));
}

or just get a copy of the array by using s.getArray();

-Mark.
0
 

Expert Comment

by:CleanupPing
ID: 9058594
rebel_vb:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9727268
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Split points between girionis and wide_awake

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jimmack
EE Cleanup Volunteer
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month14 days, 12 hours left to enroll

771 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