Java array returned in method

I am taking the first steps in java, so my question is simple - I have an array with 8 integers, and I want to return an array containing elements of an odd index from the original array. what is wrong with slowing down the method? Any other implementation recommendations would be appreciated.

PS - I know that I do not need to use the method here, it's just for 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;

      }
    }


} 
+4
source share
3 answers

Code issue:

  • you cannot define a method inside another method.
  • If you want to return an array containing odd index elements from the original array. you should check index%2!=0instead of checking the array value of this index.

try it

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);
    }
 }

exit:

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

1) Java . oddIndex() main().

2) . j oddIndex()

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;

    }

}

, Jhamon, . != .

+6
  • .

  • newArr. . , . ArrayList.

  • for, = 1, +2. , 1,3,5... .

    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;
        }
    }
    

:

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

Source: https://habr.com/ru/post/1606916/


All Articles