Unit 7 Array Lists
for(String name:roster){}
Developing Algorithms with Array Lists
- arraylist can change size so a lot more versatile than an array
Searching
- data: arrays, lists, queue, stack, etc
- Loop with an if statement inside, going through each index and checking if a certain condition is met
- Order matters occasionally
Sorting
- Sorting can work in either ascending or descending order
import java.util.ArrayList;
import java.util.Collections;
ArrayList<String> countries = new ArrayList<>();
countries.add("USA");
countries.add("China");
countries.add("Russia");
countries.add("France");
public void print(ArrayList<String> countries){
for (String str : countries){
System.out.println(str);
}
}
Collections.sort(countries, Collections.reverseOrder());
System.out.println("Reverse: ");
print(countries);
System.out.println("----------");
String tmp = countries.get(0);
countries.set(0, countries.get(countries.size()-1));
countries.set(countries.size()-1, tmp);
print(countries);