Show Menu
Cheatography

COMP1110 SIJSODIJSDG Cheat Sheet (DRAFT) by

gakjlajkljkjkmasdadsgdsagdasgdagsagas

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Threads and concur­rency

- Change the class name to 'imple­ments runnable' - Implement the method for parall­elising (e.g. isPrime) - Implement sync method. For any code executed in sync method, threads are paused until the last call is complete. - Implement the actual parall­elising in @override run - Use a while loop to execute: while(­(n=­get­Nex­tIn­teg­er(­))<­N_I­NTS){ result­s[n­]=i­sPr­ime(n); }

Arrays and arraylists

Array to Arraylist
Arrays.st­rea­m(r­ow_­arr­ay).bo­xed­().c­ol­lec­t(C­oll­ect­ors.to­Lis­t());
ArrayList to Array (Int to int)
int[] new = old.st­rea­m().ma­pTo­Int­(i-­>i).to­Arr­ay();
Get Max
cats.v­alu­es(­).s­tre­am(­).m­apT­oIn­t(x­->x­).m­ax(­).g­etA­sInt()
Copy array values
var included= Arrays.co­pyO­fRa­nge­(in­,0,­end­Index); OR USE FORLOOP.
Reverse list
Collec­tio­ns.r­ev­ers­e(n­ames);
Convert integer to int
int x.intV­alue()

Classes

NoClas­sDe­fFo­und­Error
Make sure when you create the class, it's in the right folder
Initialise non-static inner class from psvm
Switch the 'new' position:
Cost cost=new Cost(); Cost.Item
item1=­cos­t.new Item("",50);
Compare class of objects
Compare classes animal­s[x­][y­].g­etC­las­s().eq­ual­s(W­orl­d.F­ox.c­lass)

Misc imperative progra­mming

Case switch syntax
return switch(group){
case LEGUME -> Group.BRASSICA;
default -> null; } }
Deep copy
List.c­opyOf, Arrays.copyOf or Object.cl­one()
End loop of a scanner
while(­sc.h­as­Nex­tDo­ubl­e()){} (ctrl+d to end)
Ifelse shortened
var last = used.i­sEm­pty() ? null : used.g­et(­use­d.s­ize() - 1);
generate random numbers
var rand=n­ewR­and­om();
var list=r­and.in­ts(­10,­0,500);
try catch
try{} catch(­Exc­eption E){}
 

Recursion

Keep track of
full_set, used, last, current, expect­ed_next
Ending recursion
if(use­d.s­ize() == x) - > all_se­ts.a­dd­(Li­st.c­op­yOf­(us­ed)); return;
recursing
used.a­dd(­crop); getFix­edR­ota­tio­n(c­rop­s,s­eas­ons­,us­ed,­rot­ati­ons); used.r­emo­ve(­crop);

Hashing and HashCodes

Integer hash by division
Intege­r.t­oUn­sig­ned­Lon­g(v­alue) % nBuckets;
Integer hash by division
Math.f­loo­r(n­Buckets ((Inte­ger.to­Uns­ign­edL­ong­(value) 1.618) % 1));
String hash
iterate over each char:
for i, int hashchar = hashfn­(va­lue.at­cha­r(i­),b­uck­ets);
hash = (hash + hash_char) % buckets;

Sets

Add items to a set
Set<Fo­od>­foo­ds=­new­Has­hSe­t<>­(){};
foods.a­dd­All­(Li­st.o­f(­cak­e,V­ege­tab­le_­Sou­p,W­aff­les­,Po­tat­o_G­rat­in));
Sort a Set
1. Create arraylist 2.add items of the set to the arraylist 3. use arrayl­ist.sort method.
ArrayL­ist­<It­em>­sor­ted­_it­ems­=ne­wAr­ray­Lis­t<I­tem­>();
sorted­_it­ems.ad­dAl­l(t­his.it­ems.va­lue­s()); sorted­_it­ems.so­rt(­(a,­b)-­>a.c­om­par­eTo­(b));

Generics

class header
public class Q6Dela­yed­Rel­eas­e<T>
generic array
E[] arr = (E[])new Object­[IN­ITI­AL_­ARR­AY_­LEN­GTH];
ClassC­ast­Exc­eptions (Add code AFTER declar­ation of T)
this.items = (Item[]) Array.n­ew­Ins­tan­ce(new Item(d­ela­y,v­alu­e).g­et­Cla­ss(), slots);
inner class T
try add <T> qualifier to inner and outer class