All Flashcards
What are the differences between ArrayLists and arrays in terms of size?
ArrayLists are dynamic in size; arrays have a fixed size.
What are the differences between ArrayLists and LinkedLists in terms of element access?
ArrayLists allow direct access; LinkedLists require sequential access.
What are the differences between using primitive types (e.g., int) directly and using their wrapper classes (e.g., Integer) in ArrayLists?
Primitive types cannot be directly stored in ArrayLists, requiring wrapper classes. Wrapper classes have overhead.
Compare the memory usage of ArrayList
ArrayList
Compare the performance of adding elements to the end of an ArrayList vs. adding elements to a LinkedList.
Adding to the end of an ArrayList is generally faster due to direct access, while LinkedList requires traversing to the end.
Compare the ease of use between ArrayLists and arrays when inserting elements in the middle of the list.
ArrayLists provide methods for easy insertion, while arrays require manual shifting of elements.
Compare the type safety of a raw ArrayList (without generics) and an ArrayList with generics.
ArrayList with generics provides compile-time type safety, while a raw ArrayList does not.
Compare the performance of accessing an element in an ArrayList using its index vs. accessing an element in a LinkedList using its index.
ArrayList provides O(1) (constant time) access, while LinkedList provides O(n) (linear time) access.
Compare the use of ArrayLists and arrays in terms of their ability to store null values.
Both ArrayLists and arrays can store null values.
Compare the suitability of ArrayLists and arrays for scenarios where the size of the data collection is known in advance and will not change.
Arrays are more suitable in this scenario due to their lower memory overhead and potentially faster access times.
What is the definition of Java Collections Framework?
A set of data structures used to store data in Java, including sets, lists, deques, and maps.
What is a Set?
A data type where each item occurs only once, and the data is unordered.
What is a List?
A collection of items that can repeat, and the data is ordered.
What is a Deque?
Similar to lists, but items can only be added or removed at the beginning and the end.
What is a Map?
Pairs with a key and a value to represent pairs of items.
What is an ArrayList?
A dynamic array that can grow or shrink in size during runtime.
What are Generics?
A feature that allows specifying the type of objects an ArrayList can hold, enabling compile-time type checking.
What is a Wrapper Class?
A class that encapsulates a primitive data type, like Integer for int or Double for double.
What is autoboxing?
Automatic conversion between primitive data types and their corresponding wrapper classes.
What is unboxing?
Automatic conversion from wrapper class objects to their corresponding primitive data types.
What is the key difference between LinkedLists and ArrayLists in terms of element access?
LinkedLists require sequential access, while ArrayLists allow direct access using a method call.
Why are generics useful when creating ArrayLists?
They allow the compiler to perform type checking, preventing runtime errors and improving code safety.
Explain the advantage of using ArrayLists over arrays when the size of the data is unknown.
ArrayLists are dynamic and can automatically adjust their size, while arrays have a fixed size.
What is the purpose of importing 'java.util.ArrayList'?
It makes the ArrayList class available for use in your Java program.
Why are wrapper classes used with ArrayLists?
ArrayLists can only store objects, not primitive data types. Wrapper classes allow you to store primitives as objects.
What are the performance implications of using Wrapper Classes?
They are generally slower and use more memory than their primitive counterparts.
When should you consider using regular arrays instead of ArrayLists?
When working with large amounts of data and needing to optimize for performance.
Explain how ArrayLists provide dynamic resizing.
ArrayLists automatically increase their capacity when more elements are added than the current capacity allows.
What is the significance of the '
'E' represents the type of object that the ArrayList will store. It allows you to specify the class of objects in the ArrayList.
Explain the concept of type safety in the context of ArrayLists and Generics.
Generics enforce type safety by ensuring that only objects of the specified type can be added to the ArrayList, preventing runtime ClassCastException errors.