add value with n variable with Arraylist in java
Scanner in = new Scanner(System.in); int n = 5; ArrayListaddName = new ArrayList<>(); for(int i = 0; i < n; i++){ addName.add(in.nextLine()); }
Here is what the above code is Doing:
1. We are creating a scanner object to take input from the user.
2. We are creating an ArrayList of type String.
3. We are using a for loop to take input from the user.
4. We are using the add() method to add the input to the ArrayList.