Java ArrayList: A Comprehensive Guide with Exercises and Examples, Study notes of Computer Programming

A comprehensive guide to java's arraylist class, covering its fundamental concepts, methods, and practical applications. It includes code examples, exercises, and explanations to help you understand how to use arraylist effectively in your java programs. Key aspects such as arraylist's size, capacity, and methods like add, remove, get, and set. It also delves into the use of arraylist for storing various data types and its advantages over traditional arrays. Through clear explanations and practical examples, this document equips you with the knowledge and skills to confidently work with arraylist in your java projects.

Typology: Study notes

2024/2025

Available from 03/25/2025

victor-kiragu
victor-kiragu 🇺🇸

209 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Java Chapter 13: ArrayList
1. import java.util.*;
2. class Arraylist {
3. public static void main(String args[]) {
4. ArrayList obj = new ArrayList();
5. obj.add("A");
6. obj.add("B");
7. obj.add("C");
8. obj.add(1, "D");
9. System.out.println(obj);
10. }
11. } ADBC
1. import java.util.*;
2. class Output {
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Java ArrayList: A Comprehensive Guide with Exercises and Examples and more Study notes Computer Programming in PDF only on Docsity!

Java Chapter 13: ArrayList

  1. import java.util.*;
  2. class Arraylist {
  3. public static void main(String args[]) {
  4. ArrayList obj = new ArrayList();
  5. obj.add("A");
  6. obj.add("B");
  7. obj.add("C");
  8. obj.add(1, "D");
  9. System.out.println(obj);
  10. }
  11. } ✓ADBC
  12. import java.util.*;
  13. class Output {
  1. public static void main(String args[]) {
  2. ArrayList obj = new ArrayList();
  3. obj.add("A");
  4. obj.add(0, "B");
  5. System.out.println(obj.size());
  6. }
  7. } ✓ 2
  8. Let's pretend for a moment that the ArrayList class implements the get method in the following manner: public Object get(int i) { < statement > return items[i]; }

    A prime is an integer that is greater than 1 and has no factors besides 1 and itself. A list of primes can be built gradually. If at some point the list holds the first n primes in ascending order, the next prime is the smallest integer that is greater than the last prime in the list and is not evenly divisible by any of the numbers already in the list. We can start with a list with just one element, 2. Using the above algorithm, write a method that creates and returns an ArrayList that holds all the primes below a given number N. Use your method in a program that prints all the primes under 10,000 in ascending order. ✓import.java.util.ArrayList; public class Primes { public static ArrayList allPrimes (int n) { ArrayList< Integer> primes= new ArrayList(); int p= 2; primes.add(p); while ( p<n) { p++; boolean foundDivisor =false; for (int i = 0; i < primes.size() && !foundDivisor; i++) if (p % primes.get (i) ==0)

    foundDivisor = true; if (!foundDivisor) primes.add(p); } return primes; } } public static void main (String[]args) { ArrayListprimes = allPrimes(10000); for (Integer p: primes) System.out.println(p + " "); } } ArrayList ✓library class from java.util that keeps track of size and capacity ArrayList list = new ArrayList(); for (int i = 1; i <= 10; i++) { list.add(10 * i); // [10, 20, 30, 40, ..., 100] } What is the output of the following code?

    methods. ✓public ArrayList concatenate (ArrayList list1, ArrayListlist2) { ArrayListresult = new ArrayList(); for (String s: list1) result.add(s); for (String s: list 2) result.add (s); return result; } size of ArrayList ✓is the number of elements currently stored (capacity = size if you need to add another element) ArrayList provides get and set methods ArrayList holds a specific type --- syntactically: ArrayList ArrayList constructor creates an empty list (size () == 0) of default capacity = 10 ArrayList implements over two dozen methods Be careful with ArrayList add and remove methods, they change the size of the list and indices of subsequent elements Know what a toString method does in an ArrayList String s = "abcdefghijklmnop";

    ArrayList r = new ArrayList(); r.add("abc"); r.add("cde"); r.set(1,"789"); r.add("xyz"); r.add("123"); Collections.sort(r); r.remove(2); The first index position in an array is __________. System.out.print( s.substring(0,1) ); // LINE 2 System.out.print( s.substring(2,3) ); // LINE 3 System.out.print( s.substring(5,6) ); // LINE 4 System.out.print( r.get(0) ); // LINE 5 System.out.print(r.get(0).substring(0,1)); // LINE 6 System.out.print( r.get(2) ); // LINE 7

    3.c 4.f

    1. 123
    2. 1 7.xyz
    3. 0 9.false
    4. false 11.123,xyz
    5. 123,xyz
    6. 123,xyz,one 14, five, 123,xyz, one
    7. [] Suppose list is an ArrayList and n == list.size(). Which of the following calls will throw an IndexOutOfBoundsException? ✓list.get list.set NOT add remove T or F? An ArrayList holds boolean values. ✓False T or F? An ArrayList can also hold references to Integer objects. ✓False

    T or F? An ArrayList can hold objects of any type. ✓True T or F? An ArrayList can also hold references to Object objects. ✓False T or F? An ArrayList holds references to String objects. ✓True T or F? F (a)¸ java.util.ArrayList extends java.util.List. ✓False The wrapper class for the primitive type char is ____________________. ✓Character The wrapper class for the primitive type int is ____________________. ✓Integer To treat primitive type values as objects, you must use ____________________ classes. ✓wrappers Which of the following ArrayList's methods returns an object of the type E? ✓get set NOT add contains Which statement accesses the first element of an array list of strings named sData. ✓String s = sData.get(0);