Fill Character List From ASCII Table And Sort


/ Published in: Java
Save to your folder(s)

FIll a List with ASCII (0-127) characters with a given number of characters.


Copy this code and paste it in your HTML
  1. private List<Character> initCharacterList(int numberOfCharacters, int asciiStart, int asciiEnd) {
  2. // first ensure ascii table values
  3. assertTrue("asciiStart is invalid", asciiStart >= 0 && asciiStart < asciiEnd);
  4. assertTrue("asciiEnd is invalid", asciiEnd <= 127 && asciiEnd > asciiStart);
  5.  
  6. List<Character> list = new ArrayList<>();
  7.  
  8. int asciiPos = asciiStart;
  9.  
  10. for (int i = 0; i < numberOfCharacters; i++) {
  11. Character c = new Character((char)asciiPos);
  12. list.add(c);
  13. asciiPos++;
  14. if (asciiPos > asciiEnd) {
  15. asciiPos = asciiStart;
  16. }
  17. }
  18.  
  19. Collections.sort(list);
  20.  
  21. return list;
  22. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.