6

I'm doing my first steps in java, so my question is simple - I have an array with 8 integers and I want to return an array that contains the odd index elements from the original array. what's wrong with the method deceleration? Any other implementation tips would be appreciated.

P.S - I know that I don't have to use method here, it's just for the exercise.

package com.tau;

public class Main {


    public static void main(String[] args) {

        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8};


        System.out.println("1.e odd index numbers in array : " + oddIndex(arr));

        int j = 0;
        public static int[] oddIndex(int[] array){
            int newArrSize = array.length;
            if ((newArrSize % 2) != 0) {
                newArrSize--;
            }

            int[] newArr = new int[newArrSize];

            for (int i = 0; i < array.length; i++)
                if ((array[i] % 2) == 0) {
                    newArr[j] = array[i];
                    j++;
                }
            return newArr;

      }
    }


} 
5
  • 7
    You can't declare a method inside of another method. Commented Sep 11, 2015 at 7:36
  • 1
    this method will return the odd elements, not the odd index elements Commented Sep 11, 2015 at 7:40
  • 1
    If you return the odd indexed elements, you will have around half the number of elements. Commented Sep 11, 2015 at 7:44
  • 1
    To look at every second element, do i += 2 Commented Sep 11, 2015 at 7:45
  • 1
    Confusingly your odd indexed elements have even numbers. i.e. arr[1] == 2 Commented Sep 11, 2015 at 7:45

3 Answers 3

8

1)You cannot have methods inside method in Java. Move your oddIndex() methods outside main() method.

2) And you cannot local variables of a method in another method. So I moved your variable j to oddIndex() method

public class Main {

    public static void main(String[] args) {

        int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8 };

        System.out.println("1.e odd index numbers in array : " + oddIndex(arr));

    }

    public static int[] oddIndex(int[] array) {
        int j = 0;
        int newArrSize = array.length;
        if ((newArrSize % 2) != 0) {
            newArrSize--;
        }

        int[] newArr = new int[newArrSize];

        for (int i = 0; i < array.length; i++)
            if ((array[i] % 2) == 0) {
                newArr[j] = array[i];
                j++;
            }
        return newArr;

    }

}

And also, as Jhamon commented, your method name and logic inside are not matched. Odd index != odd value.

Sign up to request clarification or add additional context in comments.

Comments

3

Issue with your code:

  • you can not define method inside another method.
  • If you want to return an array that contains the odd index elements from the original array. you should check index%2!=0 instead of checking array value of that index.

try this

public class Main {
      public static void main(String[] args) {
         int[] arr = {1, 2, 3, 4, 5, 6, 7, 8};
         System.out.println("1.e odd index numbers in array : " +  Arrays.toString(oddIndex(arr)));

    }



     public static int[] oddIndex(int[] array){

         int[] newArr = new int[array.length];
         int j=0;
         for (int i = 0; i < array.length; i++){
             if ((i % 2) != 0) {
                 newArr[j++] = array[i];

             }
         }
         return Arrays.copyOf(newArr, j);
    }
 }

output:

1.e odd index numbers in array : [2, 4, 6, 8] // odd index elements from original array

4 Comments

Why do you prefer returning Arrays.copyOf(newArr, j) and printing Arrays.toString(oddIndex(arr)) rather than returning newArr and printing oddIndex(arr), any special reason ?
it's because the size of newArr is same as size of original array. so simply trimming the size.
you can optimize the newArr as int[] newArr = new int[array.length/2]; and then simple return newArr. Since the size of odd index elements will be half of size of original array.
Arrays.toString(oddIndex(arr) is used to print the elements as string otherwise printing arr will print only reference address of arr.
0
  1. You can't write a method within a method.

  2. You are creating a newArr named array. Size of this array is given you. If you make the array dynamic then the memory usage will be less. For dynamic array you have to use ArrayList.

  3. For checking the odd index directly give the value in the for loop that i=1 and then increment +2. So then the index will be 1,3,5... You don't have to do that much calculation for this.

    import java.util.ArrayList;
    
    public class ReturnArray 
    {
        public static void main(String[] args) 
        {
            int[] arr = {1, 2, 3, 4, 5, 6, 7, 8};
            System.out.println("Odd index numbers in array : " + oddIndex(arr));            
        }
    
        public static ArrayList<Integer> oddIndex(int[] array)
        {
            ArrayList<Integer> al = new ArrayList<Integer>();
            for (int i = 1; i < array.length; i+=2)
                al.add(array[i]);
            return al;
        }
    }
    

Output :

Odd index numbers in array : [2, 4, 6, 8]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.