Java Programming
I need to create an application that have an Arraylist string with 6 elements. Then it ask the user to input more elements until it reaches 10 elements in total.
After that the application remove 1 element with remove() by index.
To finalize it uses a for-each loop to display the list.
Thanks for your help!
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions. Thank You! =========================================================================== import java.util.ArrayList; import java.util.Scanner; public class List { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); ArrayList<String> elements = new ArrayList<>(); //Arraylist string with 6 elements. elements.add("Brazil"); elements.add("China"); elements.add("India"); elements.add("Spain"); elements.add("Germany"); elements.add("Singapore"); // Add 4 more elements ; // it ask the user to input more elements until it // reaches 10 elements in total. for(int count=1; count<=4; count++){ System.out.print("Enter a string: "); String str = scanner.nextLine(); elements.add(str); } System.out.println("ArrayList Before Deleting: "); for(int i=0;i<elements.size();i++){ System.out.println(i+": "+elements.get(i)); } //After that the application remove 1 element with remove() by index. System.out.print("Enter an index [0-9]: "); int index = scanner.nextInt(); if(0<=index && index<elements.size()){ elements.remove(index); System.out.println("\nArrayList after Deleting"); //To finalize it uses a for-each loop to display the list. for(int i=0;i<elements.size();i++){ System.out.println(i+": "+elements.get(i)); } }else{ System.out.println("Error: Index out of range."); } } }
=============================================================
Get Answers For Free
Most questions answered within 1 hours.