ArrayList Basics

Lab Java Beginner 10 points

Read n words into an ArrayList<String>. Print its size, the first and last word, and the whole list.

Input

n, then n words.

Output

Four lines.

Example

Input

3
apple banana cherry

Output

3
apple
cherry
[apple, banana, cherry]
Sample input available Sample output available
Editor
Java

Hints

add() appends, get(i) reads, size() counts.

A List prints like [a, b, c].

Solution explanation

Unlike arrays, an ArrayList grows as you add items. Declare it as the List interface.

Show solution
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        List<String> words = new ArrayList<String>();
        for (int i = 0; i < n; i++) {
            words.add(sc.next());
        }
        System.out.println(words.size());
        System.out.println(words.get(0));
        System.out.println(words.get(words.size() - 1));
        System.out.println(words);
    }
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD