[java] How to return a specific element of an array?

I want to return odd numbers of an array yet Eclipse doesn't seem to accept my return array[i]; code. I think it requires returning a whole array since I set an array as a parameter to my method. As I said before, I need to pass an array and get a specific element of that array in return. Even if I make that array static, how do I return a single element?

Edit : Alright then, here it is:

public class newClass{
public static void main(String[] args) 
{       
    int [] newArray= new int [4];
    int [] array = {4,5,6,7};

    newArray[0] = array[0]+array[1]+array[2]+array[3];
    newArray[1] = array[0]*array[1]*array[2]*array[3];
    newArray[2] = findOut(array);

}

public static int findOut (int [] array3)
{
    int e1=0;
    int e2=0;
    for (int i=0; i<array3.length; i++)
    {
        if (array3[i]%2==0)
        {
            e1+=array3[i];
            array3[i]=e1
            return array3[i];
        }

        else
        {
            e2+=array3[i];
            array3[i]=e2;
            return array3[i];

        }

    }

}


}

I know there are probably more than a few mistakes here but I'm working on it and I'm not only returning odd numbers, I also add them together.

This question is related to java arrays parameters elements

The answer is


I want to return odd numbers of an array

If i read that correctly, you want something like this?

List<Integer> getOddNumbers(int[] integers) {
  List<Integer> oddNumbers = new ArrayList<Integer>();
  for (int i : integers)
    if (i % 2 != 0)
      oddNumbers.add(i);
  return oddNumbers;
}

Make sure return type of you method is same what you want to return. Eg: `

  public int get(int[] r)
  {
     return r[0];
  }

`

Note : return type is int, not int[], so it is able to return int.

In general, prototype can be

public Type get(Type[] array, int index)
{
    return array[index];
}

(Edited.) There are two reasons why it doesn't compile: You're missing a semi-colon at the end of this statement:

array3[i]=e1

Also the findOut method doesn't return any value if the array length is 0. Adding a return 0; at the end of the method will make it compile. I've no idea if that will make it do what you want though, as I've no idea what you want it to do.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to arrays

PHP array value passes to next row Use NSInteger as array index How do I show a message in the foreach loop? Objects are not valid as a React child. If you meant to render a collection of children, use an array instead Iterating over arrays in Python 3 Best way to "push" into C# array Sort Array of object by object field in Angular 6 Checking for duplicate strings in JavaScript array what does numpy ndarray shape do? How to round a numpy array?

Examples related to parameters

Stored procedure with default parameters AngularJS ui router passing data between states without URL C#: HttpClient with POST parameters HTTP Request in Swift with POST method In Swift how to call method with parameters on GCD main thread? How to pass parameters to maven build using pom.xml? Default Values to Stored Procedure in Oracle How do you run a .exe with parameters using vba's shell()? How to set table name in dynamic SQL query? How to pass parameters or arguments into a gradle task

Examples related to elements

comparing elements of the same array in java Android ListView with onClick items How to return a specific element of an array? Best way to get child nodes Skipping every other element after the first jquery find element by specific class when element has multiple classes Get multiple elements by Id How to get span tag inside a div in jQuery and assign a text? How to set DOM element as the first child? Number of elements in a javascript object