Unit 7 Array Lists Collegeboard
- a reference type
- mutatble and contains
- For loops
- While loops
- Enhanced for loops
- for each loops
- using arrary lists with doubles
- Searching
- the locating of data w/ linear structures
- ie: arrays, lists, que, stack
- involves control structures
- Sorting
- ascending and descending order
import java.util.ArrayList;
public class ArrayListInit
{
public static void main(String[] args)
{
//ArrayLists can only be initialized with Object data types.
// This will cause an error because we are attempting to create an ArrayList with primitive type int.
ArrayList<int> array = new ArrayList<int>();
//Comment out the code above, and uncomment the code below to create a working ArrayList!
// ArrayList<Integer> array = new ArrayList<Integer>();
//We can also create an ArrayList without specifying the type
//This is NOT RECOMMENDED because any errors made to the type will NOT be
//discovered until after runtime.
ArrayList arrayWithoutAType = new ArrayList();
System.out.println("Congrats! You created an ArrayList!");
}
}
//descending order and swapping first and last
import java.util.ArrayList;
import java.util.Collections;
// Initializing an ArrayList filled with integers
ArrayList<Integer> integers = new ArrayList<>(Arrays.asList(37,64,92,84,1));
Collections.sort(integers);
Collections.reverse(integers);
System.out.println("Reverse order: " + integers);
public static void swap(){
Integer temp = numbers.get(0);
int last_index = integers.size()-1;
integers.set(0, integers.get(last_index));
integers.set(last_index, temp);
System.out.println("swapped the first and last numbers " + integers);
}
swap()
//hashcode
import java.util.ArrayList;
import java.util.Collections;
// Initializing an ArrayList filled with integers
ArrayList<Integer> integers = new ArrayList<>(Arrays.asList(37,64,92,84,1));
Collections.sort(integers);
System.out.println("hash code: " + integers.hashCode());