|
@ -1,10 +1,7 @@ |
|
|
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++) { |
|
|
for (int i = 0; i < arr.length/2; i++) { |
|
|
int tmp = arr[i]; |
|
|
int tmp = arr[i]; |
|
|
arr[i] = arr[arr.length - 1 - i]; |
|
|
arr[i] = arr[arr.length - 1 - i]; |
|
@ -14,18 +11,52 @@ public class ArrayManipulation { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
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}; |
|
|
|
|
|
|
|
|
int[] arr = {6,7,8,9,10}; |
|
|
System.out.println(Arrays.toString(reverseArray(arr))); |
|
|
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))); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |