Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int[][] splitAtFive(int[] array){
- int smallerLength = 0;
- int lagerLength = 0;
- int smallerIndex = 0;
- int largerIndex = 1;
- int SMALLER = 0;
- int LARGER = 1;
- for(int i = 0; i <array.length; i++) {
- if (array[i] < 5) {
- smallerLength++;
- } else if (array[i] >= 5) {
- lagerLength++;
- }
- }
- int[][] results = new int[2][];
- results[SMALLER] = new int[smallerLength];
- results[LARGER] = new int [lagerLength];
- for (int i = 0; i <array.length; i++){
- if(array[i] < 5){
- //Access by normal array access, so double access for a two dimensional
- results[SMALLER][smallerIndex] = array[i];
- smallerIndex++;
- }else if(array[i] >= 5){
- results[LARGER][largerIndex] = array[i];
- largerIndex++;
- }
- } return results;
- }
- ---------test--------------
- public void testSplitAt5() {
- int[][] output1 = new int[][] { {0, 1, 2, 3, 4}, {5, 6, 7} };
- int[][] output2 = new int[][] { {-3, -17}, {} };
- int[][] output3 = new int[][] { {}, {55, 2323, 102, 7} };
- int[][] output4 = new int[][] { {2, 4, 3, 1, 3}, {5, 7, 6, 9, 10, 13, 56, 43, 17, 89, 24, 37, 12, 101, 112} };
- String failMessage = "Expected method to return an array with values less than 5";
- assertArrayEquals(failMessage, output1, App.splitAtFive(intTest1));
- assertArrayEquals(failMessage, output2, App.splitAtFive(intTest2));
- assertArrayEquals(failMessage, output3, App.splitAtFive(intTest3));
- assertArrayEquals(failMessage, output4, App.splitAtFive(intTest4));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement