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.
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
import java.util.*;
class Arraylist {
public static void main(String args[]) {
ArrayList obj = new ArrayList();
obj.add("A");
obj.add("B");
obj.add("C");
obj.add(1, "D");
System.out.println(obj);
}
} ✓ADBC
import java.util.*;
class Output {
public static void main(String args[]) {
ArrayList obj = new ArrayList();
obj.add("A");
obj.add(0, "B");
System.out.println(obj.size());
}
} ✓ 2
Let's pretend for a moment that the ArrayList
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
123
1 7.xyz
0 9.false
false 11.123,xyz
123,xyz
123,xyz,one 14, five, 123,xyz, one
[] 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