?
Solved

How to initialize an array and then pass the array to another class.method

Posted on 2004-09-24
6
Medium Priority
?
271 Views
Last Modified: 2010-04-17
How do I initializ an array with some values in a program and then pass those values to another program.method?
0
Comment
Question by:coolgem
[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
  • 3
  • 2
6 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12148389
Hi coolgem,

What language are you using?

Idle_Mind
0
 
LVL 3

Accepted Solution

by:
KeithWatson earned 1000 total points
ID: 12151167
Hi Coolgem,

Not sure if you mean that you want to pass arrays between methods in a single program or if you want to have multiple programs running in different JVMs communicating; however, assuming the former, this is how you would do it:

public class Test {

      public void example(String[] myArray) {
            
            for (int i=0; i<myArray.length; i++) {
                  System.out.println(myArray[i]);
            }

      }
      
      public static void main(String[] args) throws Exception {

            String[] myArray = new String[3];
            myArray[0] = "Keith";
            myArray[1] = "Was";
            myArray[2] = "Here";
            
            // Alternative Terser Syntax: String[] myArray = new String[] {"Keith", "Was", "Here"};
            
            Test test = new Test();
            test.example(myArray);
          
      }
}
0
 

Author Comment

by:coolgem
ID: 12160055
Thank you KeithWatson. Eventually the array will be a list of "TO" addresses for emails.
0
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.

 
LVL 3

Expert Comment

by:KeithWatson
ID: 12164776
Hi Coolgem,

Just a small point, but it might be worth mentioning that Java provides and number of classes that are perhaps more flexible than arrays to hold collections of items. The simplest benefit is that you don't have to know the size of a collection at compile time; whereas, with arrays, you must declare them with an explicit size.

Here is the above code written using a LinkedList class. The collections classes are well worth having a look into, since they can save you a lot of hassle. In your case, I assume you most likely won't know how many email "to" addresses you will need to store at compile time.

Thanks for the feedback by the way!

Keith.


package com.test;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class Test {

      public void example(List myList) {

            Iterator i = myList.iterator();
            
            while (i.hasNext()) {
                  System.out.println( (String) i.next());
            }
            
      }

      public static void main(String[] args) throws Exception {

            List myList = new LinkedList();
            
            myList.add("Keith");
            myList.add("Was");
            myList.add("Here");
            
            Test test = new Test();
            test.example(myList);

      }
}

0
 
LVL 3

Expert Comment

by:KeithWatson
ID: 12164941
Oops, I said at compile time. I meant at runtime, but once you've set the size of an array, you can't change it.
0
 

Author Comment

by:coolgem
ID: 12173710
Hi KeithWatson,
Thank you for the LinKedList idea.  I found some code that I can emulate and I will do my SQL and place the results into a Vector object.  Th e reason I would use a Vector which has no size limitations.
coolgem
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
Make the most of your online learning experience.
Progress
Introduction to Processes
Suggested Courses
Course of the Month7 days, 23 hours left to enroll

765 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