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.

46 lines
1.0 KiB

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