Array List Week 7
Me attempting to do the work and be responsible
Notes
7.1 Intro to ArrayLists
- Arrays: Static, Static (fixed size), Fundamental java feature, An object with no methods, Not as flexible, Can store more primitive data, Slightly slower than Arrays
- ArrayLists: Dynamic (can change size) Part of a framework, A class with many methods, Designed to be more flexible, Store object references, Can only be used with an import statement
Primitive Data Types:
- boolean
- char
- double
- int
Wrapper Class Data Types (Store the primitive values as objects)
- Boolean
- Character
- Double
- Integer
7.2 ArrayList Methods
size(); --> Returns the number of elements in the list add(obj); --> Adds element at the end add(index, object); --> Adds element at specific index remove(index); --> Removes element from specific index set(index, object); --> Replaces element at index with new object get(index); --> Returns element at index
7.3 Traversing ArrayLists
- 2 ways --> for loops and enhanced for loops
- use get() instead of bracket notation for getting an element of an arraylist
- use size() to find number of elements in arraylist instead of using .length
7.4 Developing Algorithms Using ArrayLists
They can...
- Modify array values
- Find the max value
- Find the min value
7.5 Searching
Important to remember...
- When looking at int values, the == operator should be used.
- When searching for a double value, we need to make sure the value is close enough by doing some math.
- Object instances should always use the .equals(otherThing) method to check for a match.
7.6 Sorting
- Selection sort identifies either the maximum or minimum of the compared values and iterates over the structure checking if the item stored at the index matches the condition, if so, it will swap the value stored at the index and continue.
- The insertion sort is characterized by building a sorted structure as it proceeds. It inserts each value it finds at the appropriate location in the data structure. This is often accomplished by using a while loop as the inner loop.
7.7 Ethical Issues around Data Collection
- More data --> more to protect --> more likely leak will happen
- Delete things you don't need anymore
- Ensure important data is encrypted
// HACK!!!!
// Create an arrayList and use one of the cool methods for it
import java.util.ArrayList;
public class hack1 {
public static void main (String[] args) {
ArrayList<String> omoriChara = new ArrayList<String>(Arrays.asList("Mari", "Hero", "Aubrey", "Basil", "Kel", "Sunny"));
System.out.println("There are " + omoriChara.size() + " characters still alive");
omoriChara.remove("Mari");
System.out.println("There are now " + omoriChara.size() + " characters still alive");
omoriChara.add("Something");
System.out.println("There are now " + omoriChara.size() + " characters in the game");
}
}
hack1.main(null);
import java.util.ArrayList;
ArrayList<String> color = new ArrayList<String>();
color.add("red apple");
color.add("green box");
color.add("blue water");
color.add("red panda");
for(int i = color.size() - 1; i >= 0; i--) {
if(color.get(i).contains("red")) { color.remove(i);}
}
System.out.println(color);
/*/
using
if(color.get(i).contains("red"))
iterate through the arraylist and remove all elements that contain the word red in them
/*/
// find the sum of the elements in the arraylist
ArrayList<Integer> num = new ArrayList<Integer>();
num.add(5);
num.add(1);
num.add(3);
int sum = 0;
for (int number: num) {
sum += number;
}
System.out.print(sum);