You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
980 B

  1. import java.util.Arrays;
  2. public class ArrayManipulation {
  3. public static int[] reverseArray(int[] arr) {
  4. for (int i = 0; i < arr.length/2; i++) {
  5. int tmp = arr[i];
  6. arr[i] = arr[arr.length - 1 - i];
  7. arr[arr.length - 1 - i] = tmp;
  8. }
  9. return arr;
  10. }
  11. public static int[] removeLast(int[] arr){
  12. int arr2[];
  13. if(arr.length<=1) return arr2 = new int[0]; //if array only has 1 elements, give empty array
  14. arr2 = new int[arr.length-1]; //else make new array with length one less
  15. for(int i=0; i<arr.length-1; i++){
  16. arr2[i]=arr[i]; //copy all but the last elements of original array
  17. }
  18. return arr2; //return new array
  19. }
  20. public static void main(String[] args) {
  21. int[] arr = {1,2,3,4,5};
  22. System.out.println(Arrays.toString(removeLast(arr)));
  23. System.out.println(Arrays.toString(reverseArray(arr)));
  24. }
  25. }