List and ArrayLists
Lists and ArrayLists Week 7
Show usage of Lists interfaces in a Jupyter Notebook example. Try using List interface and making a ArrayList object. Consider using an example that will be part of interest or final projects. This will probably become your favorite Data Structure in Java.
//declaring List interface
//public interface List<E> extends Collection<E>;
//ArrayList (object of List interface)
//creates an integer arraylist
List<Integer> l1 = new ArrayList<Integer>();
import java.util.ArrayList;
ArrayList<String> dnMarketItems = new ArrayList<String>();
add(int index, element) This method is used to add an element at a particular index in the list. When a single parameter is passed, it simply adds the element at the end of the list.
dnMarketItems.add("AP Physics C Baron's Prep Book");
dnMarketItems.add("TI Inspire Calculator");
dnMarketItems.add("PE Lock");
addAll(int index, Collection collection) This method is used to add all the elements in the given collection to the list. When a single parameter is passed, it adds all the elements of the given collection at the end of the list.
ArrayList<String> dnMeenaListings = new ArrayList<String>();
dnMeenaListings.add("AP Calc BC Baron's Prep Book");
dnMeenaListings.add("Calc Tutoring 1 hr sessions");
dnMarketItems.addAll(dnMeenaListings);
System.out.println("Del Norte Market Listings: " + dnMarketItems + "\n");
System.out.println("Meena's Listings: " + dnMeenaListings);
size() This method is used to return the size of the list.
System.out.println("The number of listings in DN Marketplace is currently " + dnMarketItems.size());
clear() This method is used to remove all the elements in the list. However, the reference of the list created is still stored.
dnMeenaListings.clear();
System.out.println("Meena's Listings: " + dnMeenaListings);
remove(int index) This method removes an element from the specified index. It shifts subsequent elements(if any) to left and decreases their indexes by 1.
// deletes the first item on the list (AP Physics C Prep Book)
dnMarketItems.remove(0);
System.out.println("Del Norte Market Listings: " + dnMarketItems);
remove(element) This method is used to remove the first occurrence of the given element in the list.
dnMarketItems.add("AP Physics C Baron's Prep Book");
// removes PE Lock
dnMarketItems.remove("PE Lock");
System.out.println("Del Norte Market Listings: " + dnMarketItems);
get(int index) This method returns elements at the specified index.
System.out.println("The first item listed is " + dnMarketItems.get(0));
set(int index, element) This method replaces elements at a given index with the new element. This function returns the element which was just replaced by a new element.
// replace the first term (index of 0)
dnMarketItems.set(0, "Used TI Inspire Calculator");
System.out.println("Del Norte Market Listings: " + dnMarketItems);
indexOf(element) This method returns the first occurrence of the given element or -1 if the element is not present in the list.
System.out.println("The index for calc tutoring sessions is " + dnMarketItems.indexOf("Calc Tutoring 1 hr sessions"));
lastIndexOf(element) This method returns the last occurrence of the given element or -1 if the element is not present in the list.
// prints negative 1 since the item is not in the list
System.out.println("The index for physics tutoring sessions is " + dnMarketItems.indexOf("Physics Tutoring 1 hr sessions"));
equals(element) This method is used to compare the equality of the given element with the elements of the list.
ArrayList<String> dnMarketInventory = new ArrayList<String>();
dnMarketInventory.add("Used TI Inspire Calculator");
dnMarketInventory.add("AP Calc BC Baron's Prep Book");
dnMarketInventory.add("Calc Tutoring 1 hr sessions");
if (dnMarketItems.equals(dnMarketInventory) == true) {
System.out.println("The inventory matches the listings!");
}
else {
System.out.println("The inventory does not match the listings!");
}
hashCode() This method is used to return the hashcode value of the given list.
System.out.println("The hashcode for this list is " + dnMarketItems.hashCode());
isEmpty() This method is used to check if the list is empty or not. It returns true if the list is empty, else false.
System.out.println("Meena's listings is empty is " + dnMeenaListings.isEmpty());
contains(element) This method is used to check if the list contains the given element or not. It returns true if the list contains the element.
System.out.println("DN Market Items contains PE Lock is " + dnMarketItems.contains("PE Lock"));
containsAll(Collection collection) This method is used to check if the list contains all the collection of elements.
if (dnMarketItems.containsAll(dnMarketInventory) == true) {
System.out.println("The inventory has at least everything in the listings!");
}
else {
System.out.println("The inventory does not have everything the listings!");
}
sort(Comparator comp) This method is used to sort the elements of the list on the basis of the given comparator.
Collections.sort(dnMarketItems);
System.out.println("Sorted DN Market items " + dnMarketItems);
public class Song
{
/** Verse - prints out a verse of the song
* @param number - a String like "one", "two", etc.
* @param rhyme - a String like "thumb", "shoe", etc.
*/
public void verse(String number, String rhyme)
{
System.out.println("This old man, he played " + number);
System.out.println("He played knick knack on my " + rhyme);
}
// The chorus method
public void chorus()
{
System.out.println("With a knick knack paddy whack, give a dog a bone.");
System.out.println("This old man came rolling home.");
}
public static void main(String args[])
{
Song mySong = new Song();
mySong.verse("one", "thumb");
mySong.chorus();
mySong.verse("two", "shoe");
mySong.chorus();
mySong.verse("three", "knee");
mySong.chorus();
mySong.verse("four", "door");
mySong.chorus();
}
}
Song.main(null);