# Thread: How to return a vector from a function

1. Member
Join Date
May 2013
Posts
4
Rep Power
0

## 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;
Vector outEvenArr = null;
Vector outOddArr = null;
boolean IsEvenArrGreater = Statistics.statistics(inputArr, outEvenArr, outOddArr);```
Thanks!

Ivan

2. Senior Member
Join Date
Nov 2012
Posts
258
Rep Power
2

## 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. Member
Join Date
May 2013
Posts
4
Rep Power
0

## 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. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18

## 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.

5. Member
Join Date
May 2013
Posts
4
Rep Power
0

## Re: How to return a vector from a function

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

6. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18

## 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.

7. Member
Join Date
May 2013
Posts
4
Rep Power
0

## 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();

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) {

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 03:42 PM.

#### Posting Permissions

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