powerpoint 6.txt

  1. What is an array?
    An array is a collection of variables of the same type (primitive or class type).
  2. What is a good usage for an array?
    To store a list of values - all of the same type.
  3. How do you declare an array?
    • type[] name = new type[size];
    • type could be int, double, String, etc.
  4. Once an array is created, its size is _________.
  5. How do you reference the ith element of an array int numbers = new int[20];
  6. What is the index of the first position in an array?
  7. How can you find the length of an array?
  8. What formula will always give you the last index position in an array?
    lastPosition = arrayName.length - 1
  9. How could you get the value at the last position in any array?
    arrayName[arrayName.length - 1]
  10. How could you get the value at the first position in any array?
  11. 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};
  12. 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 [16];
    • for (int i=0; i<17; i++)
    • numbers[i] = i+1;
  13. Review example slides p.8-9.
  14. Should array names be singular or plural?
  15. Name three basic things you can do with arrays.
    • Assign an individual value to one place
    • Read an individual value from one place
    • Read the length of the array
  16. Can you assign values to multiple places in an array at once?
  17. Can you perform a numeric operation (like addition or multiplication) on an array?
  18. 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.
  19. Can you assign the values of one array to another array like this: a[] = b[];?
  20. 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.
  21. 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.
  22. *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.
  23. When you pass an array to a method, do you use the square brackets? i.e., testMethod(arrayName[]); or testMethod(arrayName);
    No square brackets.
  24. 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[]
  25. How do you declare a method that returns an array?
    • public static arrayType[] methodName (parameters);
    • i.e. public static int[] studentGrades ( studentID, studentClasses[]);
  26. Can a for loop be based on two variables?
    • Yes.
    • for ( int i = 0, j = result.length-1; i
  27. 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
  28. 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++ );
  29. 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.
  30. 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)
  31. 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.
  32. 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.
  33. 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 found
    • Binary search - involves recursion (array must be pre-sorted)
  34. Any search is (much) more efficient if an array is _________________.
  35. 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
  36. 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.
  37. What method can you use to determine if two arrays are equal?
    Arrays.equals(array1, array2);
  38. What method can you use to load a value into an array?
    Arrays.fill(arrayName, value);
  39. 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);
  40. Is a grid a good visual interpretation of a multidimentional array?
    No, because multidimensional arrays are arrays stored within elements of other arrays.
  41. 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.
  42. In this example, what is the dimenion requested? int[][] multiplicationTable = new int [10][14];
    What is multiplicationTable.length?
  43. In this example, what is the dimenion requested? int[][] multiplicationTable = new int [10][14];
    What is multiplicationTable[0].length?
    14, and you can put any (valid) index instead of 0.
  44. 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.
  45. 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, length
    • e.g., int[][] = new arrayName[5][] is legal
  46. Can you pass a multi-dimensional array to a method?
  47. Can a method return a multi-dimensional array?
  48. In JavaFX, a stage contains a ___________ which contains a ____________ which contains a _____________.
    scene, picture, pane
  49. For an image to be visible you need to create an Image object and make it an __________________.
  50. 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.
  51. 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
  52. 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.
Card Set
powerpoint 6.txt
diane cs140 powerpoint 6