Chapter 3 - Core Java APIs Flashcards
Is java.util.List a class or an interface?
The java.util.List is an interface.
What does the set method does in a list?
Set: replaces an existing value.
Format:
list.set(index,value)
What is the sintax to convert a list to an array?
Convert a list to an array:
Object[ ] objectArray = list.toArray( );
What is the sintax to convert an array to a list?
Convert an array to a list:
String[ ] array = {“Hawk”,”robin”};
List list = Arrays.asList(array);
Can a list size be changed?
List sizes cannot be changed, for example using remove. An UnsupportedOperation Exception is thrown.
Can the date and time classes be instantiated directly?
No. The date and time classes have private constructors to force you to use static methods.
Below code won’t compile:
LocalDate d = new LocalDate( ); // DOES NOT COMPILE
What is the result if invalid numbers are passed to the of( ) method?
Passing invalid numbers to of( ) throws DateTimeException.
Example:
LocalDate.of(2015, Month.January, 32); //throws an exception.
Are the date and time classes mutable or immutable?
The date and time classes are immutable just like the String class.
Do arrays have a lenght method or property?
Arrays have a lenght property NOT a method.
Example:
char[ ] c = new char [2];
int lenght = c.lenght;
Instead of:
int lenght = c.lenght( );
Which method is used in an ArrayList to show the lenght?
ArrayLists have a method that is call size. It shows the lenght of an array. In the arraysList ‘size’ is a method not a property as lenght is in arrays.
Example:
ArrayList l = new ArrayList( ); int lenght = l.size( );
What are characteristics of an array?
- An array has a fixed size.
- An array allows multiple dimensions.
- An array is ordered.
- An array is NOT immutable.
What arrays use for comparisons?
Arrays use object equality while doing comparison. i.e: equals.
What result is generated if a binary search is done for an unsorted array?
To do a binarySearch an array must be previously sorted to get a meaningful result, otherwise an undefined result is thrown.
What are characteristics of an ArrayList?
- An arraylist can change size.
- An arraylist is ordered.
- An arraylist is not immutable.
- An arraylist can have multiple dimmensions.
- ArrayLists implement equality that means same elements in the same order.
== refers to same objects.
Can primitive values be assigned to an array list?
Primitive values cannon be assigned to a list. For example:
List weights = new ArrayList;
You can see if there is an object because of the capital letter –> Double