Fund. of Software Dev Honors
Fund. of Software Dev Honors CSSE 221
Popular in Course
Popular in Computer Science and Engineering
This 3 page Class Notes was uploaded by Miss Terry Reichel on Monday October 19, 2015. The Class Notes belongs to CSSE 221 at Rose-Hulman Institute of Technology taught by Staff in Fall. Since its upload, it has received 13 views. For similar materials see /class/225101/csse-221-rose-hulman-institute-of-technology in Computer Science and Engineering at Rose-Hulman Institute of Technology.
Reviews for Fund. of Software Dev Honors
Report this Material
What is Karma?
Karma is the currency of StudySoup.
Date Created: 10/19/15
1D and 2D Arrays An array is an Object that is an aggregate or stored collection of items An item may be an reference or primitive so long as it is of the type the array is declared as When declaring an array let s say of type int in this case you treat it much like a single variable definition int array1 new int30 In the above case quotintquot is the type associated with the given array the quotUquot known as an array indexing operator is used to show that the variable is an array and its second appearance on the right I used to give the array its maximum capacity 30 in this case A 2D array is declared similarly int array2new int3010 In the above case a 2D array is created with the first number representing the number of quotrowsquot and the second the number of quotcolumnsquot so this would be an array with 30 rows and 10 columns Arrays require to either be given a size or a list of items when they are declared This size they are given then becomes their set capacity So if an array is declared with a size of 10 it can hold NO MORE THAN 10 items If a larger capacity is wished a new array with the desired size must be created and the items from the old array moved to the new one To access an item in the array you use the variable name followed by quotUquot with the INDEX you want to access An example using array1 from above would be array13 This would access the item with index 3 but the FOURTH item in the array Similarly accessing a 2D array is much like 1D but one more number must be given array236 accessed the item with row index 3 and column index 6 Arrays are numbered starting a 0 so the 151 item in the 0 index the 2nd item in the 1 index and so on Because of this the MAXIMUM index of an array is one less than its size The size of an array can be accessed by the quotlength operation To use it place quotlength after the array s name array1ength would give the size of array1 30 in this case As a reference type the operation does not create a COPY of an array With this in mind if we were to declare two arrays quotAlphaquot and Beta and said AlphaBeta it would not copy Beta to Alpha but make both variables reference Beta This in mind if a value in Beta were changed ex Beta39 then both Alpha and Beta would reflect that change the operation does not create a separate copy it simply links both variable names to the same data ArrayLists are basically very exible arrays the capacity of the Java ArrayList is the actual size of the array used to store the list elements Capacity of the ArrayList grows automatically as we add elements to it Every time this happens the internal array has to be reallocated The items remain in relative position to other items unless manually modi ed Code example ArrayList arrayList new ArrayListthis creates an empty arraylist of size zero arrayListaddObj ect eadds whatever you want to the arraylist making its size one bigger as well you can also typecast whether the arraylist is to be an arraylist of ints or Booleans like this ArrayListltIntegergt arrayList new ArrayListltIntegergt Most commonly used methods are add Element e Appends the speci ed element to the end of this list returns a Boolean add int index Element e Inserts the speci ed element at the speci ed position in this list Returns void Clear Removes all of the elements from this list Returns void isEmpty Returns true if this list contains no elements Returns a Boolean indexOf Object 0 Returns the index of the rst occurrence of the speci ed element in this list or 1 if this list does not contain the element Returns an int isEmpty Returns true if this list contains no elements Returns a Boolean remove int index Removes the element at the speci ed position in this list Returns the removed element There are many other useful tools for arrayList that can be found in the java API Polymorphism Summary Type Compatibility Derived Classes are compatible with the classes they inherit or known as the Base Class Derived classes are combatable with Base other Derived classes from the same base considering that they use methodsvariables from the base class Polymorphism A Base class object can take base values from derived class objects This allows base objects to become equal to a derived object This is polymorphism Arrays for base objects can also be made to enclose the derived class objects Although making slots within this array do derived commands doesn t work without typecasting the derived class Having the wrong typecast on the array slot type will throw a ClassCastExcepton Using Instanceofwithin an if statement can prevent this error from normally occurring code example class Ball public void action DO NOTHING public class Soccerball extends Ball Override public void action Systemoutprihtlhquotgoalquot public class Football extends Ball Override public void action Systemoutprihtlhquottouchdownquot public class Vollyball extends Ball Override public void action Systemoutprihtlhquotkillquot l l public class ActionLawsuit x q Put here a description of what this method does k A m 54 n ihMyHahd k public static void mainBall inMyHand inMyHandactionargs notice separate methods are not required to display each individual class39s action