# How to return a vector from a function

• 05-17-2013, 08:02 AM
8Observer8
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.

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
• 05-17-2013, 09:29 AM
monkeyjr97
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?
• 05-17-2013, 09:44 AM
8Observer8
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:

Code:

```bool statistics(std::vector<int>& inputArr,             std::vector<int>& outOdd,             std::vector<int>& outEven);```
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; }```
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);```
• 05-17-2013, 09:54 AM
Tolls
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.
• 05-17-2013, 12:40 PM
8Observer8
Re: How to return a vector from a function
Tolls, thank you for reply but I don't understand you.
• 05-17-2013, 12:53 PM
Tolls
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.
• 05-17-2013, 02:38 PM
8Observer8
Re: How to return a vector from a function
It work! Thank You!

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();     } }```
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:
Quote:

inputArr:
5
6
3
4

outEvenArr:
2
4
6

outOddArr:
3
5