Hausaufgabe Programmieren2
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.

62 lines
1.5 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  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[] removeFirst(int[] arr) {
  12. if (arr == null || arr.length==0) {
  13. int[] result = new int[0];
  14. return result;
  15. }
  16. if(arr.length == 1||arr.length == 0) {
  17. return arr;
  18. }
  19. int[] result = new int[arr.length-1];
  20. for(int a=1;a<arr.length;a++) {
  21. result[a-1] = arr[a];
  22. }
  23. return result;
  24. }
  25. public static int[] removeLast(int[] arr) {
  26. if (arr == null || arr.length==0) {
  27. int[] result = new int[0];
  28. return result;
  29. }
  30. if(arr.length == 1||arr.length == 0) {
  31. return arr;
  32. }
  33. int[] result = new int[arr.length-1];
  34. for(int a=0;a<arr.length-1;a++) {
  35. result[a] = arr[a];
  36. }
  37. return result;
  38. }
  39. public static int[] squareEach(int[] arr) {
  40. if (arr == null || arr.length==0) {
  41. int[] result = new int[0];
  42. return result;
  43. }
  44. int[] result = new int[arr.length];
  45. for(int a=0;a<arr.length;a++) {
  46. result[a] = arr[a] * arr[a];
  47. }
  48. return result;
  49. }
  50. public static void main(String[] args) {
  51. int[] arr = {6,7,8,9,10};
  52. System.out.println(Arrays.toString(reverseArray(arr)));
  53. System.out.println(Arrays.toString(removeFirst(arr)));
  54. System.out.println(Arrays.toString(removeLast(arr)));
  55. System.out.println(Arrays.toString(squareEach(arr)));
  56. }
  57. }