3 Array Solutions
3.1 Print Array
Write a method that prints out a string array, one element per line
3.1.1 Solution
public class NNArrays { NNArrays() { String [] breakfast = {"Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"}; printArray(breakfast); //Alternative Method printArrayAlt(breakfast); } void printArray(String [] array) { for(int i = 0; i<array.length; i++) { System.out.println(array[i]); } } //Alternative method using for each loop void printArrayAlt(String [] array) { for(String s: array) { System.out.println(s); } } public static void main(String[] args) { new NNArrays(); } }
3.2 Last Element of Array
Write a method that returns the last element of a string array
3.2.1 Solution
public class NNArrays { NNArrays() { String [] breakfast = {"Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"}; System.out.println(lastElement(breakfast)); } String lastElement(String [] array) { return array[array.length-1]; } public static void main(String[] args) { new NNArrays(); } }
3.3 Last But One Element of Array
Write a method that returns the last but one element of a string array
3.3.1 Solution
public class NNArrays { NNArrays() { String [] breakfast = {"Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"}; //Added two extra arrays for testing String [] coin = {"Heads", "Tails"}; String [] lonely = {"solo"}; System.out.println(lastButOne(breakfast)); System.out.println(lastButOneSafer(breakfast)); System.out.println(lastButOneSafer(coin)); System.out.println(lastButOneSafer(lonely)); } String lastButOne(String [] array) { return array[array.length-2]; } // The previous method will throw an // arrayIndexOutOfBoundsException for arrays of length <2. // This method will return an empty string if length <2 String lastButOneSafer(String [] array) { if (array.length<2) { return ""; } else { return array[array.length-2]; } } public static void main(String[] args) { new NNArrays(); } }
3.4 Reverse an Array
Write a method that returns elements of an Array in reverse order
3.4.1 Solution
public class NNArrays { NNArrays() { String [] breakfast = {"Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"}; //Added two extra arrays for testing String [] coin = {"Heads", "Tails"}; String [] lonely = {"solo"}; printArray(reverse(breakfast)); printArray(reverse(coin)); printArray(reverse(lonely)); } void printArray(String [] array) { for(int i = 0; i<array.length; i++) { System.out.println(array[i]); } } String [] reverse(String [] array) { for(int i = 0; i<array.length/2;i++) { String temp = array[i]; array[i] = array[array.length-i-1]; array[array.length-i-1] = temp; } return array; } public static void main(String[] args) { new NNArrays(); } }
3.5 Palindromic Arrays
Write a method that tests to see if an array is palindromic, i.e. the elements are the same when reversed.
3.5.1 Solution
public class NNArrays { NNArrays() { String [] breakfast = {"Sausage", "Eggs", "Beans", "Bacon", "Tomatoes", "Mushrooms"}; String [] lonely = {"solo"}; String [] palindromic = {"Sausage", "Eggs", "Beans", "Beans", "Eggs", "Sausage"}; System.out.println(isPalindrome(palindromic)); System.out.println(isPalindrome(breakfast)); System.out.println(isPalindrome(lonely)); } boolean isPalindrome(String [] array) { boolean isPal = true; for (int i = 0; i<array.length/2; i++) { if (!array[i].equals(array[array.length -i -1])) isPal = false; } return isPal; } // The following method doesn't work. // Need to compare individual elements boolean isPalindromeWrong(String [] array) { if (array.equals(reverse(array))) { return true; } else { return false; } } String [] reverse(String [] array) { for(int i = 0; i<array.length/2;i++) { String temp = array[i]; array[i] = array[array.length-i-1]; array[array.length-i-1] = temp; } return array; } public static void main(String[] args) { new NNArrays(); } }
3.6 Consecutive Duplicates
Write a method to print out an int array with consecutive duplicates eliminated
3.6.1 Solution
public class NNArrays { NNArrays() { int [] nums = {1,1,3,3,3,2,2,2,1,1,1,1,4,4,4,4}; int [] num2 = {1,1}; int [] num1 = {1}; compress(nums); compress(num2); compress(num1); } void compress(int [] array) { System.out.println(array[0]); for (int i = 1; i<array.length; i++) { if (array[i]!=array[i-1]) { System.out.println(array[i]); } } } public static void main(String[] args) { new NNArrays(); } }
3.7 Pack Duplicates
Pack consecutive duplicates of a char array into Strings
3.7.1 Solution
public class NNArrays { NNArrays() { char [] letters = {'a', 'a', 'a', 'a', 'b', 'c', 'c', 'a', 'a', 'd', 'e', 'e', 'e', 'e'}; pack(letters); } void pack(char [] array) { String s = Character.toString(array[0]); for(int i = 1; i<array.length; i++) { if(array[i] != array [i-1]) { System.out.print(s+ ", "); s = Character.toString(array[i]); } else { s = s + array[i]; } } System.out.println(s); } public static void main(String[] args) { new NNArrays(); } }