Unit 07 Arraylists
Documentation of Arraylists
- Accessing Elements
- Modifying Elements Inside an Arraylist
- Removing and Clearing Elements
- Arraylist Searching Methods
- Sorting Elements of Arraylist
- Homework
First you have to import the java arraylist. This is done by:
import java.util.ArrayList;
Then you can create the arraylist
One of the best things about arraylists is that you can add and remove elements from the arraylist after you initialize it. This means that the length of the arraylist can change after you create it, unlike in arrays.
import java.util.ArrayList; // import the ArrayList class
ArrayList<String> names = new ArrayList<String>();
names.add("Aadit");//adding values to list
names.add("Adi");
names.add("Soma");
names.add("Tianbin");
System.out.println("Number of elements: " +names.size());//prints the number of elements in the arraylist
for(int i = 0;i<names.size();i++){
System.out.println(names.get(i));//printing all values in list using for loop
}
ArrayList<Integer> nums = new ArrayList<Integer>();//making list
for(int i = 0;i<10;i++){
nums.add(i);//adding value
}
for(int i = 0;i<nums.size();i++){
nums.set(i,i*i);//cool way of squaring all the values.
System.out.println(nums.get(i));//printing these values
}
Modifying Elements Inside an Arraylist
As shown above in the code block, to modify or change code from elements that have already been set, use the set() method. The set method takes two parameters. The first parameter is the index that you are trying to modify. The second parameter is the value that you are giving the index in the first parameter
//Examples:
nums.set(0,100);
names.set(1,"John");
letters.set(3,'D');
ArrayList<Integer> nums = new ArrayList<Integer>();
for(int i = 0;i<10;i++){
nums.add(i);
}
System.out.println(nums.size());
nums.remove(8);
System.out.println(nums.size());//new size after removing value
System.out.println(names.isEmpty());//checks if list is empty after
nums.clear();//clears list
System.out.println(nums.size());
System.out.println(nums.isEmpty());//list is now empty
Arraylist Searching Methods
You can also use methods to search through arraylists and find certain elements. The indexOf() method returns the first occurrence of the given element or -1 if it doesn't exist. The lastIndexOf() returns the last occurrence of the element or -1 if it's not in the list. Another method that searches through the list is the contains() method.
ArrayList<String> names = new ArrayList<String>();//creating list
names.add("Aadit");//adding elements to list
names.add("Adi");
names.add("Soma");
names.add("Tianbin");
names.add("John");
names.add("Aadit");
//testing indexOf() and lastIndexOf()
System.out.println("Testing indexOf() and lastIndexOf() ");
System.out.println(names.indexOf("Aadit"));
System.out.println(names.lastIndexOf("Aadit"));
System.out.println(names.indexOf("Billy"));
//testing contains
System.out.println("Testing contains");
System.out.println(names.contains("Aadit"));
System.out.println(names.contains("Billy"));
ArrayList<Integer> nums = new ArrayList<Integer>();//creating arraylsit
nums.add(1);//adding elements to array
nums.add(6);
nums.add(8);
nums.add(4);
nums.add(36);
nums.add(2);
nums.add(89);
nums.add(100);
nums.add(4);
System.out.println("Unsorted Arraylist");
for(int i = 0;i<nums.size();i++){//printing unsorted elements
System.out.print(nums.get(i)+" ");
}
//bubble sort algorithm
boolean isInOrder = false;
while (isInOrder == false) {
isInOrder = true;
for (int i = 0; i < nums.size() - 1; i++) {
if (nums.get(i + 1) < nums.get(i)) {
int temp = nums.get(i);
nums.set(i,nums.get(i + 1));
nums.set(i+1,temp);
isInOrder = false;
}
}
}
System.out.println("\nSorted Arraylist");
for(int i = 0;i<nums.size();i++){//printing arraylist again, but sorted this time
System.out.print(nums.get(i)+" ");
}
import java.util.Random;
import java.util.ArrayList; // import the ArrayList class
public class HomeWork1{
ArrayList<Integer> numbers = new ArrayList<Integer>();
Random rand = new Random();
void run(){
for(int i = 0; i<10;i++){
int x = rand.nextInt(100);
numbers.add(x);
}
//descending order then reverse first and last element
System.out.println("Before operations: "+ numbers);
Collections.sort(numbers, Collections.reverseOrder());
int temp = numbers.get(0);
numbers.set(0,numbers.get(numbers.size()-1));
numbers.set(numbers.size()-1,temp);
System.out.println("After operation: "+ numbers);
}
public static void main(String[] args) {
HomeWork1 hw1 = new HomeWork1();
hw1.run();
}
}
HomeWork1.main(null);
import java.util.Random;
import java.util.ArrayList; // import the ArrayList class
public class HomeWork2{
ArrayList<Integer> numbers = new ArrayList<Integer>();
Random rand = new Random();
void run(){
for(int i = 0; i<10;i++){
int x = rand.nextInt(100);
numbers.add(x);
}
//hashmaps before and after sort
System.out.println("Hash map before operation: "+ numbers.hashCode());
Collections.sort(numbers);
System.out.println("Hash map after operation: "+ numbers.hashCode());
}
public static void main(String[] args) {
HomeWork2 hw2 = new HomeWork2();
hw2.run();
}
}
HomeWork2.main(null);
import java.util.Random;
import java.util.ArrayList; // import the ArrayList class
public class HomeWork3{
ArrayList<Integer> numbers1 = new ArrayList<Integer>();
ArrayList<Integer> numbers2 = new ArrayList<Integer>();
Random rand = new Random();
void run(){
//adding numbers to array 1
for(int i = 0; i<10;i++){
int x = rand.nextInt(100);
numbers1.add(x);
}
//adding numbers to array 2
for(int i = 0; i<10;i++){
int x = rand.nextInt(100);
numbers2.add(x);
}
//replace three elements, then reverse order
System.out.println("Before operations, Arraylist 1: "+ numbers1);
System.out.println("Before operations, Arraylist 2: "+ numbers2);
//randomly swap elements from lists three times
for(int i = 0; i<3;i++){
int replace = rand.nextInt(10);
numbers1.set(replace,numbers2.get(replace));
}
//reverse order of array 1
Collections.sort(numbers1, Collections.reverseOrder());
System.out.println("After operations, Arraylist 1: "+ numbers1);
System.out.println("After operations, Arraylist 2: "+ numbers2);
}
public static void main(String[] args) {
HomeWork3 hw3 = new HomeWork3();
hw3.run();
}
}
HomeWork3.main(null);