arrays Flashcards
Visualize an array as a row of boxes or elements, each one with an index number under it. We use zero-based indexing with arrays, so the first box is always numbered
0
True or false :important Point : Data types may not be mixed in the elements of an array. (T!)
true
Arrays are ______with respect to data type. This means all the data values must be of the same type.
homogeneous
the operator we use in creating an array is the square brackets [ ], also called the . (T!)
ARRAY INDEX OPERATOR
Arrays are in Java.They are declared, and then instantiated.
objects
is preferred way. (hard to miss seeing that array index operator when it’s in the middle)
Datatype [ ] arrayName
datatype may be a
primitive, or any object.
instantiate or build the array, we use the new keyword and then specify how many elements we want.
int [ ] myIntArray = new int [10]
once created, the array size is fixed, or
immutable
last element of any array will have an index number equal to the
(number of elements – -1 )
Java offers a shortcut way to declare, create and populate an array all at once by enclosing the data values inside curly braces. (T!
String [ ] monthsArray = {“Jan”,”Feb”,”Mar”,”Apr”,”May”,”Jun”,”Jul”,”Aug”,”Sep”,”Oct”, ”Nov”,”Dec”};
hen array is created, Java will automatically assign a default value to each element. (T!)
Arrays of primitive numerics (i.e. int, short, double, float, etc) get a zero value assigned to each element. Boolean array elements get initialized to are false, char array elements get assigned the unicode character of ‘u0000’.Any array of objects, such as Strings, will gets NULL assigned to each element.
Q. should we be using the < operator or the <= operator in the loop continuation condition expression?
A. ALWAYS use the < operator with arrayName.length… using <= will send you out of bounds.(T
Math.random( ) returns a double value ranging from
000 up to 0.999999999999999. Note it does not return 1.0(T!)
(int)(Math.random() * (highValue – lowValue + 1) + lowValue);
general formula for generating a range of values using Math.random
(int)(Math.random() * (highValue – lowValue + 1) + lowValue);
general formula for generating a range of values using Math.random
An array is called a random access type of structure because access to any element in the array takes the same amount of time. (T!
Q. How does this work?A. Program just has to calculate the OFFSET from the starting address of element 0 of the array by multiplying the data type size in bits by the index number of the desired element. OFFSET = (data type size in bits) * element index number
So, what would the starting address of element [10] be?Offset = index# * data type size = 10 * 32 = 320 bits.
by doing this simple calculation, your program can get access to any element in the array in the same amount of time.
Therefore, start address of element[10] would be bit 1000 + 320=1320.For element[67], offset would be 67 *32 bits = 2144 bits.Therefore, start address of element[67] would be 1000 + 2144=3144
T or F
t takes the same amount of time to retrieve the value at element[16] as it does to retrieve the value at element[98].
Arrays: Benefits and Disadvantages:
Advantages:Retrieving data from any array element takes the same amount of time if you know what element it is in.DisadvantagesArray size is fixed (immutable) once created. If you run out of space you need to create a new larger array and copy to it.Inserting a new value into the middle of a sorted array is not simple. It involves a lot of copying of the element values and then creating a new array to hold the re-sorted elements. If your data needs to be sorted and it does not change much and you need fast access to it, then an array is a good choice. (fast retrieval…T!)