Unit 7 Homework
import java.util.ArrayList;
import java.util.Collections;
ArrayList<String> songs = new ArrayList<>();
songs.add("Shake It Off");
songs.add("In Da Club");
songs.add("Party In The USA");
songs.add("I Ain't Worried");
songs.add("One Dance");
songs.add("I Wanna Benz");
public void print(ArrayList<String> songs){
for (String str : songs){
System.out.println(str);
}
}
// shows descending order
System.out.println("------------------");
Collections.sort(songs, Collections.reverseOrder());
System.out.println("Song's Descending Order: ");
print(songs);
System.out.println();
System.out.println("------------------");
// swaps 1st and 6th list items
System.out.println("Swap 1st & Last Element: ");
String temp = songs.get(0);
songs.remove(0);
songs.add(0,songs.get(songs.size()-1));
songs.remove(songs.size()-1);
songs.add(songs.size(), temp);
print(songs);
import java.util.ArrayList;
public class ArrayChecker {
// Here is the code to check if array list is sorted in Ascending order or not
boolean checkSorting(ArrayList< String > arraylist){
boolean isSorted=true;
for(int i=1;i < arraylist.size();i++){
if(arraylist.get(i-1).compareTo(arraylist.get(i)) > 0){
isSorted= false;
break;
}
}
return isSorted;
}
public static void main(String[] args) {
ArrayList < String > al=new ArrayList < String >();
al.add("1, 2, 3, 4, 5, 6");
System.out.println(al);
ArrayChecker alDemo=new ArrayChecker();
System.out.println("Is List Ascending: "+alDemo.checkSorting(al));
al.remove("1, 2, 3, 4, 5, 6");
al.add("6, 5, 4, 3, 2, 1");
System.out.println(al);
ArrayChecker alDemo1=new ArrayChecker();
System.out.println("Is List Descending: "+alDemo1.checkSorting(al));
al.remove("6, 5, 4, 3, 2, 1");
}
}
ArrayChecker.main(null);