Posted By

borysn on 05/31/13


Tagged

list java character ascii Fill


Versions (?)

Fill Character List From ASCII Table And Sort


 / Published in: Java
 

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

  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  

You need to login to post a comment.