fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. List<Integer> list = new ArrayList<Integer>();
  13. Scanner in = new Scanner(System.in);
  14. int n = in.nextInt();
  15. int[] defaultArray = new int[n];
  16. // do validation for n
  17. for (int i = 0; i < n; i++) {
  18. int temp = in.nextInt();
  19.  
  20. if (temp != 0) {
  21. list.add(temp);
  22. defaultArray[i] = temp;
  23. }
  24. }
  25. Integer[] positiveArray = new Integer[list.size()];
  26. positiveArray = list.toArray(positiveArray);
  27. System.out.println(Arrays.toString(defaultArray));
  28. System.out.println(Arrays.toString(positiveArray));
  29. }
  30. }
Success #stdin #stdout 0.16s 321344KB
stdin
5
1
2
0
3
2
stdout
[1, 2, 0, 3, 2]
[1, 2, 3, 2]