Skip to main content

We can store the integer value while iterating in the list using for loop.

We can store the integer value while iterating in the list using forloop.

import java.util.*;

class ArrayListDemo{
    public static void main(String args[]){
      Scanner scanner = new Scanner(System.in);
      List<Integer> list = new ArrayList<Integer>();
      System.out.println("Enter the number of elements you wanna print :");
      int n = scanner.nextInt();
      
      System.out.println("Enter the elements :");
      for(int i = 0; i < n; i++){
          list.add(scanner.nextInt());
      }
      
      System.out.println("List's elements are : " + list);
      
      /*Like this you can store string while iterating in java using forloop*/

      List<String> list1 = new ArrayList<String>();
      System.out.println("Enter the number of elements you wanna store or print : ");
      int nString = scanner.nextInt();
      System.out.println("Enter the elements : ");
      for(int i = 0; i < nString; i++){
          list1.add(scanner.next());
      }
      System.out.println("Names are : " + list1);
      scanner.close();
   }
 }
   

Output Image : Just Click on link

https://drive.google.com/file/d/1OAqMVt_XeEiqdNoK1PGySxFP0leNbZhJ/view?usp=share_link

Enter the number of elements you wanna print : 
5 
Enter the elements : 
11 
12 
13 
14 
15 
List's elements are : [11, 12, 13, 14, 15] 
Enter the number of elements you wanna store or print : 
5 
Enter the elements : 
apple 
banana 
mango 
papaya
orange 
Names are : [apple, banana, mango, papaya, orange] 
We can store the integer value while iterating in the list using forloop.

import java.util.*;

class ArrayListDemo{
    public static void main(String args[]){
      Scanner scanner = new Scanner(System.in);
      List<Integer> list = new ArrayList<Integer>();
      System.out.println("Enter the number of elements you wanna print :");
      int n = scanner.nextInt();
      
      System.out.println("Enter the elements :");
      for(int i = 0; i < n; i++){
          list.add(scanner.nextInt());
      }
      
      System.out.println("List's elements are : " + list);
      
      /*Like this you can store string while iterating in java using forloop*/

      List<String> list1 = new ArrayList<String>();
      System.out.println("Enter the number of elements you wanna store or print : ");
      int nString = scanner.nextInt();
      System.out.println("Enter the elements : ");
      for(int i = 0; i < nString; i++){
          list1.add(scanner.next());
      }
      System.out.println("Names are : " + list1);
      scanner.close();
   }
 }
   

Output Image : Just Click on link

https://drive.google.com/file/d/1OAqMVt_XeEiqdNoK1PGySxFP0leNbZhJ/view?usp=share_link

We can store the integer value while iterating in the list using for loop.

import java.util.*;

class ArrayListDemo{
    public static void main(String args[]){
      Scanner scanner = new Scanner(System.in);
      List<Integer> list = new ArrayList<Integer>();
      System.out.println("Enter the number of elements you wanna print :");
      int n = scanner.nextInt();
      
      System.out.println("Enter the elements :");
      for(int i = 0; i < n; i++){
          list.add(scanner.nextInt());
      }
      
      System.out.println("List's elements are : " + list);
      
      /*Like this you can store string while iterating in java using forloop*/

      List<String> list1 = new ArrayList<String>();
      System.out.println("Enter the number of elements you wanna store or print : ");
      int nString = scanner.nextInt();
      System.out.println("Enter the elements : ");
      for(int i = 0; i < nString; i++){
          list1.add(scanner.next());
      }
      System.out.println("Names are : " + list1);
      scanner.close();
   }
 }
   

Output:

Enter the number of elements you wanna print : 
5 
Enter the elements : 
11 
12 
13 
14 
15 
List's elements are : [11, 12, 13, 14, 15] 
Enter the number of elements you wanna store or print : 
5 
Enter the elements : 
apple 
banana 
mango 
papaya
orange 
Names are : [apple, banana, mango, papaya, orange] 
added 35 characters in body
Source Link
We can store the integer value while iterating in the list using forloop.

import java.util.*;

class ArrayListDemo{
    public static void main(String args[]){
      Scanner scanner = new Scanner(System.in);
      List<Integer> list = new ArrayList<Integer>();
      System.out.println("Enter the number of elements you wanna print :");
      int n = scanner.nextInt();
      
      System.out.println("Enter the elements :");
      for(int i = 0; i < n; i++){
          list.add(scanner.nextInt());
      }
      
      System.out.println("List's elements are : " + list);
      
      /*Like this you can store string while iterating in java using forloop*/

      List<String> list1 = new ArrayList<String>();
      System.out.println("Enter the number of elements you wanna store or print : ");
      int nString = scanner.nextInt();
      System.out.println("Enter the elements : ");
      for(int i = 0; i < nString; i++){
          list1.add(scanner.next());
      }
      System.out.println("Names are : " + list1);
      scanner.close();
   }
 }
   

Output Image : Just Click on link

https://drive.google.com/file/d/1OAqMVt_XeEiqdNoK1PGySxFP0leNbZhJ/view?usp=share_link

We can store the integer value while iterating in the list using forloop.

import java.util.*;

class ArrayListDemo{
    public static void main(String args[]){
      Scanner scanner = new Scanner(System.in);
      List<Integer> list = new ArrayList<Integer>();
      System.out.println("Enter the number of elements you wanna print :");
      int n = scanner.nextInt();
      
      System.out.println("Enter the elements :");
      for(int i = 0; i < n; i++){
          list.add(scanner.nextInt());
      }
      
      System.out.println("List's elements are : " + list);
      
      /*Like this you can store string while iterating in java using forloop*/

      List<String> list1 = new ArrayList<String>();
      System.out.println("Enter the number of elements you wanna store or print : ");
      int nString = scanner.nextInt();
      System.out.println("Enter the elements : ");
      for(int i = 0; i < nString; i++){
          list1.add(scanner.next());
      }
      System.out.println("Names are : " + list1);
      scanner.close();
   }
 }
   

https://drive.google.com/file/d/1OAqMVt_XeEiqdNoK1PGySxFP0leNbZhJ/view?usp=share_link

We can store the integer value while iterating in the list using forloop.

import java.util.*;

class ArrayListDemo{
    public static void main(String args[]){
      Scanner scanner = new Scanner(System.in);
      List<Integer> list = new ArrayList<Integer>();
      System.out.println("Enter the number of elements you wanna print :");
      int n = scanner.nextInt();
      
      System.out.println("Enter the elements :");
      for(int i = 0; i < n; i++){
          list.add(scanner.nextInt());
      }
      
      System.out.println("List's elements are : " + list);
      
      /*Like this you can store string while iterating in java using forloop*/

      List<String> list1 = new ArrayList<String>();
      System.out.println("Enter the number of elements you wanna store or print : ");
      int nString = scanner.nextInt();
      System.out.println("Enter the elements : ");
      for(int i = 0; i < nString; i++){
          list1.add(scanner.next());
      }
      System.out.println("Names are : " + list1);
      scanner.close();
   }
 }
   

Output Image : Just Click on link

https://drive.google.com/file/d/1OAqMVt_XeEiqdNoK1PGySxFP0leNbZhJ/view?usp=share_link

deleted 236 characters in body
Source Link
We can store the integer value while iterating in the list using forloop.

import java.util.*;

class ArrayListDemo{
    public static void main(String args[]){
      Scanner scanner = new Scanner(System.in);
      List<Integer> list = new ArrayList<Integer>();
      System.out.println("Enter the number of elements you wanna print :");
      int n = scanner.nextInt();
      
      System.out.println("Enter the elements :");
      for(int i = 0; i < n; i++){
          list.add(scanner.nextInt());
      }
      
      System.out.println("List's elements are : " + list);
      
      /*Like this you can store string while iterating in java using forloop*/

      List<String> list1 = new ArrayList<String>();
      System.out.println("Enter the number of elements you wanna store or print : ");
      int nString = scanner.nextInt();
      System.out.println("Enter the elements : ");
      for(int i = 0; i < nString; i++){
          list1.add(scanner.next());
      }
      System.out.println("Names are : " + list1);
      scanner.close();
   }
 }
    

Ouput :

Enter the number of elements you wanna print : 5 Enter the elements : 11 12 13 14 15 List's elements are : [11, 12, 13, 14, 15] Enter the number of elements you wanna store or print : 5 Enter the elements : apple banana mango papaya orange Names are : [apple, banana, mango, papaya, orange]https://drive.google.com/file/d/1OAqMVt_XeEiqdNoK1PGySxFP0leNbZhJ/view?usp=share_link

We can store the integer value while iterating in the list using forloop.

import java.util.*;

class ArrayListDemo{
    public static void main(String args[]){
      Scanner scanner = new Scanner(System.in);
      List<Integer> list = new ArrayList<Integer>();
      System.out.println("Enter the number of elements you wanna print :");
      int n = scanner.nextInt();
      
      System.out.println("Enter the elements :");
      for(int i = 0; i < n; i++){
          list.add(scanner.nextInt());
      }
      
      System.out.println("List's elements are : " + list);
      
      /*Like this you can store string while iterating in java using forloop*/

      List<String> list1 = new ArrayList<String>();
      System.out.println("Enter the number of elements you wanna store or print : ");
      int nString = scanner.nextInt();
      System.out.println("Enter the elements : ");
      for(int i = 0; i < nString; i++){
          list1.add(scanner.next());
      }
      System.out.println("Names are : " + list1);
      scanner.close();
   }
 }
    

Ouput :

Enter the number of elements you wanna print : 5 Enter the elements : 11 12 13 14 15 List's elements are : [11, 12, 13, 14, 15] Enter the number of elements you wanna store or print : 5 Enter the elements : apple banana mango papaya orange Names are : [apple, banana, mango, papaya, orange]

We can store the integer value while iterating in the list using forloop.

import java.util.*;

class ArrayListDemo{
    public static void main(String args[]){
      Scanner scanner = new Scanner(System.in);
      List<Integer> list = new ArrayList<Integer>();
      System.out.println("Enter the number of elements you wanna print :");
      int n = scanner.nextInt();
      
      System.out.println("Enter the elements :");
      for(int i = 0; i < n; i++){
          list.add(scanner.nextInt());
      }
      
      System.out.println("List's elements are : " + list);
      
      /*Like this you can store string while iterating in java using forloop*/

      List<String> list1 = new ArrayList<String>();
      System.out.println("Enter the number of elements you wanna store or print : ");
      int nString = scanner.nextInt();
      System.out.println("Enter the elements : ");
      for(int i = 0; i < nString; i++){
          list1.add(scanner.next());
      }
      System.out.println("Names are : " + list1);
      scanner.close();
   }
 }
   

https://drive.google.com/file/d/1OAqMVt_XeEiqdNoK1PGySxFP0leNbZhJ/view?usp=share_link

added 320 characters in body
Source Link
Loading
deleted 28 characters in body
Source Link
Loading
added 1125 characters in body
Source Link
Loading
Source Link
Loading