Chapter 3: Core Java APIs Flashcards
Covers using operators, Arrays, Strings & StringBuilder, ArrayList, classes from java.time & wrapper classes
What are the features of an ArrayList
1) its an ordered list 2) Dynamic in size 3) Allows duplicates
What must you do before using the ArrayList class?
you must import the ArrayList class using either : import java.util.*; (imports entire package) OR import java.util.ArrayList; (imports just the class)
How many ways can you create an ArrayList for the purposes of the exam?List them
You can use 3 different constructors ArrayList list = new ArrayList( ); // sets elements to their default values ArrayList list2 = new ArrayList(10); // set the initial capacity ArrayList list3 = new ArrayList(list2); // creating a copy of another list. This constructor takes a collection as a param.
What interface does List extend? How would you create a new type of this interface
Interface extends Collection. Collection collection = new ArrayList( ) OR Collection collection2 = new ArrayList<>( );
Name all the methods for the ArrayList (only the ones you need to know for the exam).
add( ) , set( ) remove ( ) , contains ( ) equals ( ) , clear ( ) IsEmpty ( ) , size ( )
Will this compile? Explain.
ArrayList list = new ArrayList <>( );
list.add(Boolean.TRUE);
No because we are using generics to specify that this list only accepts type String which Boolean.TRUE isn’t.
Will this compile? Explain.
ArrayList list = new ArrayList( );
list.add(Boolean.TRUE); list.add(“hawk”);
Yes, as we haven’t specified a type for this list, we can add any object.
What does this code do?
List <string>birds = new ArrayList<>( );</string>
birds. add(“hawk”);
birds. add(1, “robin”);
it adds “hawk” to the end of the birds list and then adds “robin” to index 1 of the list which is after “hawk”
What will be the output of this code? List<> list1 = new ArrayList( ); list1. add(“hello”)
It won’t compile as the diamond operator on the RHS is empty. it should have a type declared inside
What are the method signatures for remove( ) method of an arrayList?
boolean remove(Object) // returns a boolean when an element is passed in. AND E remove(int index) // removes an element at the set index
In arrayList, what is the method signature for set( )? what does this method do?
E set (index, newElement). set( ) replaces an element at the index provided with the new element passed in
What do the IsEmpty( ) and size( ) methods of ArrayList return when called independently?
A boolean and an int
What does ArrayLists clear( ) method do?
clear () empties all the contents of the list
what does the ArrayList contains( ) do?
it checks to see whether the list has the specified value and returns a boolean
what is the output of this code?
List<string> one = new ArrayList<>( );</string>
List<string> two = new ArrayList<>( );</string>
one. add(“a”); one.add(“b”);
two. add(“a”);
two. add(0, “b”);
System.out.println(one.equals(two));
false. because for the equals to return true, the size and the order of the list has to be the same
What does the equals() method do?
it checks whether the two lists contain the same elements and in the same order.
What is the method for converting a String into an int primitive?
parseInt( ) e.g : int primitive = Integer.parseInt(“123”)
What is the method for converting a String into an Integer wrapper class?
valueOf( ) e.g: Integer wrapper = Integer.valueOf(“123”)
How do you convert a String into a boolean primitive and a float primitive?
Boolean.parseBoolean(“true”); Float.parseFloat(“2.2”);
How do you convert a String into a Boolean and Float wrapper class?
Boolean.valueOf(“true”); Float.valueOf(“2.2”);
How do you convert a String into a Character wrapper class
No such method exists, you just call charAt normally.
What is Autoboxing?
This is when Java automatically converts a primitive into its relevant wrapper class. This is great as ArrayLists aren’t allowed to contain primitives
What is unboxing?
When you convert a wrapper into a primitive
What happens on line 1 & 2 of this code? List heights = new ArrayList<>( ); heights. add(null); // 1 int h = heights.get(0); // 2
// 1 compiles fine as you can assign null to any variable reference. // 2 will result in a NullPointerEx as you are calling a method on null
SUV s = al1.get(0)
Car c1 = al2.get(0)
Drivable d1 = al3.get(0);
Car c2 = al4.get(0);
Drivable d2 = al5.get(0);