Lists

  • Lists in java act as an interface to store and order objects and allow copies of values
  • Lists extend the collection class
  • Four subclasses
    - ArrayList
    - LinkedList
    - Vector
    - Stack
  • ArrayList and LinkedList are the most commonly used
public interface List<E> extends Collection<E>

ArrayLists

  • Size can be changed unlike with built-in arrays
import java.util.ArrayList; 
ArrayList<String> delNorteMarketplace = new ArrayList<String>();
  • Use the "add" command to add elements to the index
  • Use the "addAll" command to add all the elements listed to the index
delNorteMarketplace.add("Blue Homecoming Dress");
delNorteMarketplace.add("To Kill A Mockingbird book");
ArrayList<String> NewItems = new ArrayList<String>(); 

NewItems.add("Blue Homecoming Dress"); 
NewItems.add("To Kill A Mockingbird book"); 
delNorteMarketplace.addAll(NewItems);
  • To find how mny items are in the list utilize "size()"
  • To clear all the elements in the list utilize "clear()"
  • To remove the one of the index elements on the list usr "remove"
  • Use "get" to find a specific element in the list
System.out.println("Del Norte Marketplace has" + delNorteMarketplace.size() "items")
NewItems.clear();
dnMarketItems.remove(1); //removes To Kill A Mockingbird item
System.out.println("The first item listed is " + delNorteMarketplace.get(0));
  • "indexOf" returns the first occurance of the element
System.out.println("The dress index is" + delNorteMarketplace.indexOf("Blue Homecoming Dress"));
  • equals, shown by == shows if two items on a list are the same
  • "hasCode" is used to return the hashcode value of the list
System.out.println("The hashcode for Del Norte Marketplace is " + delNorteMarketplace.hashCode());
  • "isEmpty" is used to see if the particular list is empty
System.out.println("There are no new items" " + NewItems.isEmpty());
  • "contains" us ised to see of the list has a certain element
System.out.println("The Del Norte Marketplace site has shoes" + delNorteMarketplace.contains("shoes"));
  • "sort" is used to sort the elements in the list gtiven a condition
Collections.sort(delNorteMarketplace);
System.out.println("Sort" + delNorteMarketplace);