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

import java.util.Arrays;
public class ArrayManipulation {
public static int[] reverseArray(int[] arr) {
for (int i = 0; i < arr.length/2; i++) {
int tmp = arr[i];
arr[i] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = tmp;
}
return arr;
}
public static int[] removeFirst(int[] arr) {
if (arr == null || arr.length==0) {
int[] result = new int[0];
return result;
}
if(arr.length == 1||arr.length == 0) {
return arr;
}
int[] result = new int[arr.length-1];
for(int a=1;a<arr.length;a++) {
result[a-1] = arr[a];
}
return result;
}
public static int[] removeLast(int[] arr) {
if (arr == null || arr.length==0) {
int[] result = new int[0];
return result;
}
if(arr.length == 1||arr.length == 0) {
return arr;
}
int[] result = new int[arr.length-1];
for(int a=0;a<arr.length-1;a++) {
result[a] = arr[a];
}
return result;
}
public static int[] squareEach(int[] arr) {
if (arr == null || arr.length==0) {
int[] result = new int[0];
return result;
}
int[] result = new int[arr.length];
for(int a=0;a<arr.length;a++) {
result[a] = arr[a] * arr[a];
}
return result;
}
public static void main(String[] args) {
int[] arr = {6,7,8,9,10};
System.out.println(Arrays.toString(reverseArray(arr)));
System.out.println(Arrays.toString(removeFirst(arr)));
System.out.println(Arrays.toString(removeLast(arr)));
System.out.println(Arrays.toString(squareEach(arr)));
}
}