Programmierung 2 - Praktikum
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.

67 lines
1.6 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package ha01;
  2. import java.util.Arrays;
  3. public class ArrayManipulation {
  4. public static int[] reverseArray(int[] arr) {
  5. for (int i = 0; i < arr.length/2; i++) {
  6. int tmp = arr[i];
  7. arr[i] = arr[arr.length - 1 - i];
  8. arr[arr.length - 1 - i] = tmp;
  9. }
  10. return arr;
  11. }
  12. /*Liefert ein neues Array zurück ohne das erste Element*/
  13. public static int[] removeFirst(int[] arr) {
  14. int [] arrNeu = {};
  15. arrNeu = new int[arr.length-1];
  16. if(arr.length != 0) {
  17. for (int i = 1; i < arr.length; i++) {
  18. arrNeu[i-1] = arr[i];
  19. }
  20. }
  21. return arrNeu;
  22. }
  23. /*Erstellt ein neues Array ohne das letzte Element*/
  24. public static int[] removeLast(int[] arr) {
  25. int [] arrNeu = {};
  26. arrNeu = new int[arr.length-1];
  27. if(arr.length != 0) {
  28. for (int i =0; i < arr.length -1; i++) {
  29. arrNeu[i] = arr[i];
  30. }
  31. }
  32. return arrNeu;
  33. }
  34. /*Erstellt ein neues Array wo alle Zahlen quadriert werden*/
  35. public static int[] squareEach(int[] arr) {
  36. int [] arrNeu = {};
  37. arrNeu = new int[arr.length];
  38. for (int i=0; i <arr.length; i++) {
  39. arrNeu[i] = arr[i] * arr[i];
  40. }
  41. return arrNeu;
  42. }
  43. public static void main(String[] args) {
  44. int[] arr = {1,2,3,4,5};
  45. int [] arr2 = {};
  46. System.out.println(Arrays.toString(reverseArray(arr)));
  47. System.out.println(Arrays.toString(removeFirst(arr)));
  48. System.out.println(Arrays.toString(removeLast(arr)));
  49. System.out.println(Arrays.toString(squareEach(arr)));
  50. }
  51. }