-
-
Save redrick/1408801 to your computer and use it in GitHub Desktop.
Some Arrays and stuff to use with them.... | |
private String[] receivers; // definition... | |
this.receivers = targets.clone() // not just this.recievers = recievers !! unsafe | |
for each is kinda usefull here... | |
for (int i: recievers) { | |
System.out.println(recievers[i].name); | |
} | |
Equals with hashcode for integer values (integer is primitive therefore no hasCode() for it) | |
public boolean equals(Object obj) { | |
if (!(obj instanceof Point)) return false; | |
Point point = (Point) obj; | |
return (this.x == point.x) && (this.y == point.y) && (this.name.equals(point.name)); | |
} | |
public int hashCode() { | |
int hash = 1; // universal --> hash = hash + hash * prime + attr | |
hash += hash * 31 +x; | |
hash += hash * 31 +y; | |
hash += hash * 31 + name.hashCode(); | |
return hash; | |
} | |
Here is the eqauls with hashcode for objects (list has a function hashCode()) | |
public boolean equals(Object object) { | |
if (!(object instanceof ListPolygon)) return false; | |
return vertices.equals(((ListPolygon)object).vertices); | |
} | |
public int hashCode() { | |
return vertices.hashCode(); | |
} | |
Exceptions | |
classes - inheriting from Exception | |
public MessengerException() { | |
super(); | |
} | |
public MessengerException(String msg) { | |
super(msg); | |
} | |
public MessengerException(Throwable cause) { | |
super(cause); | |
} | |
public MessengerException(String msg, Throwable cause) { | |
super(msg, cause); | |
} | |
Collections | |
List - ArrayList | |
\ HashList | |
Set - HashSet | |
\ TreeSet | |
Map - HashMap | |
private List<Country> countries = new ArrayList<Country>(); // definition | |
safely return collection so they cannot mess with it | |
return Collections.unmodifiableList(countries); |
additionally maybe these imports can be useful also for those collections...
import java.util.List;
import java.util.ArrayList;
import java.util.Collection; // is the implementation of the below intefrace
import java.util.Collections;
import java.util.Iterator;
and with those exceptions:
try and catch block is used to catch and throw them the right way....
try {
} catch(NoRouteToHostException e) {
throw new TargetUnreachableException("not reachable", e);
}
and dont forget that everytime in function one gets thrown like :
throw new TargetUnreachableException("not reachable", e);
it has to be mentioned in header of the function like extends, but :
throws TargetUnreachableException
simple as that
see this one for more :P https://gist.github.com/1584289
sure can use iterator with those collections
for (Iterator it = countries.iterator(); it.hasNext(); ) {
Country c = (Country) it.next();
result += c.getPopulation();
}