Sort by

recency

|

275 Discussions

|

  • + 0 comments

    Use stream and lambdas

    import java.util.List;
    import java.util.Scanner;
    import java.util.stream.Collectors;
    import java.util.stream.Stream;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int n = scanner.nextInt();
    
            List<Integer> list = Stream.generate(scanner::nextInt).limit(n).collect(Collectors.toList());
    
            list.forEach(System.out::println);
        }
    }
    
  • + 0 comments
    import java.util.*;
    
    public class Solution {
    
        public static void main(String[] args) {
           
            Scanner scan = new Scanner(System.in);
            int n = scan.nextInt();
            int[] a = new int[n];
         for(int i=0;i<n;i++)
         {
              a[i]=scan.nextInt();
         }
            scan.close();
    
            // Prints each sequential element in array a
            for (int i = 0; i < a.length; i++) {
                System.out.println(a[i]);
            }
        }
    }
    
  • + 0 comments

    Practicing mapping:

            //java8
            ArrayList<Integer> A = new ArrayList<>();
            while (scan.hasNextInt())A.add(scan.nextInt());
            int[] a = A.stream().mapToInt(i -> i).toArray();
    
  • + 0 comments

    In java 8:

    int[] a = new int[n]; for (int i =0; i < n; i++) { a[i] = scan.nextInt(); }

  • + 0 comments

    int a[]=new int[n];

        for(int i=0;i<a.length;i++){
        int b=scan.nextInt();
        a[i]=b;
        }