Link to home
Start Free TrialLog in
Avatar of coolgem
coolgem

asked on

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

How do I initializ an array with some values in a program and then pass those values to another program.method?
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Hi coolgem,

What language are you using?

Idle_Mind
ASKER CERTIFIED SOLUTION
Avatar of KeithWatson
KeithWatson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of coolgem
coolgem

ASKER

Thank you KeithWatson. Eventually the array will be a list of "TO" addresses for emails.
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);

      }
}

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.
Avatar of coolgem

ASKER

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