0% found this document useful (0 votes)
6 views

CS REG - Unit 7 - Part ONE

Uploaded by

zhichen.zheng
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

CS REG - Unit 7 - Part ONE

Uploaded by

zhichen.zheng
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Problem with Arrays

Consider the following problem: Read words from a text file and store the data in memory.
String[] allWords = new String[2500];

Problem: You don't know how many words the text file contains.

The array above may be enough to store all the words from Wordle(about 2300).
But what if the text file contains all of the works of Shakespeare?
• Hard to create an array of the appropriate size.
Luckily, there are other ways to store data besides in an array.
Lists
• list: a collection storing an ordered sequence of elements
• each element is accessible by a 0-based index
• a list has a size (number of elements that have been added)
• elements can be added to the front, back, or elsewhere
• in Java, a list can be represented as an ArrayList object
UNIT 7 - ARRAYLIST
7.1 - INTRODUCTION TO ARRAYLIST
7.2 - ARRAYLIST METHODS
7.3 - TRAVERSING ARRAYLISTS
7.4 - DEVELOPING ALGORITHMS USING ARRAYLISTS
7.5 - SEARCHING
7.6 - SORTING
7.7 - ETHICAL ISSUES AROUND DATA COLLECTION
Difference between Array and ArrayList
Array ArrayList

• Fixed length • Resizable length

• Fundamental Java feature • Part of a Framework

• An Object with no methods (arr.length) • A Class with many methods (arrlist.length())

• Not as flexble • Not designed to store primitives

• Can store primitive data


• Is slightly slower than Arrays
• Can only be used with an important statement (java
util)
7.1: Introduction to ArrayList
● Start with an important statement:
import java.util.ArrayList;
import java.util.List; (previous java version)

● Declare a variable to reference an ArrayList object


ArrayList<DataType> variableName = new ArrayList<DataType>();
List<DataType> variableName = new List<DataType>(n);

You don’t need to provide the length of the ArrayList when instantiate
Primitive Values disguised as Wrapper Class Objects (2.9)

● ArrayList objects are designed to only store references to objects, NOT primitive values. A
workaround is to use Wrapper classes, which store primitive values as objects.
2.9 Wrapper class
● For every primitive type in Java, there is a built-in object type called a wrapper class.

● The wrapper class for int is called Integer, and for double it is called Double.

● Sometimes you may need to create a wrapped object for a primitive type so that you can give
it to a method that is expecting an object.
2.9 Wrapper Class on your reference sheets

The int type in Java can be used to represent any whole number from -2147483648 to 2147483647.
• Why those numbers?
• - Integers in Java are represented in 2’s complement binary and each integer gets 32 bits of space.
In 32 bits of space with one bit used to represent the sign you can represent that many values.

• Why is there one more negative number than positive number?


• - It is because 0 is considered a positive number.

• Integer.MAX_VALUE + 1 = Integer.MIN_VALUE; Integer.MIN_VALUE-1 = Integer.MAX_VALUE


Example: Instantiate an ArrayList Object
import java.util.ArrayList;

public class Main{


public static void main (String[ ] args) {
ArrayList<Integer> a1 = new ArrayList<Integer>();

ArrayList<String> a2 = new ArrayList<String>(5);


// this is an arraylist of initial capacity 5

ArrayList<Strudent> a3 = new ArrayList<Student>();

}
Create more ArrayLists
● a) to store Boolean values

ArrayList< > al1 = new ArrayList< >( );

● b) to store Turtle objects


ArrayList< > al2 = new ArrayList< >( );

● c) to store 10 Strings, initially


ArrayList< > al3 = new ArrayList< >( );
Create more ArrayLists
● a) to store Boolean values

ArrayList< Boolean > al1 = new ArrayList< Boolean >( );

● b) to store Turtle objects


ArrayList< Turtle > al2 = new ArrayList< Turtle >( );

● c) to store 10 Strings, initially


ArrayList< String > al3 = new ArrayList< String >( 10 );
UNIT 7 - ARRAYLIST
7.1 - INTRODUCTION TO ARRAYLIST
7.2 - ARRAYLIST METHODS
7.3 - TRAVERSING ARRAYLISTS
7.4 - DEVELOPING ALGORITHMS USING ARRAYLISTS
7.5 - SEARCHING
7.6 - SORTING
7.7 - ETHICAL ISSUES AROUND DATA COLLECTION
Collection Frameworks
● Frameworks are prewritten, high-performing and efficient code that can handle and use objects of
groups of data.
7.2 ArrayList methods

Note: E refers to the type of objects in the arraylist.


ArrayList vs. Array
• construction
String[] names = new String[5];
ArrayList<String> list = new ArrayList<String>();

• checking length
int len = names.length;
int lenl = list.size();

• storing a value
names[0] = "Jessica";
list.add("Jessica");

• getting a value
String s = names[0];
String s = list.get(0);
Size of the ArrayList
int size( ): returns the number of elements in the list

Consider the following code:

ArrayList<Integer> a1 = new ArrayList<Integer>(5);

The ArrayList a1 has been instantiated with no entries.

System.out.println(a1.size( )); // 0
CODING LAB

Adding Items to an ArrayList

trees boolean add(E obj)


0 1

• Appends the specified object at the end of the list


• Dynamically increases the size by 1
• Returns a boolean value

ArrayList<Tree> trees = new ArrayList<Tree>();

trees.add(treeG);
trees.add(treeW);
trees.size(); //2
CODING LAB

Adding Items to an ArrayList

trees boolean add(E obj)


0 1 2

• Appends the specified object at the end of the list


• Dynamically increases the size by 1
• Returns a boolean value
trees.add(treeP);
CODING LAB

Adding Items to an ArrayList


void add(int index, E obj)
trees

0 1 2 3

• Moves elements at position index and higher to the right


• Inserts the specified object at the given index position
• Dynamically increases the size by 1
trees.add(1, treeY);
CODING LAB

Adding Items to an ArrayList: void add(int index, E obj)

trees.add(1, treeY);

0 1 2 0 1 2 3

• Moves elements at position index and higher to the right


• Inserts the specified object at the given index position
• Dynamically increases the size by 1
CODING LAB

Deleting Items from ArrayList: E remove(int index)

0 1 2 3 0 1 2

• Removes the element at the given index position


• Moves elements at index+1 and higher to the left
• Dynamically decreases the size by 1
• Returns the element that was originally at the given index position
Tree treeX = trees.remove(2);
CODING LAB

Deleting Items from ArrayList: E remove(int index)

0 1 2 3 0 1 2

• Removes the element at the given index position


• Moves elements at index+1 and higher to the left
• Dynamically decreases the size by 1
• Returns the element that was originally at the given index position
Tree treeX = trees.remove(2);

System.out.println(treeX) //Assume toString is written


// this will print the white tree
CODING LAB

Updating Items in an ArrayList: set(int index, E obj)

0 1 2 3 0 1 2 3

• Replaces the element at the given index position with obj


• Does not change the size
• Returns the element that was originally at the given index position
Tree treeY = trees.set(3,treeO);
CODING LAB

Updating Items in an ArrayList: set(int index, E obj)

0 1 2 3 0 1 2 3

• Replaces the element at the given index position with obj


• Does not change the size
• Returns the element that was originally at the given index position
Tree treeY = trees.set(3,treeO);

System.out.println(treeY); //assume toString method is written


// this will print the purple tree
CODING LAB

Accessing Items in an ArrayList


E get(int index):returns the element at position index in the list.

0 1 2 3

Tree tree1 = trees.get(1);

Tree tree2 = trees.get(0);

Tree tree3 = trees.get(trees.size()-1);


UNIT 7 - ARRAYLIST
7.1 - INTRODUCTION TO ARRAYLIST
7.2 - ARRAYLIST METHODS
7.3 - TRAVERSING ARRAYLISTS
7.4 - DEVELOPING ALGORITHMS USING ARRAYLISTS
7.5 - SEARCHING
7.6 - SORTING
7.7 - ETHICAL ISSUES AROUND DATA COLLECTION
Practice: Remove ALL Occurrences of “JJ”

Beau JJ Beau Beau JJ JJ JJ Beau

0 1 2 3 4 5 6 7

Given the ArrayList (ourDogs) of Strings, remove all elements containing the String “JJ”
Remove ALL Occurrences of “JJ”

Beau JJ Beau Beau JJ JJ JJ Beau

0 1 2 3 4 5 6 7

Given the ArrayList (ourDogs) of Strings, remove all elements containing the String “JJ”

Beau

JJ Beau Beau JJ JJ JJ Beau

0 1 2 3 4 5 6 7
Remove ALL Occurrences of “JJ”

Beau JJ Beau Beau JJ JJ JJ Beau

0 1 2 3 4 5 6 7

Given the ArrayList (ourDogs) of Strings, remove all elements containing the String “JJ”

JJ

Beau Beau Beau JJ JJ JJ Beau

0 1 2 3 4 5 6 7
Remove ALL Occurrences of “JJ”

Beau JJ Beau Beau JJ JJ JJ Beau

0 1 2 3 4 5 6 7

Given the ArrayList (ourDogs) of Strings, remove all elements containing the String “JJ”

Beau Beau Beau JJ JJ JJ Beau

0 1 2 3 4 5 6
Remove ALL Occurrences of “JJ”

Beau JJ Beau Beau JJ JJ JJ Beau

0 1 2 3 4 5 6 7

Given the ArrayList (ourDogs) of Strings, remove all elements containing the String “JJ”

Beau

Beau Beau JJ JJ JJ Beau

0 1 2 3 4 5 6
Remove ALL Occurrences of “JJ”

Beau JJ Beau Beau JJ JJ JJ Beau

0 1 2 3 4 5 6 7

Given the ArrayList (ourDogs) of Strings, remove all elements containing the String “JJ”

JJ

Beau Beau Beau JJ JJ Beau

0 1 2 3 4 5 6
Remove ALL Occurrences of “JJ”

Beau JJ Beau Beau JJ JJ JJ Beau

0 1 2 3 4 5 6 7

Given the ArrayList (ourDogs) of Strings, remove all elements containing the String “JJ”

Beau Beau Beau JJ JJ Beau

0 1 2 3 4 5
Remove ALL Occurrences of “JJ”

Beau JJ Beau Beau JJ JJ JJ Beau

0 1 2 3 4 5 6 7

Given the ArrayList (ourDogs) of Strings, remove all elements containing the String “JJ”

JJ

Beau Beau Beau JJ Beau

0 1 2 3 4 5
Remove ALL Occurrences of “JJ”

Beau JJ Beau Beau JJ JJ JJ Beau

0 1 2 3 4 5 6 7

Given the ArrayList (ourDogs) of Strings, remove all elements containing the String “JJ”

Beau Beau Beau JJ Beau

0 1 2 3 4
Remove ALL Occurrences of “JJ”

Beau JJ Beau Beau JJ JJ JJ Beau

0 1 2 3 4 5 6 7

Given the ArrayList (ourDogs) of Strings, remove all elements containing the String “JJ”

Beau Beau Beau JJ Beau

0 1 2 3 4
Remove ALL Occurrences of “JJ”

Beau JJ Beau Beau JJ JJ JJ Beau

0 1 2 3 4 5 6 7
Index
Given the ArrayList (ourDogs) of Strings, remove all elements containing the String “JJ” 0
1
for (int index = 0; index < ourDogs.size(); index++){
2
if (ourDogs.get(index).equals(“JJ”){
3
ourDogs.remove(index);
4
}
5
}
Remove ALL Occurrences of “JJ”

Beau JJ Beau Beau JJ JJ JJ Beau

0 1 2 3 4 5 6 7
Index
Given the ArrayList (ourDogs) of Strings, remove all elements containing the String “JJ” 0
1
for (int index = 0; index < ourDogs.size(); index++){
2
if (ourDogs.get(index).equals(“JJ”){
3
ourDogs.remove(index);
4
index--;
5
}
}

Beau Beau Beau Beau

0 1 2 3
Remove ALL Occurrences of “JJ”

Beau JJ Beau Beau JJ JJ JJ Beau

0 1 2 3 4 5 6 7
Index
Given the ArrayList (ourDogs) of Strings, remove all elements containing the String “JJ”
0
int index = 0;
1
while (index < target.size()){
2
if (target.get(index).equals("JJ")){
3
target.remove(index);
4
}else{
5
index++;}
}

Beau Beau Beau Beau

0 1 2 3
Lab time: findLargest(), doubleVal()
● findLargest(ArrayList<Integer> ):

○ Return the largest value of an integer ArrayList

● doubleVal(ArrayList<Integer>):

○ Return an ArrayList with all elements doubled: [1, 3, 4] becomes [2, 6, 8]


public static int findLargest(ArrayList<Integer> arl){
int max = 0;
for (int i = 0; i<arl.size(); i++){
if (max < arl.get(i))
max = arl.get(i);
}
return max;
}

public static int findLargestE(ArrayList<Integer> arl){


int max = 0;
for (int num: arl){
if (num > max)
max = num;
}
return max;
}
Lab time: findLargest(), doubleVal()
● findLargest(ArrayList<Integer> ):

○ Return the largest value of an integer ArrayList

● doubleVal(ArrayList<Integer>):

○ Return an ArrayList with all elements doubled: [1, 3, 4] becomes [2, 6, 8]


public static ArrayList<Integer> doubleVal(ArrayList<Integer>
arl){
for (int i = 0; i<arl.size(); i++){
arl.set(i, arl.get(i)*2);
}
return arl;
}

public static ArrayList<Integer> doubleValE(ArrayList<Integer>


arl){
ArrayList<Integer> results = new ArrayList<Integer>();
for (int num : arl){
num *= 2;
results.add(num);
}
return results;
}
UNIT 7 - ARRAYLIST
7.1 - INTRODUCTION TO ARRAYLIST
7.2 - ARRAYLIST METHODS
7.3 - TRAVERSING ARRAYLISTS
7.4 - DEVELOPING ALGORITHMS USING ARRAYLISTS
7.5 - SEARCHING
7.6 - SORTING
7.7 - ETHICAL ISSUES AROUND DATA COLLECTION
For you to practice in ArrayList….
• Insert elements
• Delete elements
• Determine the minimum or maximum value
• Compute a sum, average, or mode of elements
• Search for a particular element in the array
• Determine if at least one element has a particular property
• Determine if all elements have a particular property
• Access all consecutive pairs of elements
• Determine the presence or absence of duplicate elements
• Determine the number of elements meeting specific criteria
• Shift or rotate elements left or right
• Reverse the order of the elements

You might also like