Posted By

mscribellito on 11/08/11


Tagged


Versions (?)

AlphabetRange


 / Published in: Java
 

  1. import java.util.ArrayList;
  2. import java.util.Iterator;
  3.  
  4. public class AlphabetRange {
  5.  
  6. private ArrayList<Character> range;
  7.  
  8. private char start;
  9. private char stop;
  10.  
  11. public AlphabetRange(char start, char stop) {
  12.  
  13. this.start = start;
  14. this.stop = stop;
  15.  
  16. this.range = new ArrayList<Character>();
  17.  
  18. build();
  19.  
  20. }
  21.  
  22. private void build() {
  23.  
  24. boolean reverse = false;
  25.  
  26. if (stop < start) {
  27. reverse = true;
  28. }
  29.  
  30. for (int i = start; reverse ? i >= stop : i <= stop; i = reverse ? i - 1
  31. : i + 1) {
  32.  
  33. range.add((char) i);
  34.  
  35. }
  36.  
  37. }
  38.  
  39. public char get(int index) {
  40.  
  41. return range.get(index);
  42.  
  43. }
  44.  
  45. public Iterator<Character> iterator() {
  46.  
  47. return range.iterator();
  48.  
  49. }
  50.  
  51. public int size() {
  52.  
  53. return range.size();
  54.  
  55. }
  56.  
  57. public String toString() {
  58.  
  59. return range.toString();
  60.  
  61. }
  62.  
  63. }

Report this snippet  

You need to login to post a comment.