Quiz arrays ch7
Which of the following choices is the correct syntax for quickly declaring/initializing an array of six integers to store a particular list of values?
int[] a = {17, -3, 42, 5, 9, 28};
Write a method called median that accepts an array of integers as its argument and returns the median of the numbers in the array. The median is the number that will appear in the middle if you arrange the elements in order. Assume that the array is of odd size (so that one sole element constitutes the median) and that the numbers in the array are between 0 and 99 inclusive. For example, the median of {5, 2, 4, 17, 55, 4, 3, 26, 18, 2, 17} is 5, and the median of {42, 37, 1, 97, 1, 2, 7, 42, 3, 25, 89, 15, 10, 29, 27} is 25.
public static int median(int[] d) { Arrays.sort(d); return d[d.length/2]; }
Which of the following choices is the correct syntax for declaring/initializing an array of ten integers?
int[] a = new int[10];
Write a piece of code that declares an array variable named data with the elements 7, -1, 13, 24, and 6. Use only one statement to initialize the array. statement
int [] data= {7, -1, 13, 24,6};
The following program produces 4 lines of output. Write each line of output below as it would appear on the console. import java.util.*; // for Arrays class public class ReferenceMystery2 { public static void main(String[] args) { int x = 1; int[] a = new int[2]; mystery(x, a); System.out.println(x + " " + Arrays.toString(a)); x--; a[1] = a.length; mystery(x, a); System.out.println(x + " " + Arrays.toString(a)); } public static void mystery(int x, int[] list) { list[x]++; x++; System.out.println(x + " " + Arrays.toString(list)); } } line 1 line 2 line 3 line 4
2 [0, 1] 1 [0, 1] 1 [1, 2] 0 [1, 2]
What are the values of the elements in the array numbers after the following code is executed? int[] numbers = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; for (int i = 0; i < 9; i++) { numbers[i] = numbers[i + 1]; } numbers[0] numbers[1] numbers[2] numbers[3] numbers[4] numbers[5] numbers[6] numbers[7] numbers[8] numbers[9]
20 30 40 50 60 70 80 90 100 100
Why does a method to swap two array elements work correctly when a method to swap two integer values does not?
Unlike integers, arrays are objects and use reference semantics.
What elements does the array numbers contain after the following code is executed? (Write the elements in the format: {0, 1, 2, ...} ) int[] numbers = new int[8]; numbers[1] = 4; numbers[4] = 99; numbers[7] = 2; int x = numbers[1]; numbers[x] = 44; numbers[numbers[7]] = 11; // uses numbers[7] as index elements
[0, 4, 11, 0, 44, 0, 0, 2]
Answer the following questions about arrays: What expression should be used to access the first element of an array of integers called numbers? What expression should be used to access the last element of numbers, assuming it contains 10 elements? What expression can be used to access its last element, regardless of its length?
numbers [0] numbers [9] numbers[numbers.length - 1]
Write a method called kthLargest that accepts an integer k and an array a as its parameters and returns the element such that k elements have greater or equal value. If k = 0, return the largest element; if k = 1, return the second largest element, and so on. For example, if the array passed contains the values {74, 85, 102, 99, 101, 56, 84} and the integer k passed is 2, your method should return 99 because there are two values at least as large as 99 (101 and 102). Assume that 0 <= k < a.length. (Hint: Consider sorting the array, or a copy of the array first.)
public int kthLargest(int k, int[] d) { Arrays.sort(d); return d[d.length-k-1]; }
What are the values of the elements in the array numbers after the following code is executed? int[] numbers = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; for (int i = 1; i < 10; i++) { numbers[i] = numbers[i - 1]; } numbers[0] numbers[1] numbers[2] numbers[3] numbers[4] numbers[5] numbers[6] numbers[7] numbers[8] numbers[9]
10 10 10 10 10 10 10 10 10 10
Which of the following best describes an array traversal? Which of the following are examples of problems that can be solved by traversing an array? (The order of the answer choices is randomly shuffled each time.) Comparing two arrays for equality. Examining the value of the first element of the array. Printing an array's elements. Searching an array for a given value. Swapping the first and last values of an array.
A sequential processing of each of an array's elements. Comparing two arrays for equality. Printing an array's elements. Searching an array for a given value.
The following program produces 4 lines of output. Write each line of output below as it would appear on the console. import java.util.*; // for Arrays class public class ReferenceMystery1 { public static void main(String[] args) { int x = 0; int[] a = new int[4]; x++; mystery(x, a); System.out.println(x + " " + Arrays.toString(a)); x++; mystery(x, a); System.out.println(x + " " + Arrays.toString(a)); } public static void mystery(int x, int[] a) { x++; a[x]++; System.out.println(x + " " + Arrays.toString(a)); } } line 1 line 2 line 3 line 4
2 [0, 0, 1, 0] 1 [0, 0, 1, 0] 3 [0, 0, 1, 1] 2 [0, 0, 1, 1]
Consider the following method, mystery. public static void mystery(int[] a, int[] b) { for (int i = 0; i < a.length; i++) { a[i] += b[b.length - 1 - i]; } } What are the values of the elements in array a1 after the following code executes? int[] a1 = {1, 3, 5, 7, 9}; int[] a2 = {1, 4, 9, 16, 25}; mystery(a1, a2); a1 [0] a1 [1] a1 [2] a1 [3] a1 [4]
26 19 14 11 10
Fill in the array with the values that would be stored after the code executes: int[] list = {2, 18, 6, -4, 5, 1}; for (int i = 0; i < list.length; i++) { list[i] = list[i] + (list[i] / list[0]); } list[0] list[1] list[2] list[3] list[4] list[5]
3 24 8 -5 6 1
Fill in the array with the values that would be stored after the code executes: int[] data = new int[8]; data[0] = 3; data[7] = -18; data[4] = 5; data[1] = data[0]; int x = data[4]; data[4] = 6; data[x] = data[0] * data[1]; data[0] data[1] data[2] data[3] data[4] data[5] data[6] data[7]
3 3 0 0 6 9 0 -18
Consider the following method: public static void arrayMystery5(int[] nums) { for (int i = 0; i < nums.length - 1; i++) { if (nums[i] > nums[i + 1]) { nums[i + 1]++; } } } For each array below, indicate what the array's contents would be after the method mystery were called and passed that array as its parameter. {8} {14, 7} {7, 1, 3, 2, 0, 4} {10, 8, 9, 5, 5} {12, 11, 10, 10, 8, 7}
8 14, 8 7, 2, 3, 3, 1, 4 10, 9, 9, 6, 6 12, 12, 11, 11, 9, 8
Consider the following method, mystery. public static void mystery2(int[] a, int[] b) { for (int i = 0; i < a.length; i++) { a[i] = a[2 * i % a.length] - b[3 * i % b.length]; } } What are the values of the elements in array a1 after the following code executes? int[] a1 = {2, 4, 6, 8, 10, 12, 14, 16}; int[] a2 = {1, 1, 2, 3, 5, 8, 13, 21}; mystery2(a1, a2); a1 [0] a1 [1] a1 [2] a1 [3] a1 [4] a1 [5] a1 [6] a1 [7]
a1[0] 1 a1[1] 3 a1[2] -3 a1[3] 13 a1[4] -4 a1[5] -24 a1[6] -6 a1[7] -14
Write code that creates an array named odds and stores all odd numbers between -6 and 38 into it using a for loop. Make the array's size exactly large enough to store the numbers.
int [] odds=new int [22]; for(int i=0;i<odds.length;i++){ odds[i]=i * 2 - 5; }
What is wrong with the following code? Correct the bugs to produce the following expected output: first = [3, 7] second = [3, 7] They contain the same elements. int[] first = new int[2]; first[0] = 3; first[1] = 7; int[] second = new int[2]; second[0] = 3; second[1] = 7; // print the array elements System.out.println("first = " + first); System.out.println("second = " + second); // see if the elements are the same if (first == second) { System.out.println("They contain the same elements."); } else { System.out.println("The elements are different."); }
int[] first = new int[2]; first[0] = 3; first[1] = 7; int[] second = new int[2]; second[0] = 3; second[1] = 7; // print the array elements System.out.println("first = "+Arrays.toString(first)); System.out.println("second = "+Arrays.toString(second)); // see if the elements are the same if (Arrays.equals(first, second)) { System.out.println("They contain the same elements."); } else { System.out.println("The elements are different."); }
Consider the following method, mystery. public static void mystery3(int[] data, int x, int y) { data[data[x]] = data[y]; data[y] = x; } What are the values of the elements in array numbers after the following code executes? int[] numbers = {3, 7, 1, 0, 25, 4, 18, -1, 5}; mystery3(numbers, 3, 1); mystery3(numbers, 5, 6); mystery3(numbers, 8, 4); numbers[0] numbers[1] numbers[2] numbers[3] numbers[4] numbers[5] numbers[6] numbers[7] numbers[8]
numbers[0] 7 numbers[1] 3 numbers[2] 1 numbers[3] 0 numbers[4] 8 numbers[5] 18 numbers[6] 5 numbers[7] -1 numbers[8] 5
Write a method called collapse that accepts an array of integers as a parameter and returns a new array containing the result of replacing each pair of integers with the sum of that pair. For example, if an array called list stores the values {7, 2, 8, 9, 4, 13, 7, 1, 9, 10}, then the call of collapse(list) should return a new array containing {9, 17, 17, 8, 19}. The first pair from the original list is collapsed into 9 (7 + 2), the second pair is collapsed into 17 (8 + 9), and so on. If the list stores an odd number of elements, the final element is not collapsed. For example, if the list had been {1, 2, 3, 4, 5}, then the call would return {3, 7, 5}. Your method should not change the array that is passed as a parameter.
public int[] collapse(int[] d) { int n = d.length; int[] res = new int[(n+1)/2]; int i=0; int j=0; while (i < n-1) { res[j] = d[i] + d[i+1]; j++; i += 2; } if (i == n-1) res[j] = d[i]; return res; }
Write a static method named vowelCount that accepts a String as a parameter and produces and returns an array of integers representing the counts of each vowel in the String. The array returned by your method should hold 5 elements: the first is the count of As, the second is the count of Es, the third Is, the fourth Os, and the fifth Us. Assume that the string contains no uppercase letters. For example, the call vowelCount("i think, therefore i am") should return the array {1, 3, 3, 1, 0}.
public int[] vowelCount(String x) { int[] res = new int[5]; for (int i=0; i<x.length(); i++) { char ch = x.charAt(i); if (ch == 'a') { res[0]++; } else if (ch == 'e') { res[1]++; } else if (ch == 'i') { res[2]++; } else if (ch == 'o') { res[3]++; } else if (ch == 'u') { res[4]++; } } return res; } /*public static int[] vowelCount(String input) { int[] res = new int[5]; String vowel = "aeiou"; for (int i = 0; i < input.length(); i++) { int j = vowel.indexOf(input.charAt(i)); if (j < 0) { } else { res[j]++; } } return res; } */
Write a method called allLess that accepts two arrays of integers and returns true if each element in the first array is less than the element at the same index in the second array. Your method should return false if the arrays are not the same length. For example, if the two arrays passed are {45, 20, 300} and {50, 41, 600}, your method should return true. If the arrays are not the same length, you should always return false.
public static boolean allLess (int []a, int [] b){ if(a.length!=b.length){ return false; } for(int i=0;i<a.length;i++){ if( a[i]>=b[i]){ return false; } } return true; }
Write a static method named contains that accepts two arrays of integers a1 and a2 as parameters and that returns a boolean value indicating whether or not a2's sequence of elements appears in a1 (true for yes, false for no). The sequence of elements in a2 may appear anywhere in a1 but must appear consecutively and in the same order. For example, if variables called list1 and list2 store the following values: int[] list1 = {1, 6, 2, 1, 4, 1, 2, 1, 8}; int[] list2 = {1, 2, 1}; Then the call of contains(list1, list2) should return true because list2's sequence of values {1, 2, 1} is contained in list1 starting at index 5. If list2 had stored the values {2, 1, 2}, the call of contains(list1, list2) would return false because list1 does not contain that sequence of values. Any two lists with identical elements are considered to contain each other, so a call such as contains(list1, list1) should return true. You may assume that both arrays passed to your method will have lengths of at least 1. You may not use any Strings to help you solve this problem, nor methods that produce Strings such as Arrays.toString.
public static boolean contains (int [] a1, int [] a2){ for(int j=0;j<=a1.length-a2.length;j++){ int count=0; for(int i=0;i<a2.length;i++){ if(a2[i]==a1[i+j]){ count++; } } if (count==a2.length){ return true; } } return false; }
Write a method isPalindrome that accepts an array of Strings as its argument and returns true if that array is a palindrome (if it reads the same forwards as backwards) and /false if not. For example, the array {"alpha", "beta", "gamma", "delta", "gamma", "beta", "alpha"} is a palindrome, so passing that array to your method would return true. Arrays with zero or one element are considered to be palindromes.
public static boolean isPalindrome(String[] array) { for (int i = 0; i < array.length / 2; i++) { if (!array[i].equals(array[array.length - 1 - i])) { return false; } } return true; }
Write a static method named isSorted that accepts an array of doubles as a parameter and returns true if the list is in sorted (nondecreasing) order and false otherwise. For example, if arrays named list1 and list2 store {16.1, 12.3, 22.2, 14.4} and {1.5, 4.3, 7.0, 19.5, 25.1, 46.2} respectively, the calls isSorted(list1) and isSorted(list2) should return false and true respectively. Assume the array has at least one element. A one-element array is considered to be sorted.
public static boolean isSorted(double[] data) { for (int i=1; i<data.length; i++) { if (data[i] < data[i-1]){ return false; } } return true; }
Write a method named isUnique that takes an array of integers as a parameter and that returns a boolean value indicating whether or not the values in the array are unique (true for yes, false for no). The values in the list are considered unique if there is no pair of values that are equal. For example, if a variable called list stores the following values: int[] list = {3, 8, 12, 2, 9, 17, 43, -8, 46, 203, 14, 97, 10, 4}; Then the call of isUnique(list) should return true because there are no duplicated values in this list. If instead the list stored these values: int[] list = {4, 7, 2, 3, 9, 12, -47, -19, 308, 3, 74}; Then the call should return false because the value 3 appears twice in this list. Notice that given this definition, a list of 0 or 1 elements would be considered unique.
public static boolean isUnique(int []data){ Arrays.sort(data); for(int i=1;i<data.length;i++){ if(data[i]==data[i-1]){ return false; } } return true; }
Write a method called average that computes the average (arithmetic mean) of all elements in an array of integers and returns the answer as a double. For example, if the array passed contains the values [1, -2, 4, -4, 9, -6, 16, -8, 25, -10], the calculated average should be 2.5. Your method accepts an array of integers as its parameter and returns the average. You may assume that the array contains at least one element. Your method should not modify the elements of the array
public static double average(int[] a) { double mean = 0.0; for (int i = 0; i < a.length; i++) { mean += a[i]; } return mean / a.length; }
Write a method averageLength of code that computes and returns the average String length of the elements of an array of Strings. For example, if the array contains {"belt", "hat", "jelly", "bubble gum"}, the average length returned should be 5.5. Assume that the array has at least one element.
public static double averageLength (String []data){ int sum=0; for(int i=0;i<data.length;i++){ sum+=data[i].length(); } return 1.0*sum/data.length; }
Write a method called percentEven that accepts an array of integers as a parameter and returns the percentage of even numbers in the array as a real number. For example, if the array stores the elements {6, 2, 9, 11, 3}, then your method should return 40.0. If the array contains no even elements or no elements at all, return 0.0.
public static double percentEven(int []data){ if(data.length==0){ return 0.0; } int count=0; for(int i=0;i<data.length;i++){ if(data[i]%2==0){ count++; } } return 100.0*count/data.length; }
Write a method called stdev that returns the standard deviation of an array of integers. Standard deviation is computed by taking the square root of the sum of the squares of the differences between each element and the mean, divided by one less than the number of elements. (It's just that simple!) More concisely and mathematically, the standard deviation of an array a is written as follows: standard deviation For example, if the array passed contains the values {1, -2, 4, -4, 9, -6, 16, -8, 25, -10}, your method should return approximately 11.237. You may assume that the array passed is non-null and contains at least two values, because the standard deviation is undefined otherwise. (Note: you might fail the last two tests because of rounding, but as long as it's close, then your algorithm is probably correct.)
public static double stdev(int[] numbers){ double sum = 0; double sd = 0; for(int i = 0; i < numbers.length; i++){ sum += numbers[i]; } double average = sum / numbers.length; for(int i = 0; i < numbers.length; i++){ sd += Math.pow(numbers[i] - average, 2); } double SDD = Math.sqrt(sd/ (numbers.length - 1)); return SDD; }
Write a method called countInRange that accepts an array of integers, a minimum value, and a maximum value as parameters and returns the count of how many elements from the array fall between the minimum and maximum (inclusive). For example, in the array {14, 1, 22, 17, 36, 7, -43, 5}, there are four elements whose values fall between 4 and 17.
public static int countInRange (int []data, int min, int max){ int count=0; for(int i=0;i<data.length;i++){ if(data[i]<=max && data[i]>=min){ count++; } } return count; }
Write a method named lastIndexOf that accepts an array of integers and an integer value as its parameters and returns the last index at which the value occurs in the array. The method should return -1 if the value is not found. For example, in the list containing {74, 85, 102, 99, 101, 85, 56}, the last index of the value 85 is 5.
public static int lastIndexOf(int[] data, int x) { for (int i=data.length-1; i>=0; i--) { if (data[i] == x) { return i; } } return -1; }
Write a method named longestSortedSequence that accepts an array of integers as a parameter and that returns the length of the longest sorted (nondecreasing) sequence of integers in the array. For example, if a variable named array stores the following values: int[] array = {3, 8, 10, 1, 9, 14, -3, 0, 14, 207, 56, 98, 12}; then the call of longestSortedSequence(array) should return 4 because the longest sorted sequence in the array has four values in it (the sequence -3, 0, 14, 207). Notice that sorted means nondecreasing, which means that the sequence could contain duplicates. For example, if the array stores the following values: int[] array2 = {17, 42, 3, 5, 5, 5, 8, 2, 4, 6, 1, 19} Then the method would return 5 for the length of the longest sequence (the sequence 3, 5, 5, 5, 8). Your method should return 0 if passed an empty array.
public static int longestSortedSequence(int []data){ if(data.length==0){ return 0; }else{ int count=1; int longest=1; for(int i=0;i<data.length-1;i++){ if(data[i]<=data[i+1]){ count++; if(count>longest){ longest=count; } }else{ count=1; } } return longest; } }
Write a method called max that accepts an array of integers as a parameter and returns the maximum value in the array. For example, if the array passed stores {12, 7, -1, 25, 3, 9}, your method should return 25. You may assume that the array contains at least one element. Your method should not modify the elements of the array.
public static int max (int[] data){ int max = data[0]; for(int i=0;i<data.length;i++){ if(data[i]>max){ max=data[i]; } } return max; }
Write a method named minGap that accepts an integer array as a parameter and returns the minimum 'gap' between adjacent values in the array. The gap between two adjacent values in a array is defined as the second value minus the first value. For example, suppose a variable called array is an array of integers that stores the following sequence of values. int[] array = {1, 3, 6, 7, 12}; The first gap is 2 (3 - 1), the second gap is 3 (6 - 3), the third gap is 1 (7 - 6) and the fourth gap is 5 (12 - 7). Thus, the call of minGap(array) should return 1 because that is the smallest gap in the array. Notice that the minimum gap could be a negative number. For example, if array stores the following sequence of values: {3, 5, 11, 4, 8} The gaps would be computed as 2 (5 - 3), 6 (11 - 5), -7 (4 - 11), and 4 (8 - 4). Of these values, -7 is the smallest, so it would be returned. This gap information can be helpful for determining other properties of the array. For example, if the minimum gap is greater than or equal to 0, then you know the array is in sorted (nondecreasing) order. If the gap is greater than 0, then you know the array is both sorted and unique (strictly increasing). If you are passed an array with fewer than 2 elements, you should return 0.
public static int minGap(int []data){ if(data.length<2){ return 0; } int min=data[1]-data[0]; for(int i=2;i<data.length;i++){ int gap=data[i]-data[i-1]; if(gap<min){ min=gap; } } return min; }
Write a method called mode that returns the most frequently occurring element of an array of integers. Assume that the array has at least one element and that every element in the array has a value between 0 and 100 inclusive. Break ties by choosing the lower value. For example, if the array passed contains the values {27, 15, 15, 11, 27}, your method should return 15. (Hint: You may wish to look at the Tally program from earlier in this chapter to get an idea of how to solve this problem.)
public static int mode (int []array){ int [] data=new int [101]; for(int i=0;i<array.length;i++){ data[array[i]]++; } int mode=101; int count=0; for(int i=0;i<data.length;i++){ if(data[i]>count){ count=data[i]; mode=i; } } return mode; }
Write a method priceIsRight that accepts an array of integers bids and an integer price as parameters. The method returns the element in the bids array that is closest in value to price without being larger than price. For example, if bids stores the elements {200, 300, 250, 999, 40}, then priceIsRight(bids, 280) should return 250, since 250 is the bid closest to 280 without going over 280. If all bids are larger than price, then your method should return -1. The following table shows some calls to your method and their expected results: Arrays Returned Value int[] a1 = {900, 885, 989, 1}; priceIsRight(a1, 800) returns 1 int[] a2 = {200}; priceIsRight(a2, 120) returns -1 int[] a3 = {500, 300, 241, 99, 501}; priceIsRight(a3, 50) returns -1 You may assume there is at least 1 element in the array, and you may assume that the price and the values in bids will all be greater than or equal to 1. Do not modify the contents of the array passed to your method as a parameter.
public static int priceIsRight(int []bids, int price){ int min=0; for (int i=0; i< bids.length; i++) { if (bids[i] <= price && bids[i] > min) min = bids[i]; } if (min == 0){ return -1; } return min; }
Write a static method named range that takes an array of integers as a parameter and returns the range of values contained in the array. The range of an array is defined to be one more than the difference between its largest and smallest element. For example, if the largest element in the array is 15 and the smallest is 4, the range is 12. If the largest and smallest values are the same, the range is 1. The following table shows some calls to your method and their results (the largest and smallest values are underlined): Call Value Returned int[] a1 = {8, 3, 5, 7, 2, 4}; range(a1) returns 7 int[] a2 = {15, 22, 8, 19, 31}; range(a2) returns 24 int[] a3 = {3, 10000000, 5, -29, 4}; range(a3) returns 10000030 int[] a4 = {100, 5}; range(a4) returns 96 int[] a5 = {32}; range(a5) returns 1 You may assume that the array contains at least one element (that its length is at least 1). You should not make any assumptions about the values of the particular elements in the array; they could be extremely large, very small, etc. You should not modify the contents of the array.
public static int range(int[] data) { int min = data[0], max = data[0]; for (int i=0;i<data.length;i++) { if (data[i] < min) { min = data[i]; } if (data[i]> max) { max = data[i]; } } return max-min+1; }
Write a method named swapPairs that accepts an array of strings as a parameter and switches the order of values in a pairwise fashion. Your method should switch the order of the first two values, then switch the order of the next two, switch the order of the next two, and so on. For example, if the array initially stores these values: String[] a = {"four", "score", "and", "seven", "years", "ago"}; swapPairs(a); Your method should switch the first pair ("four", "score"), the second pair ("and", "seven") and the third pair ("years", "ago"), to yield this array: {"score", "four", "seven", "and", "ago", "years"} If there are an odd number of values, the final element is not moved. For example, if the original list had been: {"to", "be", "or", "not", "to", "be", "hamlet"} It would again switch pairs of values, but the final value ("hamlet") would not be moved, yielding this list: {"be", "to", "not", "or", "be", "to", "hamlet"} You may assume that the array is not null and that no element of the array is null.
public static void swapPairs(String[] array) { for (int i = 0; i <= array.length - 1 / 2; i++) { if(i < array.length - 1){ String a = array[i]; String b = array[i + 1]; array[i + 1] = a; array[i] = b; } i++; } }
Write a method called wordLengths that accepts a Scanner representing an input file as its argument. Your method should read from the given file, count the number of letters in each token in the file, and output a result diagram of how many words contain each number of letters. Use tabs before the asterisks so that they'll line up. If there are no words of a given length, omit that line from the output. For example, if the file contains the following text: Before sorting: 13 23 480 -18 75 hello how are you feeling today After sorting: -18 13 23 75 480 are feeling hello how today you your method should produce the following output to the console: 2: 6 ****** 3: 10 ********** 5: 5 ***** 6: 1 * 7: 2 ** 8: 2 ** You may assume that no token in the file is more than 80 characters in length.
public static void wordLengths (Scanner console){ int [] array=new int[81]; while(console.hasNext()){ array[console.next().length()]++; } for(int i=1;i<array.length;i++){ if (array[i]!=0){ System.out.printf("%d: %d\t", i, array[i]); for(int j=0;j<array[i];j++){ System.out.print("*"); } System.out.println(); } } }
Consider the following method: public static int arrayMystery4(int[] list) { int x = 0; for (int i = 1; i < list.length; i++) { int y = list[i] - list[0]; if (y > x) { x = y; } } return x; } For each array below, indicate what value would be returned if the method mystery were called and passed that array as its parameter. {5} {3, 12} {4, 2, 10, 8} {1, 9, 3, 5, 7} {8, 2, 10, 4, 10, 9}
0 9 6 8 2
Write code that uses a for loop to print each element of an array named data that contains five integers. If the array contains the elements [14, 5, 27, -3, 2598], then your code should produce the following output: element [0] is 14 element [1] is 5 element [2] is 27 element [3] is -3 element [4] is 2598
for(int i=0;i<data.length;i++){ System.out.println("element ["+i+"] is "+data[i]); }
Write code that creates an array of integers named data of size 5 with the following contents: [27, 51, 33, -1, 101]
int [] data =new int [5]; data[0]= 27; data[1]= 51; data[2]= 33; data[3]= -1; data[4]= 101;
Write a method called append that accepts two integer arrays as parameters and returns a new array that contains the result of appending the second array's values at the end of the first array. For example, if arrays list1 and list2 store {2, 4, 6} and {1, 2, 3, 4, 5} respectively, the call of append(list1, list2) should return a new array containing {2, 4, 6, 1, 2, 3, 4, 5}. If the call instead had been append(list2, list1), the method would return an array containing {1, 2, 3, 4, 5, 2, 4, 6}.
public int[] append(int[] list1, int[] list2) { int n1 = list1.length; int n2 = list2.length; int[] data = new int[n1+n2]; for (int i=0; i<n1; i++) { data[i] = list1[i]; } for (int i=0; i<n2; i++) { data[n1+i] = list2[i]; } return data; }
Write a method printBackwards that prints an array of integers in reverse order, in the following format. For the array {1, -3, 4, 7, 2}: element [4] is 2 element [3] is 7 element [2] is 4 element [1] is -3 element [0] is 1
public static void printBackwards(int[] list) { if (list.length == 0) { System.out.println(); } else { for (int i = list.length - 1; i >= 0; i--) { System.out.println("element ["+ i+ "] is "+list[i]); } System.out.println(); } }