Arrays+and+ArrayLists

Please create a comprehensive table comparing Arrays and ArrayLists - work together on Friday in class to determine what areas need to be addressed (such as how to add an item, what type of data can be held). Then, over the weekend, Alena you fill out the section on ArrayLists and Kaitlin, you fill out the section on arrays. You will receive a homework grade for the thoroughness and correctness of your answers.

ex. String[] responses = new String[8]; || ArrayList name = new ArrayList ; ex. ArrayList scores = new ArrayList; || 3 ways to fill. 1. String[] responses= {//values} 2. String[] responses= new String[n] responses[0]=...; responses[1]=...; etc 3. Scanner in= new Scanner(System.in); String[] responses= new String[n]; for(int i; i<responses.length; i++) { responses[i]=// input from user;} || no each item must be added individually || ex. responses[1] || listname.get(index); || listname.add(index, item); || cannot be changed. To change/remove a value, you would just set responses[0] = new value; || listname.remove(index); Takes the value out and shortens the list. || ex. responses[0]= String temp; responses[1]= String temp2; responses[0]= temp2; responses[1]= temp; || Use listname.set(index, value). The old value at that index is removed and the new value is put in its place. The numer of slots does not change ex. int temp = listname.get(index1); listname.set(index1, listname.get(index2)); listname.set(index2, temp); ||
 * **Question** || **Array** || **ArrayList** ||
 * What type of data can be contained? || primitives and object || objects NO PRIMITIVES (Wrapper Classes let us get primitive-like data in ||
 * What must be imported before use? || nothing || java.util.* ||
 * How do you create? || arraytype[] name= new arraytype[size];
 * Can the list be filled at creation? || yes, it can be filled at creation
 * How do you know how big it is? || int x=responses.length || int x = listname.size; ||
 * How do you access an item in it || arrayname[indexslotnum]
 * How to you add an item to the it? || ex. responses[0]= //input;// || listname.add(item);
 * Can you add multiple items at once? || yes during creation if using the first filling method || no ||
 * How do you remove an item from the it? || after creation, the number of slots in an array
 * How do you replace one item with another? || Simply set the slot equal to a new value.
 * Can you change the size? || no, the total number of slots cannot be changed after creation of the array || the size is automatically changed as items are added or removed ||
 * Can you insert items in the middle? || no. You cannot insert slots, but you can change values in the existing slots. || yes

ex. listname.add(indexToInsertAt, value); ||

If we have ArrayList names = new ArrayList and it has been filled with values. The for loop that will print each of the values is:
 * For and For Each Loops**

for(int i = 0, i < names.size; i++) { System.out.println(names.get(i)); }

The for each loop that will print each of the values is:

for(String ahhh : names) { System.out.println(ahhh); }

What are the limitations of the for each loop:
 * Must iterate through all values of the Array or ArrayList.
 * Must start at index 0 and move through index size/length - 1.

//Linear Search//: Searches through a list from the beginning until it either finds the target value OR reaches the end of the list and returns -1.
 * Searches**

//Binary Search//: Depends on a list that is already sorted from smallest to largest. It starts in the middle and eliminates 1/2 of the list based on the target value and the midpoint value. It then finds the midpoint of the remaining portion of the list and repeats the process. It ends when either the value is found at the midpoint OR the entire list has been checked and the value is not present (returns -1).


 * Sorts**


 * Bubble -** Short code. Uses two loops. Knows that the list is sorted when it has for looped through the whole list and has not needed to switch any values.


 * Selection-** Middle-length code.

Insertion - Longest code. Creates a second array.