Extract subarray of elements that appear after the last occurrence of "4" I am doing some challenges, and in one challenge given a non-empty array of int, I am to return a new array containing the elements from the original array that come after the last 4 in the original array. The original array will contain at least one 4.

E.g:


  post4([2, 4, 1, 2]) → [1, 2]
  post4([4, 1, 4, 2]) → [2]
  post4([4, 4, 1, 2, 3]) → [1, 2, 3]


As such, I have written code that works but it is far too complicated and unprofessional, at least it feels this way.

public static int[] post4(int[] nums) {

    int startPoint = nums.length - 1;
    int[] newArray;
    int amtOfElements = 0;
    int stopIndex = 0;

    for (int i = startPoint; i &gt;= 0; i--) {

        if (nums[i] == 4) {
            stopIndex = i;
            i = 0;
        } else {
            amtOfElements++;
        }
    }

    newArray = new int[amtOfElements];

    for (int i = 0, j = stopIndex; i &lt; amtOfElements; i++, j++) {
        newArray[i] = nums[j + 1];
    }

    return newArray;
}


As such, this looks nasty to me.
How can I shorten this to a single for-loop?
I would use this:

public int[] post4(int[] arr) {
  for(int i = arr.length-1; i &gt;= 0; --i) {
    if(arr[i] == 4) {
      return Arrays.copyOfRange(arr, i+1, arr.length);
    }
  }
  return new int[0]; // or null
}