Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static final int CLUBS = 0;
- public static final int DIAMONDS = 1;
- public static final int HEARTS = 2;
- public static final int SPADES = 3;
- public enum Suit {
- CLUBS,
- DIAMONDS,
- HEARTS,
- SPADES
- }
- List l = someList;
- Iterator i = l.getIterator();
- while (i.hasNext()) {
- MyObject o = (MyObject)i.next();
- }
- List<MyObject> l = someList;
- for (MyObject o : l) {
- //do something
- }
- try {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- String str = reader.readLine();
- reader.close();
- }
- catch (IOException e) {
- System.err.println("error when closing input stream.");
- }
- Scanner reader = new Scanner(System.in);
- String str = reader.nextLine();
- reader.close();
- Console reader = System.console();
- String str = reader.readLine();
- public int add(int... numbers){
- int sum = 0 ;
- for (int i : numbers){
- sum+=i;
- }
- return sum ;
- }
- public int add(int n1, int n2, int n3, int n4) ;
- public int add(List<Integer> numbers) ;
- Integer myInteger = 6;
- int myInt = myInteger.intValue();
- Integer myInteger = 6;
- int myInt = myInteger;
- String str = "test " + intValue + " test " + doubleValue;
- String str = String.format("test %d test %lg", intValue, doubleValue);
- myframe.getContentPane().add(mycomponent);
- myframe.add(mycomponent);
- import static Math.* ;
- Vector stringVector = new Vector();
- stringVector.add("hi");
- stringVector.add(528); // oops!
- stringVector.add(new Whatzit()); // Oh my, could spell trouble later on!
- ArrayList<String> stringList = new ArrayList<String>();
- stringList.add("hello again");
- stringList.add(new Whatzit()); // Won't compile!
- List list = getTheList();
- Iterator iter = list.iterator()
- while (iter.hasNext()) {
- String s = (String) iter.next();
- // .. do something
- }
- List list = getTheList();
- for (Iterator iter = list.iterator(); iter.hasNext();) {
- String s = (String) iter.next();
- // .. do something
- }
- List<String> list = getTheList();
- for (String s : list) {
- // .. do something
- }
- String s = n + "";
- String s = String.valueOf(n);
- // AGGHHH!!!
- int[] array = new int[] {0, 1, 2, 3, 4};
- for (int i = 0; i < array.length; i++)
- {
- // Do something...
- }
- // Nice and clean.
- int[] array = new int[] {0, 1, 2, 3, 4};
- for (int n : array)
- {
- // Do something...
- }
- int[] src = new int[] {1, 2, 3, 4, 5};
- int[] dest = new int[src.length];
- System.arraycopy(src, 0, dest, 0, src.length);
- int[] src = new int[] {1, 2, 3, 4, 5};
- int[] dest = Arrays.copyOf(src, src.length);
- List<String> items = new ArrayList<String>();
- items.add("one");
- items.add("two");
- items.add("three");
- handleItems(items);
- handleItems(Arrays.asList("one", "two", "three"));
- class Test extends TestCase {
- public void testYadaYada() { ... }
- }
- class Test {
- @Test public void yadaYada() { ... }
- }
- String s1 = "...", s2 = "...";
- if (s1.intern() == s2.intern()) {
- ....
- }
- String s1 = "...", s2 = "...";
- if (s1.equals(s2)) {
- ....
- }
- public class Enum
- {
- public static final Enum FOO = new Enum();
- public static final Enum BAR = new Enum();
- }
- public enum Singleton {
- INSTANCE;
- public void someMethod() {
- ...
- }
- }
- public class Singleton {
- public static final Singleton INSTANCE = new Singleton();
- private Singleton() {
- ...
- }
- public void someMethod() {
- ...
- }
- }
Add Comment
Please, Sign In to add comment