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.

53 lines
1.3 KiB

2 years ago
  1. import java.util.Arrays;
  2. public class ArrayManipulation {
  3. public static void main(String[] args) {
  4. int[] arr = {1,2,3,4,5};
  5. System.out.println(Arrays.toString(reverseArray(arr)));
  6. System.out.println(Arrays.toString(removeFirst(arr)));
  7. System.out.println(Arrays.toString(removeLast(arr)));
  8. System.out.println(Arrays.toString(squareEach(arr)));
  9. }
  10. public static int[] reverseArray(int[] arr) {
  11. for (int i = 0; i < arr.length / 2; i++) {
  12. int tmp = arr[i];
  13. arr[i] = arr[arr.length - 1 - i];
  14. arr[arr.length - 1 - i] = tmp;
  15. }
  16. return arr;
  17. }
  18. public static int[] removeFirst(int[] arr) {
  19. int[] arr1 = new int[arr.length - 1];
  20. for (int j = 0; j < arr1.length; j++) {
  21. arr1[j] = arr[j + 1];
  22. }
  23. return arr1;
  24. }
  25. public static int[] removeLast(int[] arr) {
  26. int[] arr1 = new int[arr.length - 1];
  27. for (int j = 0; j < arr.length - 1; j++) {
  28. arr1[j] = arr[j];
  29. }
  30. return arr1;
  31. }
  32. public static int[] squareEach(int[] arr) {
  33. int[] arr1 = new int[arr.length];
  34. for (int j = 0; j < arr.length; j++) {
  35. arr1[j] = arr[j] * arr[j];
  36. }
  37. return arr1;
  38. }
  39. }