ChrisAmwell
ChrisAmwell

Reputation: 15

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!

Upvotes: 1

Views: 1117

Answers (6)

Kaplan
Kaplan

Reputation: 3758

a shorter lambda

public void displayData(ArrayList<char[]> values) {
  values.stream().forEach( arr -> System.out.println( arr ) );
}

Upvotes: 1

xdeepakv
xdeepakv

Reputation: 8135

Little bit improvement

Using list:

import java.util.ArrayList;
import java.util.List;

public class Student {
    public static void main(String[] args) {
        List<String> lines = createList((int) 'A', (int) 'Z');
        print(lines);
    }

    public static List<String> createList(int min, int max) {
        List<String> lines = new ArrayList<>();
        for (int i = 0; i <= max - min; i++) {
            StringBuilder s = new StringBuilder();
            for (int j = min; j <= min + i; j++) {
                s.append(Character.toString((char) j));
            }
            lines.add(s.toString());
        }
        return lines;
    }

    public static void print(List<String> lines) {
        for (String line : lines)
            System.out.println(line);
    }
}

Working: https://www.jdoodle.com/embed/v0/21iL

Without List:

public class Student {
    public static void main(String[] args) {
        print((int) 'A', (int) 'Z');
    }

    public static void print(int min, int max) {
        for (int i = 0; i <= max - min; i++) {
            StringBuilder s = new StringBuilder();
            for (int j = min; j <= min + i; j++) {
                s.append(Character.toString((char) j));
            }
            System.out.println(s.toString());
        }
    }
}

Working: https://www.jdoodle.com/embed/v0/21iP

Using Stream:

import java.util.stream.IntStream;
import java.util.stream.Stream;

public class Student {
    public static void main(String[] args) {
        Stream<String> lines = createList((int) 'A', (int) 'Z');
        lines.forEach(System.out::println);
    }

    public static String row(int i, int min, int max) {
        StringBuilder s = new StringBuilder();
        IntStream.rangeClosed(min, min + i).forEach(j -> s.append(Character.toString((char) j)));
        return s.toString();
    }

    public static Stream<String> createList(int min, int max) {
        return IntStream.rangeClosed(0, max - min).mapToObj(i -> row(i, min, max));
    }
}

Upvotes: 1

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79425

Some of the ways in which it can be done are as follows:

  1. By using enhanced for loop:
public static void displayData(ArrayList<char[]> values) {
    for (char[] arr : values) {
        for (char ch : arr) {
            System.out.print(ch);
        }
        System.out.println();
    }
}
  1. By using index-based for loop:
public static void displayData(ArrayList<char[]> values) {
    for (int i = 0; i < values.size(); i++) {
        for (int j = 0; j < values.get(i).length; j++) {
            System.out.print(values.get(i)[j]);
        }
        System.out.println();
    }
}
  1. By mixing both, enhanced for loop and index-based for loop:
public static void displayData(ArrayList<char[]> values) {
    for (char[] arr : values) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]);
        }
        System.out.println();
    }
}

-OR-

public static void displayData(ArrayList<char[]> values) {
    for (int i = 0; i < values.size(); i++) {
        for (char ch : values.get(i)) {
            System.out.print(ch);
        }
        System.out.println();
    }
}
  1. By using a for loop and String(char[]) constructor:
public static void displayData(ArrayList<char[]> values) {
    for (char[] arr : values) {
        System.out.println(new String(arr));
    }
}
  1. By using Stream API:
public static void displayData(ArrayList<char[]> values) {
    values.stream().forEach(arr -> System.out.println(new String(arr)));
}

Upvotes: 1

Gutieuler
Gutieuler

Reputation: 21

You can split the problem in two parts:

Part 1 -. Print an alphabet:

private static void printAlphabet(char[] alphabet) {

        for(char c : alphabet) {
            System.out.print(c);
        }

}

Part 2 -. Using the last method you can print all alphabets adding a new line at end every time you has printed an alphatet:

public static void displayData(ArrayList<char[]> values) {

         for (char[] alphabet: values) {
             printAlphabet(alphabet);
             System.out.println();
         }

}

Bonus: You can use Stream and functional programming:

public static void displayDataFunctional(ArrayList<char[]> values) {

         values.stream().forEach((alphabet) -> printAlphabet(alphabet));

}

Advice: By convention, Java type names usually start with an uppercase letter.

Upvotes: 1

QuickSilver
QuickSilver

Reputation: 4045

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();
        }
    }

Upvotes: 2

funky
funky

Reputation: 313

You can write it like this:

public static void displayData(ArrayList<char[]> values) {
     for(char[] s : values){
        for(char c : s){
            System.out.print(c);
        }
        System.out.println();
    }
}

This should give you your desired result.

This first advanced for is looping through every entry of the ArrayList. The second for is looping through every entry that is inside ONE of the entries of the ArrayList.

Upvotes: 2

Related Questions