// CSE 143, Winter 2011, Marty Stepp // This is the code we wrote in the first lecture. // This program implements and tests a 'stutter' method for arrays. import java.util.*; public class FirstLecture { public static void main(String[] args) { int[] numbers = { 1000, -3, 29, 42, 12 }; numbers[0] = 42; System.out.println(Arrays.toString(numbers)); int[] result = stutter(numbers); System.out.println(Arrays.toString(result)); } // Accepts an array of integers as a parameter and returns a new // array containing 2 copies of each element from the original. // For example, if the method is passed {4, 2, -1, 7}, // it returns {4, 4, 2, 2, -1, -1, 7, 7}. // Assumes that a is not null. public static int[] stutter(int[] a) { int[] b = new int[a.length * 2]; for (int i = 0; i < a.length; i++) { b[2 * i] = a[i]; b[2 * i + 1] = a[i]; } return b; } // Second solution to stutter problem ("b-based") public static int[] stutter2(int[] a) { int[] b = new int[a.length * 2]; for (int i = 0; i < b.length; i++) { b[i] = a[i / 2]; } return b; } }