File tree 5 files changed +64
-15
lines changed
5 files changed +64
-15
lines changed Original file line number Diff line number Diff line change 1
1
class Collection (list ):
2
- pass
2
+ def add (self ,o ):
3
+ self .append (o )
4
+
5
+ def iterator (self ):
6
+ return Iterator (self )
3
7
4
8
5
9
class List (Collection ):
@@ -24,12 +28,12 @@ class ArrayList(Collection):
24
28
def __init__ (self ,typ ):
25
29
self ._typ = typ
26
30
27
- def add ( self , o ):
28
- self . append ( o )
29
-
30
- def iterator (self ):
31
- return Iterator ( self )
32
-
33
-
34
-
31
+
32
+ class Arrays ( object ):
33
+ @ classmethod
34
+ def asList (self , * a ):
35
+ res = List ( )
36
+ res . extend ( a )
37
+ return res
38
+
35
39
Original file line number Diff line number Diff line change
1
+ // from http://www.javapractices.com/topic/TopicAction.do?Id=88
2
+
3
+ import java .util .*;
4
+
5
+ /** Different iteration styles. */
6
+ public class IterateNoIndex {
7
+
8
+ /**
9
+ * Iterating without an index is more compact and less
10
+ * error prone.
11
+ */
12
+ public static void withoutIndex (){
13
+ //for-each loop
14
+ List <String > trees = Arrays .asList ("Maple" , "Birch" , "Poplar" );
15
+ for (String tree : trees ){
16
+ log (tree );
17
+ }
18
+
19
+ //with an Iterator
20
+ Iterator <String > iter = trees .iterator ();
21
+ while (iter .hasNext ()) {
22
+ log (iter .next ());
23
+ }
24
+ }
25
+
26
+ /** Iterating with an index is more error prone. */
27
+ public static void withIndex (){
28
+ //traditional for-loop
29
+ for (int idx =0 ; idx < 10 ; ++idx ){
30
+ log ("Iteration..." );
31
+ }
32
+ }
33
+
34
+ // PRIVATE //
35
+ private static void log (String aMessage ){
36
+ System .out .println (aMessage );
37
+ }
38
+
39
+ public static void main ( String ... aArguments ) {
40
+ withoutIndex ();
41
+ withIndex ();
42
+ }
43
+
44
+ }
45
+
Original file line number Diff line number Diff line change 4
4
5
5
public final class LoopStyles {
6
6
7
- public static void main (String ... aArguments ) {
7
+ public static void main ( String ... aArguments ) {
8
8
List <String > flavours = new ArrayList <String >();
9
9
flavours .add ("chocolate" );
10
10
flavours .add ("strawberry" );
Original file line number Diff line number Diff line change @@ -25,10 +25,10 @@ strategies
25
25
26
26
expr-to-box: Plus(e) -> H hs=0 [ "+" ~e]
27
27
expr-to-box: Minus(e) -> H hs=0 [ "-" ~e]
28
- expr-to-box: PreIncr(e) -> H hs=0 [ "++" ~e "###NOTIMPL### "]
29
- expr-to-box: PreDecr(e) -> H hs=0 [ "--" ~e "###NOTIMPL### "]
30
- expr-to-box: PostIncr(e) -> H hs=0 [~e "++" "###NOTIMPL### "]
31
- expr-to-box: PostDecr(e) -> H hs=0 [~e "--" "###NOTIMPL### "]
28
+ expr-to-box: PreIncr(e) -> H hs=0 [~e " += 1 ##PreIncr "]
29
+ expr-to-box: PreDecr(e) -> H hs=0 [~e " -= 1 ##PreDecr "]
30
+ expr-to-box: PostIncr(e) -> H hs=0 [~e " += 1 ##PostIntcr "]
31
+ expr-to-box: PostDecr(e) -> H hs=0 [~e " -= 1 ##PostDecr "]
32
32
33
33
expr-to-box: Mul(e1, e2) -> H hs=1 [~e1 "*" ~e2]
34
34
expr-to-box: Div(e1, e2) -> H hs=1 [~e1 "/" ~e2]
Original file line number Diff line number Diff line change @@ -168,7 +168,7 @@ rules
168
168
H hs=0 [
169
169
"while " ~*bcond ":"
170
170
]
171
- H hs=1 [" " ~bupdate]
171
+ H hs=0 [" " ~bupdate]
172
172
]
173
173
174
174
where
You can’t perform that action at this time.
0 commit comments