- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to implement an ArrayList using JShell in Java 9?
JShell is an interactive Java Shell tool that enables us to execute java code from the shell and instantly displays the output. JShell is the REPL(Read Evaluate Print Loop) tool that runs from the command-line. We can start a JShell by simply typing "jshell" in the command prompt, and to exit the jshell by using "/exit" command. For small snippets, we do not need to create a main() method in JShell.
We can also implement the major collections like list, map and set by using this tool. In the below program, we can implement an ArrayList with various scenarios.
C:\Users\User\Desktop\Java 9 QNA>jshell | Welcome to JShell -- Version 9.0.4 | For an introduction type: /help intro jshell> ArrayList<String> list = new ArrayList<String>(); list ==>  jshell> list.add("Jai");list.add("Adithya");list.add("Raja");list.add("Chaitanya"); $2 ==> true $3 ==> true $4 ==> true $5 ==> true jshell> list list ==> [Jai, Adithya, Raja, Chaitanya] jshell> list.isEmpty() $7 ==> false jshell> list.get(3) $8 ==> "Chaitanya" jshell> list.get(9) | java.lang.IndexOutOfBoundsException thrown: Index 9 out-of-bounds for length 4 | at Preconditions.outOfBounds (Preconditions.java:64) | at Preconditions.outOfBoundsCheckIndex (Preconditions.java:70) | at Preconditions.checkIndex (Preconditions.java:248) | at Objects.checkIndex (Objects.java:372) | at ArrayList.get (ArrayList.java:440) | at (#9:1) jshell> list.size() $10 ==> 4 jshell> if(list.isEmpty()) System.out.println("Empty"); else System.out.println("Not Empty"); Not Empty
- How to implement java.time.LocalDate using JShell in Java 9?
- How to implement JShell using JavaFX in Java 9?
- How to implement a String in JShell in Java 9?
- How to implement the encapsulation concept in JShell in Java 9?
- How to implement a lambda expression in JShell in Java 9?
- How to implement the Fibonacci series in JShell in Java 9?
- How to implement integer type conversion in JShell in Java 9?
- How to implement a Set interface in JShell in Java 9?
- How to implement relational and logical operators in JShell in Java 9?
- How to implement String utility and immutability in JShell in Java 9?
- How to implement HashMap, LinkedHashMap, and TreeMap in JShell in Java 9?
- How can we implement a map in JShell in Java 9?
- JShell in Java 9?
- How to handle an exception in JShell in Java 9?
- How to initialize an array in JShell in Java 9?