Solved

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

Posted on 2004-09-24
6
269 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 250 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
Industry Leaders: 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!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

A short article about problems I had with the new location API and permissions in Marshmallow
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

733 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