?
Solved

Passing an array between classes

Posted on 2003-03-19
5
Medium Priority
?
226 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
4 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

Hire Technology Freelancers with Gigs

Work with 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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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.
Suggested Courses
Course of the Month12 days, 19 hours left to enroll

580 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