Advertisement
Guest User

HexIterator

a guest
May 21st, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. import java.util.Iterator;
  2.  
  3. public class HexIteration implements Iterable<String>{
  4. private int[] decimals;
  5. private int n = 100;
  6.  
  7. public HexIteration(){
  8.  
  9. this.decimals = new int[n];
  10.  
  11. for(int i = 0; i < n; i++){
  12. this.decimals[i] = i;
  13. }
  14. }
  15.  
  16. public Iterator<String> iterator(){
  17. return new HexIterator();
  18. }
  19.  
  20. public class HexIterator implements Iterator<String>{
  21.  
  22. private int i = 0;
  23.  
  24. private String searchMaxNum(int decimal){
  25. int curr = decimal;
  26. int maxNum = 0;
  27. while(curr > 0){
  28. if(maxNum < (curr % 16)){
  29. maxNum = curr % 16;
  30. }
  31.  
  32. curr /= 16;
  33. }
  34. return Integer.toHexString(maxNum);
  35. }
  36.  
  37. public boolean hasNext(){
  38. return i < n;
  39. }
  40.  
  41. public String next(){
  42. return searchMaxNum(decimals[i++]);
  43. }
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement