-
Notifications
You must be signed in to change notification settings - Fork 0
/
arraylist.html
66 lines (58 loc) · 3.99 KB
/
arraylist.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<!DOCTYPE html>
<html lang="en">
<head>
<title>ArrayLists</title>
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1">
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="index.js"></script>
<link rel="icon" type="image/x-icon" href="images/star.png">
</head>
<body onload="head(); dateTime(); foot();">
<header id="header" style="position:fixed;"></header>
<h1 class="title">ArrayLists</h1>
<div class="text">
<h3>What are ArrayLists?</h3>
<p>The ArrayList is a dynamic array, meaning that it is resizable. This solves the issue with an array’s set size, but takes up more memory. The ArrayList implements the java.util.List<E> interface. ArrayLists can hold objects of a specified type, but not elements of primitive data type. The ArrayList keeps track of the list capacity and list size.</p>
<h3><br>ArrayList Syntax</h3>
<p>Declaration and initialization: <span class="textCode">ArrayList<Type> arrList = new ArrayList<Type>();</span> or <nobr><span class="textCode">ArrayList<Type> arrList = new ArrayList<Type>(int capacity);</span></nobr><br>For ArrayLists without a specified size, the default size is 10.<br><br>ArrayList elements have indicies like arrays, but cannot be accessed with the array syntax. Iterating through an ArrayList uses a for or for-each loop.
<br><br>ArrayList methods:
<uL>
<li><span class="textCode">arrName.size()</span>: Returns size of array as int</li>
<li><span class="textCode">arrName.isEmpty()</span>: Returns a boolean for if the ArrayList is empty or not</li>
<li><span class="textCode">arrName.add(E obj)</span>: Adds element at the end of the ArrayList</li>
<li><span class="textCode">arrName.add(int i, E obj)</span>: Adds element at index i</li>
<li><span class="textCode">arrName.set(int i, E obj)</span>: Sets the element at index i to E</li>
<li><span class="textCode">arrName.get(int i)</span>: Accesses the element at index i</li>
<li><span class="textCode">arrName.remove(int i)</span>: Removes the element at index i</li>
<li><span class="textCode">arrName.contains(E obj)</span>: Returns a boolean for if the ArrayList contains E or not</li>
<li><span class="textCode">arrName.indexOf(E obj)</span>: Returns the index of E. If E is not present, returns -1</li>
</uL>
Although ArrayLists need to be declared as an object type, autoboxing/autounboxing occurs when there needs to be conversion from int to Integer and from double to Double. Thus, one does not need to use objects when implementing ArrayList methods with numbers.</p>
<p class="underline">Example:</p>
<div class="code">
<pre>
ArrayList<Integer> arrList = new ArrayList<Integer>(2);
arrList.add(1);
arrList.add(2); //list is now [1, 2]
arrList.add(1, 5); //list is now [1, 5, 2]
arrList.set(0, 0); //list is now [0, 5, 2]
System.out.println(arrList.get(1)); //prints 5
System.out.println(arrList.indexOf(2)); //prints 2
System.out.println(arrList.size()); //prints 3
arrList.remove(0); //list is now [5, 2]
</pre>
</div>
<h3><br>ArrayList Details</h3>
<p>An ArrayList automatically doubles in capacity when it runs out of space. It allocates a bigger array and copies all pre existing values into it. In addition, the compiler will throw IndexOutOfBoundsException like an array if the index is invalid.</p>
<h2><br>Common Mistakes</h2>
<ol>
<li>Forgetting that the type has to be an object</li>
<li>Trying to access elements using array syntax</li>
<li>Forgetting that indices start at 0</li>
<li>Trying to access out of bounds indices</li>
</ol>
</div>
<footer id="footer"></footer>
</body>
</html>