Passing Array to a method

Hi,
There is a class MyClass that implements Serializable. I need to create array of this class and pass to a method.
In java, this can be done as following:
               MyClass[] myClassVariables=new MyClass[2];
               MyClass myClassInstance1=new MyClass();
               MyClass myClassInstance2=new MyClass();
               myClassVariables[0]=myClassInstance1;
               myClassVariables[1]=myClassInstance2;
               String message = getValues(myClassVariables);
import java.io.Serializable;
public class MyClass implements Serializable{
public String getValues(MyClass[] myClassVariables)
When I implement this in a workflow, I get the following error:
XPRESS exception:
com.waveset.util.WavesetException: Couldn't find method getValues(java.lang.String, java.lang.String) in class MyClass
java.lang.NoSuchMethodException: MyClass.getValues(java.lang.String, java.util.ArrayList)
Code I am using is as following:
<set name='myClassInstance1'>
<new class='com.marathon.MyClass'/>
</set>
<set name='myClassInstance2'>
<new class='com.marathon.MyClass'/>
</set>
<set name='myList'>
<new class='java.util.ArrayList'/>
</set>
<set name='myClassVariables'>
<list>
<ref>myClassInstance1</ref>
<ref>myClassInstance2</ref>
</list>
</set>
<set name='message'>
<invoke name='getValues' class='MyClass'>
<ref>myClassVariables</ref>
</invoke>
</set>
<ref>message</ref>
Please help me,
Thanks

You might want start by reading a few books on OO design first.
What you will probably want to do is have a class which stores the array and extends ActionListener...
public class Gui extends ActionListener {
  Person[] ppl = new Person[100];
  Component comp;
  Button addPersonButton;
  public Gui () {
    comp = new .....;
    addPersonButton = new Button("Add person");
    comp.add(addPersonButton);
    comp.addActionListener(this);
  public static void main(String args[]) {
    Gui gui = new Gui();
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == addPersonButton) {
      doAddPerson();
  pulbic void doAddPerson() {
    ppl[0] = ....
}Hope this shows you a bit more. Not sure how good my design of gui's is though :-/
Rob.

Similar Messages

  • Passing Array to Another Method

    Hello, I created a program with an array in one of the methods. I have been trying to figure out how to correctly pass the array to another method in the same class. I know my problem is in my method delcaration statements. Could someone please show me what I am doing wrong? Please let me know if you have any questions. Thanks for your help.
    import javax.swing.*;
    import java.util.*;
    class Bank1 {
         public static void main(String[] args) {
              Bank1 bank = new Bank1();
              bank.menu();
         //Main Menu that initializes other methods
         public void menu( ) {
              Scanner scanner = new Scanner(System.in);
              System.out.println("Welcome to the bank.  Please choose from the following options:");
              System.out.println("O - Open new account");
              System.out.println("T - Perform transaction on an account");
              System.out.println("Q - Quit program");
              String initial = scanner.next();
              char uInitial = initial.toUpperCase().charAt(0);
              while (uInitial != 'O' && uInitial != 'T' && uInitial != 'Q') {
                   System.out.println("That was an invalid input. Please try again.");
                   System.out.println();
                   initial = scanner.next();
                   uInitial = initial.toUpperCase().charAt(0);
              if (uInitial == 'O') newAccount();
              if (uInitial == 'T') transaction();
         //Method that creates new bank account
         public Person[] newAccount( ) {
              Person[] userData = new Person[1];
              for (int i = 0; i < userData.length; i++) {
                   Scanner scanner1 = new Scanner(System.in);
                   System.out.println("Enter your first and last name:");
                   String name = scanner1.next();
                   Scanner scanner2 = new Scanner(System.in);
                   System.out.println("Enter your address:");
                   String address = scanner2.next();
                   Scanner scanner3 = new Scanner(System.in);
                   System.out.println("Enter your telephone number:");
                   int telephone = scanner3.nextInt();
                   Scanner scanner4 = new Scanner(System.in);
                   System.out.println("Enter an initial balance:");
                   int balance = scanner4.nextInt();
                   int account = i + 578;
                   userData[i] = new Person( );
                   userData.setName               ( name );
                   userData[i].setAddress          ( address );
                   userData[i].setTelephone     ( telephone );
                   userData[i].setBalance          ( balance     );
                   userData[i].setAccount          ( account     );
                   System.out.println();
                   System.out.println("Your bank account number is: " + userData[i].getAccount());
              return userData;
              menu();
         //Method that gives transaction options
         public void transaction(Person userData[] ) {
              System.out.println(userData[0].getBalance());

    Thank you jverd, I was able to get that to work for me.
    I have another basic question about arrarys now. In all of the arrary examples I have seen, the array is populated all at once using a for statement like in my program.
    userData = new Person[50];
    for (int i = 0; i < userData.length; i++) In my program though, I want it to only fill the first array parameter and then go up to the main menu. If the user chooses to add another account, the next spot in the array will be used. Can someone point me in the right direction for doing this?

  • Passing array values to methods & Classes

    Hi,
    I have written the following two classes, I want to pass the value from one class - insersortest to the other class what kind of return statement do I do ? I am not sure what does this Comparable object do in this program ? I am trying the program (algorithm specfieid) from one of the Data Structure book.
    import java.io.*;
    import java.lang.*;
    public class Insersortest
    public static void main(String args[]) throws IOException
    Insersort ghl = new Insersort();
              Comparable a[] = {1, 3, 5, 9, 1};          
                   System.out.println("Detecting duplicates"+ ghl.insertA(a));               
    -=-=-=-
    class Insersort
    public static int insertA( Comparable [ ] a )
    for( int p = 1; p < a.length; p++ )
    Comparable tmp = a[ p ];
    int j = p;
    for( ; j > 0 && tmp.compareTo( a[ j - 1 ] ) < 0; j-- )
    a[ j ] = a[ j - 1 ];
    a[ j ] = tmp;
    Could somebody provide their view please.
    PK

    You return arrays just like any object:
    public Object[] getArray()Comparable is an interface. Any object that implements Comparable (and can therefore be stored in a Comparable's reference) is guarenteed to have a compareTo method.
    Does that help?

  • Passing array to a method through actionevent? ??

    what the hell is this guy on about your asking..
    well.. i have a user defined data structures (people)
    and i'm creating my first gui, so i have an action caught with
    public static void main(String[] args)
    int MAX = 1000;
    person[] ppl;
    ppl = new person[MAX];
    public void actionPerformed(ActionEvent e) {
    if(e.getSource() == addNewPerson) {
    //method call in here to add new person
    //but how can i pass the array this far so i can then give it
    //to my method, or am i looking at this all wrong?
    }if as the comments suggests, i'm calling this all wrong, what way should i implement this? i'm basically getting cannot resolve symbol, pointing to my method which adds a new person (so as i said before, it needs to be passed to the method)

    You might want start by reading a few books on OO design first.
    What you will probably want to do is have a class which stores the array and extends ActionListener...
    public class Gui extends ActionListener {
      Person[] ppl = new Person[100];
      Component comp;
      Button addPersonButton;
      public Gui () {
        comp = new .....;
        addPersonButton = new Button("Add person");
        comp.add(addPersonButton);
        comp.addActionListener(this);
      public static void main(String args[]) {
        Gui gui = new Gui();
      public void actionPerformed(ActionEvent e) {
        if (e.getSource() == addPersonButton) {
          doAddPerson();
      pulbic void doAddPerson() {
        ppl[0] = ....
    }Hope this shows you a bit more. Not sure how good my design of gui's is though :-/
    Rob.

  • How can i pass array as argument in magento api method calling using sudzc API in iphone Native APP

    0down votefavorite
    I am implementing magento standard api method in native iphone app. I use webservice generated by sudzc. To call method I use:
    [service call:self action:@selector(callHandler:) sessionId:@" " resourcePath:@" " args:@""];
    When I used methods e.g. cart.info or product.info in which I have to pass one parameter; it gives valid response. But when I used method e.g. cart_product.add in which I have to pass an argument as value of array it gives error
    SQLSTATE[42000]: Syntax error or access violation:
        1064 You have an error in your SQL syntax;
        check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
    I have tested in SOAP UI also. There also it gives me same error. But same method works in PHP file. Even I tried by creating same soap request which I created using JavaScript but still it doesn't work.
    Can anyone help me to pass array as a parameter using sudzc api to call magento web services?

    There is an error in your SQL.

  • Passing array from JS to method of applet

    There is unable in IE pass array of values from javascript to method of applet, but in Mozilla it's working fine. In IE i have got exception:
    Exception:
    java.lang.Exception: setTest{0} :no such method exists
         at sun.plugin.com.JavaClass.getMethod1(Unknown Source)
         at sun.plugin.com.JavaClass.getDispatcher(Unknown Source)
         at sun.plugin.com.DispatchImpl.invokeImpl(Unknown Source)
         at sun.plugin.com.DispatchImpl$1.run(Unknown Source)
         at java.security.AccessController.doPrivileged(Native Method)
         at sun.plugin.com.DispatchImpl.invoke(Unknown Source)
    java.lang.Exception: java.lang.Exception: setTest{0} :no such method exists
         at sun.plugin.com.DispatchImpl.invokeImpl(Unknown Source)
         at sun.plugin.com.DispatchImpl$1.run(Unknown Source)
         at java.security.AccessController.doPrivileged(Native Method)
         at sun.plugin.com.DispatchImpl.invoke(Unknown Source)
    JavaScript code:
    var testArr=[];
        testArr[0]=1;
        testArr[1]=2;
        testArr[2]=3;
        document.sync.setTest(testArr);
    Applet method:
    public void setTest(Object[] test){
            System.out.println("Test "+test.length);
            for (Object o: test){
                System.out.println(o.toString());
        }How do passing in IE?

    yes, MAYSCRIPT just allow to call methods. but as i know it's unable to pass simply array from js to applet and from applet to js. so i convert array of values to String and in applet i use StringTokenizer to parsing. Thanks. ;)

  • Passing hard coded array to a method

    how to passing hard coded array to a method
    like method1("eee","eoe","eee",.....)
    which should populate a array of strings (say arr[]) for the method
    what can be done??

    or if you're using Java 5.0 declare the method aspublic void method(String... array) {
    }

  • Passing a whole array through a method

    I need some quick help on how to pass a whole array through the method addCoins(). Here is the code:
    public class Purse {
         private Coin[] coins;
         private int lastIdx;
    public Purse() {
         coins = new Coin[100];
         lastIdx = 0;
    public boolean addCoin(Coin b) {
         if (lastIdx < coins.length) {
              lastIdx++;
              coins[lastIdx] = b;
              return true;
         else {
              return false;
    public boolean addCoins(Coin[] b) {
         if (lastIdx < coins.length) {
              for (b=0; b<99; b++) {
                   coins[b] = b;
         else {
              return false;
    }I figured passing the whole array would be "Coin[] b" but apparently Eclipse says that it can't convert from int to Coin[]. I have no idea what that means. Thanks in advance.

    I need to create 3 classes, Coin, Purse, and TestPurse.
    Coin code:
    public class Coin {
         private String typeOfCoin;
         private double value;
    public Coin(String t, double v) {
         typeOfCoin = t;
         value = v;
    public String toString() {
         return typeOfCoin + value;
    }Purse code:
    public class Purse {
         private Coin[] coins;
         private int lastIdx;
    public Purse() {
         coins = new Coin[100];
         lastIdx = 0;
    public boolean addCoin(Coin b) {
         if (lastIdx < coins.length) {
              lastIdx++;
              coins[lastIdx] = b;
              return true;
         else {
              return false;
    public boolean addCoins(Coin[] b) {
          if (lastIdx < coins.length) {
              for (int i=0; i<coins.length; i++) {
                   coins[i] = b;
         else {
              return false;
    public Coin getReverse() {
         coins = new Coin[0];
         lastIdx = 100;
    }In the Purse class, I need methods addCoin and addCoins that need to return boolean.  addCoin is supposed to add the argument coin as an element of the coins array if there is space.  addCoins neds to add all the coins in the input argument as elements of the coins array if there is space.  I then have to create two methods, getReverse and getValue that take no argument and getReverse that returns an array of type Coin while getValue returns a double.  In getReverse, I create an array simlar to coins but this new array is reverse.  I started on this method but I first need to worry about the addCoin and addCoins method.  For the record, getValue needs to add up the values of all the coins in the purse and return it, which I have no idea how to do that.  That's my next question.
    Edited by: quagmire87 on Dec 1, 2009 4:35 PM
    fixed method addCoins                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • How do I pass an array to a method?

    Hopefully you guys don't mind answering one of my stupid questions :o(
    I have problem passing an array to a method.
    //Here is the code that calls to the method:
    String[] Folder = new String[19];
    int cnt;
    for(cnt=0; cnt<20; cnt++){
    Folder[cnt] = request.getParameter("searchfolder_name" + (cnt + 1));
    ProcessNotify(Folder[cnt]);
    //================================================================
    //Here the the method that is supposed to take in the array
    private void ProcessNotify(String FolderName[]){
    FolderName = new String[19];
    int cnt;
    for(cnt=0; cnt<20; cnt++){
    System.out.println("Folder Name: " + FolderName[cnt]);
    I got an error that says java.lang.String[] can not be applied to java.lang.String
    I thought "String FolderName[]" is an array? Anyway... I'm confused. If you know the answer, plz let me know :o) big thx
    lil

    ProcessNotify(Folder[cnt]);
    Are you trying to pass the whole array or just one piece from it? What you coded, tried to pass just one piece by specifying a subscript. BTW variable cnt is a subscript out of range.
    If you meant to pass the whole array, just use it's name with no subscript.

  • Passing arrays to methods?

    Hi all,
    Thanks to most of the main contributors in this forum nearly all of my major hurdles in transitioning from proc-C to Cocoa have been overcome. However, while I was shown how to use NSMutableArray in conjunction with NSDictionary to do pretty much the same job as a C-style multi-dimensional array, dictionaries aren't always the perfect solution in every situation. So, let's suppose I wanted to construct a pure Cocoa facsimile of a C-style [4][x] array like this...
    NSArray *replicaArray = [NSArray arrayWithObjects:
    [NSNull null], // index 0 (unused)
    [NSMutableArray array], // index 1
    [NSMutableArray array], // index 2
    [NSMutableArray array],nil]; // index 3
    I threw in an NSNull as the first array object because (whenever possible) I adopted the habit of leaving row0 and column0 of multidimensional arrays unused to make for more natural, readable iteration ops and stuff. The above code works perfectly well, but with 4-5 different arrays in the same proc-C apps I often had to pass them to common functions (to handle things like bubble-sorting etc.) like the one below:
    void bubbleSortSimpleArray (short arry[ ], short numsToSort)
    short ctr,loopCtr,storeValue;
    for (loopCtr = 1; loopCtr <= numsToSort; loopCtr++)
    ctr = 1;
    while (ctr < numsToSort)
    if (arry[ctr] > arry[ctr+1])
    storeValue = arry[ctr+1];
    arry[ctr+1] = arry[ctr];
    arry[ctr] = storeValue;
    ++ctr;
    Of course, thanks to the advantages of Cocoa this entire code block can be replaced in just a couple lines with 'sortUsingSelector'. Sorry for the lengthy outline in attempting to describe my problem as clearly as possible , but (finally) _is it possible to pass arrays to methods_ in our Cocoa code in a similar way? I was thinking something along the lines of:
    - (void)doMoreProcessing:(id)array
    //do some other common stuff on my array(s) here...
    That'd simplify converting some of my old code and be a real bonus for me, but I haven't been able to find any reference about this on the net so far, and experimenting with my code hasn't provided me with any answers yet, either... Any guidance on this would be very much appreciated!
    Thanks in advance.
    Ernie

    Hi Bang A. Lore,
    Thanks for your input but I've got it figured out now However, while you quite correctly say:
    Pointers work the same way as they do in C.
    the cause of my problems wasn't a lack of understanding of pointers, but by the syntax differences between proc-C and obj-C. I'm well aware of the fact that the name of a passed array is just a pointer to its address in memory of course, but attempting to use a mandatory 'C' style header declaration along the lines of:
    - (void)test2DArray:(NSMutableArray*)array[][];
    just kept giving me parse errors, as did any attempt to use square brackets in the implementation file. While 'C' insists on using them, I found (after some more experimentation) that obj-C will quite happily accept:
    - (void)test2DArray:(NSMutableArray*)array;
    ...or the even simpler...
    - (void)test2DArray:(id)array;
    Also pleasantly surprised to find that Cocoa doesn't seem to give a hoot whether the (array) object you pass it is flat OR multidimensional -- whereas 'C' insists you even provide the called function with the number of rows contained in a multi-subscripted array -- which often meant I had to include two versions of many functions containing identical code, depending on whether they were called by 1D or 2D arrays.
    Once I hit on the idea of omitting the square brackets entirely (just to stop the darned parse errors and see what would happen out of curiosity), the problem just went away.
    Thanks once again. Appreciate it!

  • Getting Type Mismatch Error while passing Array of Interfaces from C#(VSTO) to VBA through IDispatch interface

    Hi,
    I am facing issues like Type Mismatch while passing Array of interfaces from .NET  to VBA and vice versa using VSTO technology.
    My requirement is that ComInterfaceType needs to be InterfaceIsIDispatch.
    My Interface definition is somewhat like this
        [ComVisible(true)]
        [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
        [Guid("AAF48FBC-52B6-4179-A8D2-944D7FBF264E")]
        public interface IInterface1
            [DispId(0)]
            IInterface2[] GetObj();
            [DispId(1)]
            void SetObj(ref IInterface2[] obj);
        [ComVisible(true)]
        [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
        [Guid("CDC06E1D-FE8C-477E-97F1-604B73EF868F")]
        public interface IInterface2
    IF i am passing array of above interface (created in C#.Net) to VBA using GetObj API,i am getting type mismatch error in VBA while assigning the value to variable in VBA.Even assigning to variant type variable gives TypeMismatch.
    Also while passing Array of interfaces from VBA using SetObj API,excel crashes and sometimes it says method doesn't exists.
    Kindly provide some assistance regarding the same.
    Thanks

    Hi,
    I am facing issues like Type Mismatch while passing Array of interfaces from .NET  to VBA and vice versa using VSTO technology.
    My requirement is that ComInterfaceType needs to be InterfaceIsIDispatch.
    My Interface definition is somewhat like this
        [ComVisible(true)]
        [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
        [Guid("AAF48FBC-52B6-4179-A8D2-944D7FBF264E")]
        public interface IInterface1
            [DispId(0)]
            IInterface2[] GetObj();
            [DispId(1)]
            void SetObj(ref IInterface2[] obj);
        [ComVisible(true)]
        [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
        [Guid("CDC06E1D-FE8C-477E-97F1-604B73EF868F")]
        public interface IInterface2
    IF i am passing array of above interface (created in C#.Net) to VBA using GetObj API,i am getting type mismatch error in VBA while assigning the value to variable in VBA.Even assigning to variant type variable gives TypeMismatch.
    Also while passing Array of interfaces from VBA using SetObj API,excel crashes and sometimes it says method doesn't exists.
    Kindly provide some assistance regarding the same.
    Thanks

  • Passing parameters to a method

    I am passing objects to another method.
    I do some changes to them and expect the changed objects to be available in the calling method ( since they are supposedely passed by reference) when the execution is returned to it. However, I don't get the changed objects but rather the old ones, before the method execution.
    Can someone explain?
    Thanks,
    Boris.

    I do some changes to them and expect the changed
    objects to be available in the calling methodIf you modify an object passed to a method, then the changes made to that object will be visible as soon as they are made.
    If you change the parameter so that it references some other object, this will NOT be seen in the calling method.
    You can "fake" it by putting the object in question in an array and passing the array. Better though is to just not expect methods to be able to change references they are passed to point at some other object.

  • Filling empty java array in C method

    Hi everyone,
    I'm passing to a C method an empty byte array from the Java Side. I want the C method to fill the Java byte array and return it at the end of the method to the java side.
    in the Java side, I have my byte array :
    byte[] myArray = null;
    then I'm passing the array to the C method as
    myMethod(myArray)
    in the C method I'M trying to access the array as follow :
    myMethod(jbyteArray myArray)
    jbyte* tab = *env)->GetByteArrayElements(env, myArray, 0);
    I'm getting an error at runtime. Can somebody help me with that issue please ? Thanks
    Sebastien

    You are not passing a byte array, you are passing null.
    If you want to pass a byte array you first have to create it using the new operator:
    byte[] myArray = new byte[theSizeOfTheArray];
    myMethod(myArray);If you want the JNI code to create the array, that is fine also, but in that case the native method would probably have to return the array instead of void.

  • Pass array on to another class

    Hi. Simple question here.
    I have two classes and in one I have an array. I want to pass it on to the other class so I can use the values and stuff there. How do I do that?

    You don't pass things to classes. You pass them to constructors or methods of a class or instance of one.
    If you do this, for example:
    System.out.println("Hello world");
    you are passing a String argument to the println method.
    You can similarly pass your array to a method of some class, if that method accepts an array as a parameter.
    P.S. You're welcome. Obviously you didn't get hit by a bus and couldn't reply with a simple "thank you", as you posted another topic a day or so later than this. But, you're welcome.

  • Passing Arrays to a mathod

    I was wondering how I would be able to pass 2 arrays to 1 method?
    I have array a and array b, which I need to pass to a method that will add them together and return the result in array c. but I just need to know how to pass the two arrays into the method for now.

    return the result in array c
    int[] someMethod(int[] arr1, int[] arr2) {                                                                                                                                                                                   

Maybe you are looking for

  • Mail Quits (Crashes) Before It Even Opens

    After the most recent Software Update, I cannot open Mail. Each time I try, it crashes as I attempt to open it, asking for permission to send a report to Apple (which I do). I am loathe to turn to Thunderbird or worse, Entourage, as I hope this will

  • Deinstallation of Oracle E-business suite with demo vision demo on linux

    Hi all, I installed oracle ebs 12.1.1 on my server with demo vision database. Now I want to deinstall the ebs i previously installed. What is the procedure for the deinstallation? Thanks in advance.. Regards, Mike

  • N97, a great phone.

    I got to say this, i have started to like my N97 more and more. This weekend i was on a fishing trip with a few friends and they all used android phones. After just one day i was the only one with a working phone although i used maps (love the latest

  • Fixed Column changing Row

    Hi, I'm fairly sure there is no formula for what I would like to achieve ?? Is there a Macro? that can be used.?? I have a spreadsheet that I would like to be able to recognise a variable cell reference. I.E. the formulas will look similar to this wh

  • Help with attaching a downloadable file

    I am building my website and I need to add a price list that people can download. I am sure that this is really simple, I just have no idea how to do it. Cany anyone help? Thanks!