Skip to content

Instantly share code, notes, and snippets.

@redrick
Created November 30, 2011 11:49
Show Gist options
  • Save redrick/1408801 to your computer and use it in GitHub Desktop.
Save redrick/1408801 to your computer and use it in GitHub Desktop.
Some basics for java
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);
@redrick
Copy link
Author

redrick commented Nov 30, 2011

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

@redrick
Copy link
Author

redrick commented Jan 9, 2012

see this one for more :P https://gist.github.com/1584289

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment