Solved

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

Posted on 2004-09-24
6
259 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
dividesSelf challange 15 78
changeXy challenge 13 57
drawing animated level bar based on numbers 3 72
Installed softare without GUID 6 54
Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
A short article about problems I had with the new location API and permissions in Marshmallow
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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 …

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now