Created
November 30, 2011 11:49
-
-
Save redrick/1408801 to your computer and use it in GitHub Desktop.
Some basics for java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
see this one for more :P https://gist.github.com/1584289