DS2 ARRAY Flashcards
OBJECT VS BUILT-IN TYPE
object:
-create with new obj()
-addressed via reference that is stored in local variable points to memory address where values are stored
-there are wrapper classes for types (Integer class has parseInt method etc)
type:
-create with type_name variable name= value
-variable points to contents ofmemory address
-can be casted or transformed to a different type (char can become int, double can become float and int etc)
ARRAYS
collection of same type items or objects
has index pointer to access position (indexing at 0)
throws ArrayOutOfBoundsException in java
public field length
size is predetermined cant extend or shorten
takes up memory serially
ERATOSTHENIS SIEVE
give n>=2 get all primes <n
approach 1:
n length boolean array
start with all positions true
parse it starting from pos 2(assuming 0, 1 are not prime numbers)
every time i is prime find multiplicands<n and set to false
if we encounter false continue
java code:
primes(n){
~boolean[] a = new boolean[n];
~for (int i = 2; i < n; i++){a[i] = true;}
~for(int i = 2; i < n; i++)
~~{if(a[i])
~~~{for(int j = i; j * i < n; j++) //start at j=i cause for j<i previous iterations have already found it//
~~~~{a[i*j] = false;} }}}
time complexity is n/2 + n/3 +…+n/n = n(1/2+…+1/n)= n*lnn so same scale as O(nlogn)
TYPES OF ARRAY COPIES
(concerns arrays that contain objects types cannot have a copied reference so a semi-deep type copy is a deep copy)
-shallow copy: copy of reference
is basically 2 names for the same thing
points to same address
if one is altered both are altered
-semi-deep copy: copy of content reference
2 different arrays
contents point to the same address
changing one array doesnt affect the other one but changing something in the contents of one array does
(changing a field of obj a in ar1 changes the field of that object in ar2 but changing ar1[i] doesnt change ar2[i])
-deep copy: use new() when copying contents
only values are the same
SIZE CHANGE
arrays are static data structures so to change size we have to copy the contents to a different size array and delete the original
caution with memory leaks especially in c and c++ since memory isnt freed automatically when deleting a reference
MULTI-DIMENTIONAL ARRAYS
declare as obj[i][j]
store serially in lines(first line then second etc)
thats why we parse i then j optimally
APPLICATION IN GRAPH REPRESENTATION
v : vertices (dots)
e : edges (connectors)
using adjacency matrix
if i and j vertices are connected ar[i][j] and [j][i] set to 1 else set to 0
n^2 memory
O(1) time complexity for adjacency check
SPARCE ARRAYS
2-D array majority of contents have value 0
better store as 1-D array with coords and value
k( non zero points) memory instead of n^2
TRIANGULAR ARRAYS
above or below diagonal is all 0
there are more effective ways of storing but depends on application and trade offs
HONOURABLE MENTIONS
classes Vector and ArrayList are dynamic but slower to parse (get() method)
usually use array when creating data structures like trees