Results 1 to 7 of 7
  1. #1
    8Observer8 is offline Member
    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    Default How to return a vector from a function

    Hi

    I know that the function arguments are passed by value. I want to fill the array in the function.

    Java Code:
    Vector inputArr = null;
    inputArr.add(4);
    inputArr.add(16);
    inputArr.add(19);
    inputArr.add(31);
    inputArr.add(2);
    Vector outEvenArr = null;
    Vector outOddArr = null;
    boolean IsEvenArrGreater = Statistics.statistics(inputArr, outEvenArr, outOddArr);
    Thanks!

    Ivan

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: How to return a vector from a function

    you cant define your vector as null then try and add things to it. What are you trying to do?

  3. #3
    8Observer8 is offline Member
    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    Default Re: How to return a vector from a function

    I want to return vectors outEvenArr, outEvenArr from function Statistics.statistics()

    In C++ I could write:

    Java Code:
    bool statistics(std::vector<int>& inputArr,
                std::vector<int>& outOdd,
                std::vector<int>& outEven);
    Java Code:
    statistics(std::vector<int>& inputArr,
            std::vector<int>& outOdd,
            std::vector<int>& outEven) {
        for (unsigned int i = 0; i < inputArr.size(); i++) {
            if (inputArr[i] % 2 != 0) {
                outOdd.push_back(inputArr[i]);
            } else {
                outEven.push_back(inputArr[i]);
            }
        }
    
        bool isEvenArrGreater;
        if (outEven.size() >= outOdd.size()) {
            isEvenArrGreater = true;
        } else {
            isEvenArrGreater = false;
        }
    
        return isEvenArrGreater;
    }
    Java Code:
    std::vector<int> inputArr;
        inputArr.push_back(4);
        inputArr.push_back(16);
        inputArr.push_back(19);
        inputArr.push_back(31);
        inputArr.push_back(2);
    
        std::vector<int> outOdd;
        std::vector<int> outEven;
    
        CStatistics cStatistics;
        bool isEvenGreater = cStatistics.statistics(inputArr, outOdd, outEven);

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: How to return a vector from a function

    Instantiate the Vectors and pass them into the method.
    Have the method populate (not instantiate) them.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    8Observer8 is offline Member
    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    Default Re: How to return a vector from a function

    Tolls, thank you for reply but I don't understand you.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: How to return a vector from a function

    Currently all 3 of your Vectors (and really you should be using List/ArrayList) are null.
    Instantiate them so they are not null.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    8Observer8 is offline Member
    Join Date
    May 2013
    Posts
    4
    Rep Power
    0

    Default Re: How to return a vector from a function

    It work! Thank You!

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package acmp_0005_statistics_app;
    
    import org.me.statisticslib.Statistics;
    import java.util.Vector;
    
    /**
     *
     * @author Ivan
     */
    public class Main {
    
        public static void printArr(Vector v) {
            for (int i = 0; i < v.size(); i++) {
                System.out.println(v.elementAt(i));
            }
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            Vector inputArr = new Vector();
            inputArr.add(5);
            inputArr.add(6);
            inputArr.add(3);
            inputArr.add(4);
    
            Vector outEvenArr = new Vector();
            Vector outOddArr = new Vector();
    
            Statistics.statistics(inputArr, outEvenArr, outOddArr);
    
            System.out.println("inputArr:");
            printArr(inputArr);
            System.out.println();
    
            System.out.println("outEvenArr:");
            printArr(outEvenArr);
            System.out.println();
    
            System.out.println("outOddArr:");
            printArr(outOddArr);
            System.out.println();
        }
    }
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package org.me.statisticslib;
    
    import java.util.Vector;
    
    /**
     * Implementation of a class Statistics
     *
     * @author Ivan
     */
    public class Statistics {
        
        /**
         * Returns two arrays: even array and odd array
         * 
         * @param inputArr The input array
         * @param outEvenArr The output even array
         * @param outOddArr The output odd array
         * @return Even array is greater than odd array
         */
        public static boolean statistics(Vector inputArr,
                Vector outEvenArr, Vector outOddArr) {
            
            outEvenArr.add(2);
            outEvenArr.add(4);
            outEvenArr.add(6);
            
            outOddArr.add(3);
            outOddArr.add(5);
            
            boolean EvenArrIsGreater = true;
            return EvenArrIsGreater;
        }
    }
    Output:
    inputArr:
    5
    6
    3
    4

    outEvenArr:
    2
    4
    6

    outOddArr:
    3
    5
    Last edited by 8Observer8; 05-17-2013 at 02:42 PM.

Similar Threads

  1. function return 1 > 0 ??
    By garnachito in forum Android
    Replies: 4
    Last Post: 02-19-2013, 10:26 AM
  2. Vector+remove function
    By w95 in forum New To Java
    Replies: 3
    Last Post: 05-27-2011, 06:45 PM
  3. Return ArrayList function - Java
    By skp123 in forum New To Java
    Replies: 23
    Last Post: 05-01-2011, 04:11 AM
  4. Filling an array from the return value of the function
    By alex1988 in forum Java Applets
    Replies: 7
    Last Post: 02-02-2011, 09:29 AM
  5. Passing a Vector object to a function
    By evapisces in forum New To Java
    Replies: 4
    Last Post: 09-27-2008, 03:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •