Solved

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

Posted on 2004-09-24
6
267 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
  • 3
  • 2
6 Comments
 
LVL 85

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

770 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