# powerpoint 6.txt

 What is an array? An array is a collection of variables of the same type (primitive or class type). What is a good usage for an array? To store a list of values - all of the same type. How do you declare an array? type[] name = new type[size]; type could be int, double, String, etc. Once an array is created, its size is _________. fixed How do you reference the ith element of an array int numbers = new int; numbers[i] What is the index of the first position in an array? 0 How can you find the length of an array? arrayName.length() What formula will always give you the last index position in an array? lastPosition = arrayName.length - 1 How could you get the value at the last position in any array? arrayName[arrayName.length - 1] How could you get the value at the first position in any array? arrayName What is the syntax for initializing an array with a list of values? int[] arrayName = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; How could you create an array with 16 elements that starts with 1 and each position holds a value that is the last value + 1? int[] numbers = new int ;for (int i=0; i<17; i++) numbers[i] = i+1; Review example slides p.8-9. Should array names be singular or plural? singular Name three basic things you can do with arrays. Assign an individual value to one placeRead an individual value from one placeRead the length of the array Can you assign values to multiple places in an array at once? No Can you perform a numeric operation (like addition or multiplication) on an array? No Is this how you compare two arrays: if (a[] == b[]) ...? No. You first compare the length then each element in the array.Also, you can use the .equals method. Can you assign the values of one array to another array like this: a[] = b[];? no If two arrays are equal (they have been assigned as a = b), what do you know about them? The entire contents of the array are stored in the same location in memory. What is pass-by-value? When a primitive-type variable is designated to be passed to a method, only the value of the variable is passed to the method. The method cannot change the value of the variable it has been passed. *What is pass-by-sharing? When an array is passed to a method, the array itself is passed. The method can change the values stored in the array. When you pass an array to a method, do you use the square brackets? i.e., testMethod(arrayName[]); or testMethod(arrayName); No square brackets. When you declare a method that takes an array as a parameter, how is the array shown in the parameter list? You use square brackets, but no dimensions are indicated.array[] How do you declare a method that returns an array? public static arrayType[] methodName (parameters);i.e. public static int[] studentGrades ( studentID, studentClasses[]); Can a for loop be based on two variables? Yes.for ( int i = 0, j = result.length-1; i What method can you use to turn a String into an array? .toCharArray() returns an array of char type with one character from the original string in each position Describe the shortcut you can use when programming a for loop based on an array. The syntax is for ( char c:doubled ) and it is equivalent to for ( int i = 0; i < doubled.length; i++ ); How can you create an array where the size is user-defined? You could use a Scanner object to receive input from the user that indicates what the array size should be. Then you can use the variable that stored the user input to declare the array. What is a potential problem with having a "dynamic" array size? You could attempt to reference an element in the array that doesn't exist (OutOfBounds error) What does this statement do? public static double[] print( double... d1) { It is the declaration of an array of indeterminate length. It indicates that the values in the array will be of double type. Is this legal? fibonacci[i] = fibonacci[i-1] + fibonacci[i-2] Yes. This is an example of recursion. An array is filled based on the previous two values in the array. What are the two types of searches you can perform on an array? Sequential search - traverse the array in order until the sought element is foundBinary search - involves recursion (array must be pre-sorted) Any search is (much) more efficient if an array is _________________. pre-sorted What is the easiest method to use to sort an array? Arrays.sort(nameOfArray);also Arrays.sort(nameOfArray,startPosition, endPosition+1);the startPosition is included, the endPosition is excluded What does the Arrays.binarySearch(arrayName, soughtValue) method do? It temporarily presorts the array and gives you the index of the position of the sought value *in the presorted array*.The order of the items in the array remains unchanged. What method can you use to determine if two arrays are equal? Arrays.equals(array1, array2); What method can you use to load a value into an array? Arrays.fill(arrayName, value); If you want to print a list of all the values in an array, can you do it without a loop? Yes, with Arrays.toString(arrayName); Is a grid a good visual interpretation of a multidimentional array? No, because multidimensional arrays are arrays stored within elements of other arrays. Is there a limit on the number of dimensions you can have in an array? Java doesn't limit the number of dimensions, but Java VM limits the dimensions to 255. In this example, what is the dimenion requested? int[][] multiplicationTable = new int ; What is multiplicationTable.length? 10 In this example, what is the dimenion requested? int[][] multiplicationTable = new int ; What is multiplicationTable.length? 14, and you can put any (valid) index instead of 0. What is a ragged array? An array that has rows of different lengths. This can happen in a two-dimensional array that assigned and declared at the same time. When declaring a two dimensional array, you must declare __________________, but you don't have to specify the __________of subsequent arrays; only that they exist. the length of the initial array, lengthe.g., int[][] = new arrayName[] is legal Can you pass a multi-dimensional array to a method? Yes Can a method return a multi-dimensional array? Yes In JavaFX, a stage contains a ___________ which contains a ____________ which contains a _____________. scene, picture, pane For an image to be visible you need to create an Image object and make it an __________________. ImageView What does String[] s_a = stringName.split(";"); do? It splits the string stringName into cells in the array s_a whenever there is a semicolon. If there were two ;s in a row it would create an empty cell. If you use two semicolons as the delimiter, it would only create a new cell-value when it encounters 2 ;s in a row. What are some special delimiters? | - splits on each character\| - splits on each pipe (esc character)\\ - splits on any whitespace character[jklmnop] - square braces means split on the sequence of characters in the braces What does this do? String s_a = stringName.split(";",4); It puts the first three strings (split on ;) into an array. The number is exclusive. Authordimeng ID300089 Card Setpowerpoint 6.txt Descriptiondiane cs140 powerpoint 6 Updated2015-04-06T19:11:55Z Show Answers