Search code examples
javaarraysalphabetical

How do I print the alphabet line by line?


I am a student who helps younger students learn how to program. A few of them have been given the code:

public class student {
    public static void main(String[] args) {
        ArrayList<char[]> data1 = new ArrayList<char[]>();
        ArrayList<char[]> data2 = createData();
        displayData(data2);
    }
    
    public static ArrayList<char[]> createData(){
        ArrayList<char[]> res = new ArrayList<char[]>();
        for(int index = 'A'; index<='Z'; index++) {
            char[] temp = new char[1+(index - 'A')];
            for(int count=0;count<temp.length;count++) {
                temp[count] = (char) ('A' + count);
            }
            res.add(temp);
        }
        return res;
    }
    
    public static void displayData(ArrayList<char[]> values) {
        
    }

This is meant to print out the alphabet like:

A

AB

ABC

etc. However I myself am having trouble as to what goes in the displayData() method.

Any help is appreciated, thanks in advance!


Solution

  • You can do it single func

    public static void displayData() {
            int start = 'A';
            int finish = 'Z';
            for (int i = start ; i < finish; i++){
                for (int j = start ; j <= i; j++) {
                    System.out.print((char)j);
                }
                System.out.println();
            }
        }