Unit 9 Arrays, Strings & StringBuilder Flashcards
Arrays:- Write a declaration for an array variable, bank, which can reference an array object whose elements are Account objects.
Account [] bank;
Arrays:- In an Array, explain the term component
Component –an array object consists of a fixed number of components. Each component is like a variable that can hold a primitive value or a reference to an object, depending on the array’s component type.
Arrays:- In an Array, explain the term component type
Component type –this is the declared type of an array’s components. Each component of an array object has the same type.
Arrays:- In an Array, explain the term element
Element –an element is the content of an array component. An array element is either a primitive value or a reference value of the declared component type.
Arrays:- Once an array has been declared, is this a legal creation of an array
roster = new String [7];
Yes
Arrays:- Once an array has been declared, is this a legal creation of an array, and what it the last index number
Enter ZERO if illegal
int numOfElements = 7;
roster = new String[numOfElements];
Yes it is legal The last index number is 6 Note that, because of zero-based indexing, the length of an array is always one more than its last index. For example, the roster array’s last index is 6, even though its length is 7.
Arrays:- What is the length of this array?
Enter ZERO if it is an illegal creation
String[] roster;
int num = 6;
roster = new String[num + 1];
7
This is a legal way of creating an array
Arrays:- If an array is created with length 7 and the first six components are filled, leaving the last component empty, what is the last component’s default value if its component type is :-
- Reference Type?
- Numerical primitive types? (double and int)
- Boolean type?
- Char? (Unicode value)
the references are automatically initialised on creation using default values. If the component type of the array is a reference type then each element is initialised to null;
if the component type is one of the numerical primitive types (double, or int, for example) then each element is initialised to 0.0 or 0 as appropriate.
If the component type is a boolean, the elements are initialised to false, and
if it is a char, the elements are initialised to the null character (the character with Unicode value 0).
Arrays:- Write a single statement that both declares a variable bankBalances, and assigns to it an array object that can hold 52 numbers of type double.
double[] bankBalances = new double[52];
Arrays:- Write a single statement that both declares a variable frogPond, and assigns to it an array object that can hold references to three Frog objects.
Frog[] frogPond = new Frog[3];
Arrays:- Declare an array variable roster with length 6, and populates it with the String objects referenced by the Strings; Jay, John, Anne, Ali, Agi and Jill.
String[] roster = {“Jay”, “John”, “Anne”, “Ali”, “Agi”, “Jill”};
Arrays:- Create an array where 3 elements of frogPond are initialised with a reference to a different newly initialised Frog object. So frogPond can hold and does hold three newly created frogs.
Frog[] frogPond = {new Frog(), new Frog(), new Frog()};
Arrays:- Will this compile?
Frog[] frogPond;
frogPond = {new Frog(), new Frog(), new Frog()};
No, the second line is not valid this syntax can be used only if the array initialiser is assigned to an array variable in the same statement as the array variable is declared
Arrays:- Will this compile?
Frog[] frogPond;
frogPond = new Frog[]{new Frog(), new Frog(), new Frog()};
Yes This style allows us to use the array initialiser syntax and to separate the declaration of the array variable from the creation and initialisation of the array object.
Arrays:- In a single statement, declare an array variable, hours, with a component type of int, and assign to it an array that has each of its five components initialised to 40.
int[] hours = {40, 40, 40, 40, 40};
Arrays:- True or false? If an item is assigned to a component that already contains an element, the new item overwrites the original element, just as with a normal variable.
True
Arrays:- Which statements, if any, are valid, when int num = 2;
Statement one:
weights[num] = 58.3;
Statement two:
weights[num + 1] = 57.5;
the statements are both valid
Arrays:- Write code to generate a dialogue box that requests a user to enter their name (the default answer should be “anonymous”), and assign the response to the first component of an array referenced by roster. Assume that roster has already been declared as an array of strings.
roster[0] = OUDialog.request(“Please enter your name”, “anonymous”);
Or, using local variable
response = OUDialog.request(“Please enter your name”, “anonymous”);
roster[0] = response;
Note that ‘first component’ always means the component with index 0.
Arrays:- Write a declaration for an array variable, quizAnswer, which can reference an array object whose elements are boolean values.
boolean[] quizAnswer;
Arrays:- Suppose that we declared a variable frogPond and initialised it to reference an array of Frog objects, as follows:
Frog[] frogPond = new Frog[3];
Here is one way we could assign a newly initialised Frog object to the component of the array at index 0:
frogPond[0] = new Frog();
Now, using a “temp” variable to reference the new Frog object, change a frog’s position to 2 and colour RED and then assign it to frogPond’s second component.
Frog temp;
temp = new Frog();
temp.setPosition(2);
temp.setColour(OUColour.RED);
frogPond[1] = temp;
- frogPond[0] is a newly created frog, position one, colour green
- frogPond[1] is now position 2 and red
- FrogPond[2] is still ‘null’
temp.right(); and temp.brown(); Will effect frogPond[1] and temp as they are one in the same.
Arrays:-
Are Arrays substitutable?
Substitutability means a subclass type is compatible with a superclass type
Frog kermit = new HoverFrog();
Arrays are the same
frogPond[2] = new HoverFrog();
Arrays:- Is the following a valid declaration and initialisation of an array?
Object[] anArray = {new HoverFrog(), new HoverFrog()};
Yes, anArray has been declared with component type Object, which is a superclass of HoverFrog.
Arrays:-
- Create a new array object, referenced by nameArray, that can hold references to six String objects.
- Write a statement that will assign the length of nameArray to a variable called len.
- Assign the following strings to the components of nameArray (in the given order, starting at index 0). “Ann” “Rob” “Kin” “Sue” “Fethi” “Jo”.
- Replace the string element “Sue”, that is stored in nameArray, with “Lin”.
- Replace the name at index 2 of nameArray with a name entered by the user using a dialogue box.
- Create an array referenced by intArray that contains the integers 10, 5, 7, 2, 9, 8, 1 and 12 (in this order).
- Create an array frogPond that can hold references to three Frog objects, and assign a new Frog object to each component (the Frog objects should be in their initial state).
- String[] nameArray = new String[6];
- int len = nameArray.length;
will assign the length of the array to an int variable len.
3.
nameArray[0] = “Ann”;
nameArray[1] = “Rob”;
nameArray[2] = “Kin”;
nameArray[3] = “Sue”;
nameArray[4] = “Fethi”;
nameArray[5] = “Jo”;
- nameArray[3] = “Lin”;
- nameArray[2] = OUDialog.request(“Enter your name”, “anonymous”);
- int[] intArray = {10, 5, 7, 2, 9, 8, 1, 12};
7.
Frog[] frogPond = new Frog[3];
frogPond[0] = new Frog();
frogPond[1] = new Frog();
frogPond[2] = new Frog();
Alternatively you could have written:
Frog[] frogPond = {new Frog(), new Frog(), new Frog()};
Arrays:- Given the array roster, created as follows:
String[] roster = {“Jay”, “John”, “Anne”, “Ali”, “Agi”, “Jill”};
Write down what would result from executing the following statements.
- System.out.println(roster[1]);
- String name = roster[5].toUpperCase();
- OUDialog.alert(roster[roster.length -2]);
- The string “John” will be displayed via the default output device (in the OUWorkspace this would be the Display Pane).
- The message toUpperCase() is sent to the String object at index 5, and the message answer “JILL” is assigned to name.
- An alert dialogue displays the string “Agi”. (Here is why: roster.length evaluates to 6, then 2 is subtracted from this to give 4; so the string at index 4 of roster is used as the argument to the alert() method.)