java实现删除数组重复数字(数组)

 int数组去除重复的数据,jdk8中使用IntStream把list转换成int数组 ,并附上冒泡排序(从小到大),(从大到小)

package demo.com.ceshi;

import java.util.ArrayList;
import java.util.List;
public class Maopao {
	public static void main(String[] args) {
		int array[] = {20,6,8,11,24,13,92,11};
		List list = new ArrayList();
		for (int i = 0; i < array.length; i++) {
                        //利用list集合的contains方法过滤重复数据
			if (!list.contains(array[i])) {
				list.add(array[i]);
			}
		}
                //遍历list集合
		for (Integer i : list) {
			System.out.print(i+",");
		}
                //利用java8的新特性stream转换成int数组
		int[] intArr = list.stream().mapToInt(Integer::intValue).toArray();
                //调用冒泡排序的方法
		getSortD_X(intArr);
		System.out.println();
		getSortX_D(array);
	}
	//冒泡排序(从大到小)
	private static void getSortD_X(int array[]) {
		for (int i = 0; i < array.length; i++) {
			for (int j = 0; j < array.length-1; j++) {
				if (array[i] > array[j]) {
					int temp = array[i];
					array[i] = array[j];
					array[j] = temp;
				}
			}
		}
		 System.out.print("从大到小:"); 
		 for (int i = 0; i < array.length; i++) {
			if (i == array.length-1) {
				System.out.print(array[i]);
			}else {
				System.out.print(array[i]+",");
			}
		}
		
	}
	//冒泡排序(从小到大)
	private static void getSortX_D(int array[]) {
		
		for (int i = 0; i < array.length; i++) {
			for (int j = 0; j < array.length-i-1; j++) {
				if (array[j] > array[j+1]) {
					int temp = array[j];
					array[j] = array[j+1];
					array[j+1] = temp;
				}
			}
		}
		System.out.print("从小到大:");
		for (int i = 0; i < array.length; i++) {
			if (i == array.length-1) {
				System.out.print(array[i]);
			}else {
				System.out.print(array[i]+",");
			}
		}
	}

}

2.运行结果

java实现删除数组重复数字(数组)_第1张图片

你可能感兴趣的:(java)