Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 1.33 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Nested loops for specific numbers
  2. method(21,19)
  3. method(21,18)
  4. ...
  5. method(21,10)
  6. method(21,9)
  7. method(20,18)
  8. method(20,17)
  9. ...
  10. method(20,13)
  11. method(20,12)
  12.        
  13. for(int ii = 9;ii<21;ii++){
  14.                 for(int xx = 4;xx<19;xx++){
  15.                     method(ii,xx);
  16.                 }
  17.             }
  18.        
  19. static class Range {
  20.     public final int start;
  21.     public final int end;
  22.  
  23.     public Range(int start, int end) {
  24.         this.start = start;
  25.         this.end = end;
  26.     }
  27. }
  28.  
  29. static final Map<Integer,Range> RANGE_MAP = new HashMap<Integer,Range>();
  30. static {
  31.     RANGE_MAP.put(21, new Range(9,19));
  32.     RANGE_MAP.put(20, new Range(12,18));
  33.     // ...
  34. }
  35.  
  36. void calling_method() {
  37.     for(Entry<Integer,Range> entry : RANGE_MAP.entrySet()) {
  38.         int ii = entry.getKey();
  39.         Range r = entry.getValue();
  40.         for(int xx = r.start; xx <= r.end; xx++){
  41.             method(ii,xx);
  42.         }
  43.     }
  44. }
  45.  
  46. void method(int ii, int xx) {
  47.     // do stuff
  48. }
  49.        
  50. private static final Map<Integer,List<Integer>> map = new HashMap<Integer,List<Integer>>(){{
  51.     put(21, new ArrayList<Integer>(){{
  52.         add(19);
  53.         add(18);
  54.         add(17);
  55.         ...
  56.     }});
  57.     put(20, new ArrayList<Integer>(){{
  58.         add(18);
  59.         add(17);
  60.         ...
  61.     }});
  62. }};
  63.        
  64. for(Integer ii : map.keySet()){
  65.     for(Integer xx : map.get(ii)){
  66.         method(ii,xx);
  67.     }
  68. }