text: Arrays
  Introduction to Arrays
  We:That is a lot of ungainly repeated code. What if we had 100 lottery numbers? It is more clean and convenient to use a Java array to stroe the data as a list.
  An array hold a fixed number of values of one type. Arrays hold
  doubles,
  ints,
  booleans, or any other primitives. Arrays can also contains
  Strings as well as object references!
  Each index of an array corresponds with a different value. Here is a diagram of an array filled with integer values:
  Similar to C and Python, the indexes start at 0! The element at index 0 is
  4, while the element at index 1 is
  8. This array has a length of 5, since it holds five elements, but the highest index of the array is 4.
  Creating an Array Explicitly
  Imagine that we're using a program to keep track of the prices of different items we want to buy. We would want a list of the prices and a list of the items they correspond to. To create an array, we first declare the type of data it holds.
  double[] prices;
  prices = {13.15, 15.87, 14.22, 16.55};
  double[] prices = {13.15, 15.87, 14.22, 16.55};
  Strings and other objects as well as primitives:
  String[] clothingItems = {"Tank Top", "Beanie", "Funny Socks", "Pants"};
  Importing Arrays
  If we want to have a descriptive printout of an array, we need a
  toString() method that is provided by the
  Arrays package in Java.
  import java.util.Arrays;
  When we import a package in Java, we are making all of the methods of that package available in our code.
  The
  Arrays package has many useful methods, including
  Arrays.toString(). When we pass an array into
  Arrays.toString(), we can see the contents of the array printed out.
  [4, 8, 12, 16, 20]
  Get Element By Index
  Now that we have an array declared and intitialized, we want to be able to get values out of it.
  We use square brackets
  [] to access data at a certain index:
  15.87.
  This happens because
  15.87 is the item at the
  1 index of the array.
  If we try to access an element outside of its appropriate index range, we will receive an
  ArrayIndexOutOfBoundsException error.
  For example, if we were to run the command
  System.out.println(prices[5]), we would get the following output:
  java.lang.ArrayIndexOutOfBoundsException: 5
  Creating an Empty Array
  We can also create empty arrays and then fill the items one by one. Empty arrays have to be intitialized with a fixed size:
  String[] menuItems = new String[5];
  5.
  After declaring and intializing, we can set each index of the array to be a different item:This group of commands has the same effect as assigning the entire array at once: We can also change an item after it has been assigned! Let's say this restaurant is changing its broccoli dish to a cauliflower one:
  menuItems[3] = "Baked cauliflower";
  ["Veggie hot dog", "Potato salad", "Cornbread", "Baked cauliflower", "Coffee ice cream"]
  Array Length
  What if we have an array storing all the usernames for our program, and we want to quickly see how many users we have? To get the length of an array, we can access the
  length field of the array object:
  5, since the
  menuItemsarray has
  5slots, even though they are all empty.
  If we print out the length of the
  prices array:
  4, since there are four items in the
  pricesarray!
  String[] args
  When we write
  main() methods for our programs, we use the parameter
  String[] args. Now that we know about array syntax, we can start to parse what that means.
  A
  String[] is an array made up of
  Strings. Examples of
  String arrays:
  argsparameter is another example of a
  Stringarray. In this case, the array
  argscontains the arguments that we pass in from the terminal when we run the class file. (So far
  argshas been empty.)
  So how can you pass arguments to
  main()> Let's say we have this class
  HelloYou:
  HelloYouin the terminal with an argument of
  "Laura":
  java HelloYou Laura
  Hello Laura
  String[] argswould be interpreted as an array with one element,
  "Laura". When we use
  args[0]in the main method, we can access that element like we did in
  HelloYou.
  We can actually create
  if-else statements that run based on the input:
  Arrays Review
  We have now seen how to store a list of values in an array. We can use this knowledge to make organized programs with more complex variables.
  Throughout these notes, we have leared about:
  Creating arrays explicitly, using
  {and
  }.
  Accessing an index of an array, using
  [and
  ].
  Creating empty arrays of a certain size, and filling the indices one by one.
  Getting the length of an array using
  .length.
  Using the argument
  argsthat is passed into the
  main()method of a class.
  Let's create a small program that holds student names and test scores with the following characteristics:
  Make an array of strings called
  studentswith the following names: Sade, Alexus, Sam, Koma.
  Create an empty array of
  doubles called
  mathScoresof size 4.
  Sade got a 94.5 on the test. Store this value in the same indice that she is listed in the
  studentsarray.
  Sam got a 76.8. Store this value in the appropriate spot in the
  mathScoresarray.
  Finally, add a print statement that says: "The number of students in the class is
  numStudents." using the
  .lengthoperator.
  Introduction to ArrayLists
  When we work with arrays in Java, we've been limited by the fact that once an array is created, it has a fixed size. We can't add or remove elements.
  But what if we needed to add to the book lists, newsfeeds, and other structures we were using arrays to represent?
  To create multiple and dynamic lists, we can use Java's
  ArrayLists.
  ArrayLists allow us to:
  Store object references as elements
  Store elements of the same type (just like arrays)
  Access elements by index (just like arrays)
  Add elements
  Remove elements
  Remember how we had to import
  java.util.Arrays in order to use additional array methods? To use an
  ArrayList at all, we need to import them from Java's
  util package as well:
  import java.util.ArrayList;
  Creating ArrayLists
  To create an
  ArrayList, we need to declare the type of object it will hold, just as we do with arrays:
  ArrayList<String> babyNames;
  <>) to declare the type of the
  ArrayList. These symbols are used for generics. Generics are a Java construct that allows us to define classes and objects as parameters of an
  ArrayList. For this reason, we can't use primitive types in an
  ArrayList: The
  <Integer>generic has to be used in an
  ArrayListinstead. You can also use
  <Double>and
  <Char>for types you would normally declare as
  doubles or
  chars.
  We can intialize to an empty
  ArrayList using the
  new keyword:
  Adding Items to an ArrayList
  Now we have an empty
  ArrayList, but how do we get it to store values?
  ArrayList comes with an
  add() method which inserts an element into the structure. There are two ways we can use
  add().
  If we want to add an element to the end of the
  ArrayList, we'll call
  add() using only one arguement that represents the value we are inserting. In this example, we'll add objects from the
  Car class to an
  ArrayList called
  carShow:
  ArrayList, we'll need two arguments in our method call: the first arguement will define the index of the new element while the second argument defines the value of the new element: By inserting a value at a specified index, any elements that appear after this new element will have their index shift over by 1.
  Also, note that an error will occur if we try to insert a value at an index that does not exist.
  You are able to add multiple data types to the same array using
  add():
  ArrayListwill be considered
  Objects. As a result, they won’t have access to some of their methods without doing some fancy casting. Although this type of
  ArrayListis allowed, using an
  ArrayListthat specifies its type is preferred.
  ArrayList Size
  Let's say we have an
  ArrayList that stores items in a user's online shopping cart. As the user navigates through the site and adds items, their cart grows bigger and bigger.
  If we wanted to display the number of items in the cart, we could find the size of it using the
  size() method:
  ArrayLists, it's important to know how to access the amount of objects we have stored.
  Accessing an Index
  With arrays, we can use bracket notation to access a value at a particular index:This code prints
  5.5, the value at index
  1of the array.
  For
  ArrayLists, bracket notation won't work. Instead we use the method
  get() to access an index:
  "HK-416 Assault Rifle", which is the value at index 2 of the
  ArrayList.
  Changing a Value
  When we were using arrays, we could rewrite entries by using bracket notation to reassign values:
  ArrayListhas a slightly different way of doing this, using the
  set()method:
  Removing an Item
  What if we wanted to get rid of an entry alltogether? For arrays, we would have to make a completely new array without the value.
  Luckily,
  ArrayLists allow us to remove an item by specifying the index to remove:
  Getting an Item's Index
  What if we had a really large list and wanted to know the position of a certain element in it? For instance, what if we had an
  ArrayList
  detectives with the names of fictional detectives in chronological order, and we wanted to know what position
  "Flecther" was.
  4, since
  "Fletcher"is at index
  4of the
  detectives
  ArrayList.
  ArrayLists Review
  Some crucial methods in
  ArrayLists:
  Adding a new ArrayList item using
  add().
  Accessing the size of an ArrayList using
  size().
  Finding an item by index using
  get().
  Changing the value of an ArrayList item using
  set().
  Removing an item with a specific value using
  remove().
  Retrieving the index of an item with a specific value using
  indexOf().