Marc Leuschner
3 years ago
1 changed files with 51 additions and 20 deletions
@ -1,31 +1,62 @@ |
|||||
import java.util.Arrays; |
import java.util.Arrays; |
||||
|
|
||||
public class ArrayManipulation { |
public class ArrayManipulation { |
||||
|
|
||||
|
|
||||
public static int[] reverseArray(int[] arr) { |
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; |
|
||||
|
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; |
return arr; |
||||
} |
|
||||
|
} |
||||
|
|
||||
public static int[] removeFirst(int[] 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) { |
public static void main(String[] args) { |
||||
|
|
||||
int[] arr = {1,2,3,4,5}; |
|
||||
System.out.println(Arrays.toString(reverseArray(arr))); |
|
||||
|
|
||||
} |
|
||||
|
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))); |
||||
|
} |
||||
} |
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue