public class Tester {
static List<String> columnNames(int n) {
List<String> result = new ArrayList<String>();
String alphabets[] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
StringBuilder sb = new StringBuilder();
for(int j = 0; j < n; j++){
int index = j/26;
char ch = (char) (j % 26 + 'A');
sb.append(ch);
String item = "";
if(index > 0) {
item += alphabets[index-1];
}
item += alphabets[j % 26];
result.add(item);
}
sb.reverse();
return result;
}
I use this function, It seems ok but I give 703 input(702 is ok but 703 is not ok) , I get an error
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 26
at example.Tester.columnNames(Tester.java:20)
at example.Tester.main(Tester.java:34)
Please help me. If input is 703 must be A,B,.......AA,AB,....,ZZ,AAA,AAB,AAC...