Analyzing an ArrayList Object
boolean contains(Object o)
This method returns true if Object o is found in the ArrayList; false otherwise.
Your Turn
Let's try it in the Java Playground.
- Predict the output of the code.
- Run the code to determine if your prediction is correct.
- Update the found to look for "David". Predict the output of the code and run it to determine if your prediction is correct.
int indexOf(Object o)
This method return the index of the first occurrence of Object o if it exists. If the ArrayList does not contain this value, the method returns -1.
Your Turn
Let's try it in the Java Playground.
- Predict the output of the code.
- Run the code to determine if your prediction is correct.
- Write code to obtain the index for "David". Predict the output of the code and run it to determine if your prediction is correct.
- Write code to obtain the index for "Susie". Predict the output of the code and run it to determine if your prediction is correct.
boolean isEmpty()
The method returns true if there are no elements in the ArrayList and false otherwise.
Your Turn
Let's try it in the Java Playground.
- Predict the output of the code.
- Run the code to determine if your prediction is correct.
Complete List of ArrayList Tutorials
Resources