解题思路:
注意事项:
参考代码:
import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] arr = new int[n]; //定义一个集合来接收非0的数据 ArrayList<Integer> list = new ArrayList<Integer>(); int count = 0; for (int i = 0; i < arr.length; i++) { arr[i] = sc.nextInt(); if (arr[i] > 0) { list.add(arr[i]); count++; } } System.out.println(count); for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i)+" "); } } }
0.0分
2 人评分