Solved

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

Posted on 2004-09-24
6
268 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Title # Comments Views Activity
Eclipse Neon start with Admin account only 6 142
Controlled Assessment GCSE - desperate help needed 4 101
recursion example 16 139
sp_configure 'external scripts enabled', 1; 7 78
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
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 …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
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…

808 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