Arrays are objects. Created using the new operator. Arrays are not primitive data types.
Once created, all values of strings are set to null and int types are 0.
Int[] scores = new int [10];
Student[] asuStud = new Student[60000];
Scores[2] = 85; instantiating index 2 if the array.
asuStud[1] = new Student(“Jill”, 3.15, 16); (name, gpa, num of courses)
Enlarging the number of students without losing the original students.
Student[] asu Stud2 = new Student[70000];
For(int i=0; i<asuStud.length; i++)
{
asuStud2[i] = asuStud[i];
}
Instantiating of arrays are 2 processes. New and instantiating.
Default value for booleans is false.
Default for characters are 0.
Private char [] charArray; (instantiation of an array in a class)
Public Char Collection (int size)
{
charArray = new char [size];
}
static – belongs to the class, will not be used outside the class.
Public int[] myMethod(int a)
{
}
int[] b = object.myMethod(x[6]); (calling myMethod)
Each object stored in an array must be instantiated SEPARATELY.
Int[][] a = new int [row][column];
SEARCHING AND SORTING
Sorting is the process of arranging a list of items in a particular order.
There are many algorithms, which vary in efficiency, for sorting a list of items.
We will examine two specific algorithms: Selection Sort and Insersion Sort
Selection Sort: The approach of selection sort…
Select a value and put it in its final position into the list.
Repeat for all other values.
In more detail, find the smallest value in the list and switch it with the first element in the
list.
Find the next smallest and switch it with the second position on the list.
Repeat with all values.
(double for loop that evaluates and switches.)
Insertion – The approach is to pick any item and insert it into its proper place in a sorted
sublist.
Repeat until all items have been inserted.
In more detail…
Consider the first item to be a sorted sublist.
Insert the second item into the sorted list.
Shifting the first item as needed to make room to insert the new addition.
Insert the third item into the sorted sublist, shifting items as necessary.
Repeat until finished.
22 11 55 66 44
1st pass – 11 22 55 66 44
2nd pass - 11 22 55 66 44
3rd pass – 11 22 55 66 44
4th pass – 11 22 44 55 66
If it shows 8 elements, there are 7 paths for sorting (TEST)
“This” refers to the object on the left hand side. “Other” refers to the object on the right.
Ex. john.hasMore(jill);
Public static void sort (int[] numbers)
SEARCHING
1. Linear Search
2. Binary Search
Linear Search begins at one end of a list and examines each element in turn. Eventually
either the item is found or the end of the list is encountered.
Always add a break when returning a value inside an if statement, otherwise the for loop
will continue to loop.
Binary Search only works on a sorted list.
Sequential Search works on any list but is slower.
Exception – an error in your programming. (ArrayIndexOutOfBound)
Checked Exceptions – (FileNotFoundException)
Try-Catch it
Throw it
Try
{
body
}
(if exception happens)
catch(FileNotFoundException e)
{
}
RECURSION
Recursion is a fundamental programming technique that can provide an elegant solution
certain kinds of problems.
Consider the list
24, 88, 40, 37
This entire list is a NUMBER. Or a number comma LIST.
24, 88,40,37
88, 40,37
40, 37
37
public int sum(int num)
{
int result;
if(num==1)
result = 1;
else
result = num+sum(num-1)
return result;
5+4+3+2+1
Powered by TCPDF (www.tcpdf.org)