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.

57 lines
1.3 KiB

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