Arrays and Arraylists Study Guide

Ace your homework & exams now with Quizwiz!

What is the max index of the following array: int[] data = {12, 34, 9, 0, -62, 88};

5

What is the length of the following array: int[] data = {12, 34, 9, 0, -62, 88};

6

ArrayList<String> list = new ArrayList<String>(10) ; list.add( "Joe" ); list.add( "Anne" ); list.add( "Suzy" ); list.add( 0, "Timothy" ); What element will be at index 2 of the list?

"Anne"

Initializing Arrays with empty data values

(type)[] (name) = new (type)[(size)]; int[] wholeNums = new int[25];

Initializing Arrays with data

(type)[] (name) = new (type){data, data, data}; String[] fNames = new String{"Bob","Jim","Ann"}; -the Array is created with its size fixed at the amount of data initialized. -For example, the size of the array above is 3

Is the following an initialization for an arrayList or an array? int[] numberList = new int[7];

Array

Declare and construct an ArrayList with an initial capacity of 20 references to Object.

ArrayList<Object> list = new ArrayList<Object>(20);

Initializing ArrayList with empty data values

ArrayList<objectType> (name) = new ArrayList<objectType>(int); -size of ArrayList depends on integer in parentheses -by default, if an integer is not put in parentheses, it creates an ArrayList with an initial capacity of 10

Differences between Array and ArrayList classes: Resizable

ArrayLists are resizable, meaning that the number of indexes they have can change, whereas the size of an array is constant.

Differences between Array and ArrayList classes: Performance

Arrays are much more efficient than arrayLists. Therefore, when dealing with large quantities of data, it is often more efficient to uses arrays.

Differences between Array and ArrayList classes: Multi-Dimensional

Arrays can be multi-dimensional, meaning 2D, 3D, and further arrays can be created whereas arrayLists are only one-dimensional.

Similarities between Array and ArrayList classes: Passed by reference

Both arrays and arrayLists are passed by reference. This means that when they are passed to a method, any changes made to the variable which they are passed will change the original object.

Bubble Sort

Bubble sort is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. Passes through the list are repeated until no swaps are needed, which indicates that the list is sorted.

How do you access an array element?

By the name of the array followed by the index value in brackets.

Example of a common error: IndexOutOfBoundsException

IndexOutOfBounds exception will occur if the programmer tries to access a value at the index of the array or arrayList's max length/size. This is because the max index is the max length/size-1. Indexing starts at zero whereas length/size starts at 1.

Insertion Sort

Insertion sort. Insertion sort is a brute-force sorting algorithm that is based on a simple method that people often use to arrange hands of playing cards: Consider the cards one at a time and insert each into its proper place among those already considered

What is the wrapper class for int?

Integer

Does a programmer always know how long an array will be when the program is being written?

No, the array object is created when the program is running, and the length might change from run to run.

Differences between Array and ArrayList classes: Data Storing

Only objects can be stored in arrayLists (when primatives are used they are converted to equivalent objects) whereas both objects and primatives can be stored in arrays.

What field returns the number of elements in an array?

The length field. It is an instance variable of the array.

Selection Sort

The selection sort algorithm divides the input list into two parts- the sublist of items already sorted, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, swapping it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.

What is auto-boxing?

When the conversion between primitive types and corresponding wrapper classes is automatic. Occurs when primatives are put in ArrayLists.

Using the Array class: Setting/changing Values

array[index] = value; array[row][col] = value;

Using the Array class: Accessing Values

data = array[index]; 2DData = array[row][col];

Example of Binary Search

int[] data; int size; public boolean binarySearch(int key) { int low = 0; int high = size - 1; while(high >= low) { int middle = (low + high) / 2; if(data[middle] == key) { return true; } if(data[middle] < key) { low = middle + 1; } if(data[middle] > key) { high = middle - 1; } } return false; }

Accessing all values in an array: For-each loops

int[] values = new int[11] {....}; for(int k : values){ //some more code } -For each integer k in values... -Loops through each int in array values

Initializing and declaring multi-dimensional arrays

int[][] example = new int[][]{ {2,45,6,76,23,4,2,100} {67,87,99,87,32,1,23,3} {4,56,8,76,4,77,90,34} }

What do you have to import to use the ArrayList class?

java.util.ArrayList;

Using the ArrayList class: Adding values to end of list

name.add(element); Example: animals.add("pig"); -"pig" is added to the end of the ArrayList animals

Using the ArrayList class: Adding values to list at specified index

name.add(index, element); Example: animals.add(5, "pig"); -"pig" is added to the ArrayList animals at index 5.

Using the ArrayList class: Returning value at specified index

name.get(index); Example: value = animals.get(5); -value is now equal to data at index 5 in the animals ArrayList. value now equals "pig."

Using the ArrayList class: Removing value at specified index

name.remove(index); Example: animals.remove(5); -the value at index 5 is now removed. "goat" is removed from the animals ArrayList.

Using the ArrayList class: Changing values in an ArrayList

name.set(index, value); Example: animals.set(5, "goat"); -the value at index 5 in the animals ArrayList is now changed to the parameter value. "pig" is changed to "goat"

What is the last index of an arrayList always equal to?

name.size()-1

Using the ArrayList class: Returning number of elements in ArrayList

name.size(); Example: animals.size(); -returns the number of indexes in the animals ArrayList

Example of Bubble Sort

public class BubbleSortExample { static void bubbleSort(int[] arr) { int n = arr.length; int temp = 0; for(int i=0; i < n; i++){ for(int j=1; j < (n-i); j++){ if(arr[j-1] > arr[j]){ //swap elements temp = arr[j-1]; arr[j-1] = arr[j]; arr[j] = temp; } } }

Example of Selection Sort

public static void selectionSort1(int[] x) { for (int i=0; i<x.length-1; i++) { for (int j=i+1; j<x.length; j++) { if (x[i] > x[j]) { //Exchange elements int temp = x[i]; x[i] = x[j]; x[j] = temp; } } } }

Example of Insertion Sort

public static void sort(String[] a) { int n = a.length; for (int i = 1; i < n; i++) { for (int j = i; j > 0; j--) { if (a[j-1].compareTo(a[j]) > 0) exch(a, j, j-1); else break; } } }

Using the Array class: Return size of 2D Array

rowSize = array.length columnSize = array[].length

Using the Array class: Return size of array

size = array.length

ArrayList<String> list = new ArrayList<String>(10) ; list.add( "Tim" ); list.add( "Suzy" ); list.add( "Joe" ); After the code has executed, what is the capacity of list? What is its size?

10, 3

Additional Similarities between Array and ArrayList classes

-Both array and ArrayList can have duplicate elements in them. -Both are unordered lists. -Both uses index to refer to their elements. -Both supports null values

Arraylist

-Ordered collection of objects -Size is variable/can be changed -in order to be used the java.util.ArrayList class must be imported

Array

-Ordered collection of primitive data types or objects -Size is fixed

Two-Dimensional Array

-Similar to normal arrays, but with two indices -When creating 2D arrays with data, the data for each row is imbedded in each column. -int[][] wholeNums = new int[25][5]; -int[][] abc = new int{{2,1},{4,8},{9,6},{3,2}};

Indexing for Arrays and Arraylists

-Starts at 0 -Increments by 1 until end of Array/Arraylist

How do you return int types from ArrayLists?

-Use arrayName.get(index).intValue(); -returns value as primative type

Accessing each element in 2D Arrays

-Use embedded loops for(int row=0; row<array.length; row++){ for(int col=0; col<array[].length; col++){ array[row][col]; } }

Binary Search

-requires a sorted array -Uses an upper extreme, lower extreme, and midpoint -if value is greater than midpoint, the lower extreme is set to the midpoint and a new midpoint is found if value is less than midpoint, the upper extreme is set to the midpoint and a new midpoint is found -this is done until the expected data value is found.

What is the index value of the first element?

0

What are searching algorithms?

Searching algorithms are code used to find a specified value in an array or arraylist.


Related study sets

Intro to health science and medicine test 1

View Set

Acute Poststreptococcal Glomerulonephritis

View Set

Disclosure of Health Information

View Set

American National Government We The People Final Review

View Set

PrQ27: Practice Quiz - Ch. 27: The Wealth of Nations and Economic Growth

View Set

HMD 220 Module 1 Quiz(Designing Experiences Chapters 1 and 2

View Set

Chapter 4 Questions Lodging Operations

View Set