public static void main(String[] args) { Integer[] nums = new Integer[]{2,5,4,7,8,9,6,4}; List<Integer> list = new ArrayList<Integer>(); for (int i = 0; i < nums.length; i++) { if (!list.contains(nums[i])) { list.add(nums[i]); } } for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } }
}
小面包
2024-08-01 16:47:07
1、如果打印结果按从小到大的顺序(jdk1.5及以上): import java.util.HashSet; import java.util.Set; public class SetTest { public static void main(String[] arg){ int [] a = {2,5,4,7,8,9,6,4}; Set<Integer> data = new HashSet<Integer>(); for (int i : a) { data.add(i); } for (Object object : data) { System.out.println(object); } } } //2、按照原有顺序输出 public class SetTest { public static void main(String[] arg){ int [] a = {2,5,4,7,8,9,6,4}; for(int i=0; i<a.length; i++){ int j = 0; for(j=0; j<i; j++){ if(a[i]==a[j]) break; } if(i==j) System.out.print(a[i]+" "); } } }