What are the defining properties of an ArrayList?
ArrayList is an ordered, resizable collection of object references. Its size and elements can change, and duplicate values are allowed.
Study Java ArrayList Algorithms and Collections with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.
What are the defining properties of an ArrayList?
ArrayList is an ordered, resizable collection of object references. Its size and elements can change, and duplicate values are allowed.
How do add(index, value) and set(index, value) differ?
add(index, value) inserts and increases the size, shifting later elements right. set(index, value) replaces an existing element without changing the size.
What indexes are valid for ArrayList access and insertion?
For access or replacement, valid indexes are 0 through size() - 1. An index equal to size() is valid only for insertion with add(index, value).
What does remove(1) mean for an ArrayList<Integer>?
In an ArrayList<Integer>, remove(1) removes the element at index 1. To remove the value 1, use remove(Integer.valueOf(1)).
Why is i < list.size() usually correct in an index loop?
Use i < list.size(). With i <= list.size(), the loop eventually attempts to access index size(), which is outside the valid range.
When should you choose an enhanced for loop?
Use an enhanced for loop when each element is needed but its index is not. Use an index-based loop when the index or controlled modification is required.
Why does backward traversal safely remove matching elements?
Traverse from the last index down to zero. Removing an element only shifts elements to its right, which have already been processed in a backward traversal.
How can a while loop remove elements without skipping any?
After removing at index i, do not increment i; examine the new element now at i. Increment i only when no removal occurs.
Why can’t an ArrayList use int as its type parameter?
Use wrapper types such as Integer, Double, or Boolean because ArrayList stores objects, not primitive values. For example, use ArrayList<Integer>, not ArrayList<int>.
What are autoboxing and unboxing?
Autoboxing converts a primitive to its wrapper object, while unboxing converts a wrapper object to its primitive value. Unboxing a null wrapper causes NullPointerException.
How should a maximum algorithm initialize its candidate?
Initialize the maximum from the first element, such as numbers.get(0), rather than from 0. Starting at 0 fails when all values are negative.
Why does duplicate detection compare j from i + 1?
Start the inner loop at i + 1. This avoids comparing an element with itself and checks each unordered pair only once.
A progressive guide to how computing reshapes culture, economic life, public institutions, and individual opportunities, with frameworks for evaluating digital equity, bias, cybersecurity, encryption, and collective participation.
A medium-difficulty assessment of programming fundamentals, algorithmic problem solving, control flow, data structures, testing, debugging, complexity, and decidability. Questions progress from foundational understanding to applied reasoning.
A 14-question quiz on the social, economic, privacy, informational, health, and environmental risks of computing innovations, including strategies for responsible design and oversight.
A practical guide to recognizing cybersecurity threats, protecting accounts and devices, and responding safely when information or systems may be compromised.
A guided introduction to how encryption, authentication, hashing, secure protocols, and key management establish confidentiality, integrity, privacy, and trust while creating practical and legal trade-offs.
A focused review of propositional logic, quantifiers, sets, relations, functions, and foundational proof methods in discrete mathematics for computer science.
A practical guide to evaluating computing innovations by examining their benefits, risks, equity, security, participation, and long-term effects on people and communities.
A medium-difficulty quiz on integrating problem definition, representation, algorithms, complexity analysis, testing, correctness, ethical evaluation, and documentation in a computer science capstone.
A progressive guide to designing, operating, and testing systems that continue useful service despite component faults, overload, data loss, and recovery challenges.
A medium-difficulty quiz on how independently operated networks, protocols, addresses, naming systems, transport services, and routing mechanisms work together to form the Internet.
A progressive guide to how packet-switched networks encapsulate data, move it across links, and manage delay, throughput, queues, and congestion.
A structured guide to designing parallel and distributed systems through decomposition, communication, coordination, state management, scalability, and fault tolerance.