zoukankan      html  css  js  c++  java
  • java不用任何已有方法完全自写的去重法

    package aa;
    class InsertSort{
    	private long[] a;
    	private int nElems;
    	//构造方法
    	public InsertSort(int max){
    		a = new long[max];
    		nElems = 0;
    	}
    	//插入方法
    	public void insert(long value){
    		a[nElems] = value;
    		nElems ++;
    	}
    	//显示方法
    	public void display(){
    		for(int j = 0; j < nElems; j++)
    		{
    			System.out.println(a[j] + " ");
    		}
    		System.out.println("");
    	}
    	//排序方法
    	public long[] insertionSort(){
    		int out,in;
    		for(out = 1; out < nElems; out++)
    		{
    			long temp = a[out];
    			in = out;
    			while(in > 0 && a[in-1] >= temp)
    			{
    				a[in] = a[in - 1];
    				in --;
    			}
    			a[in] = temp;
    		}
    		return a;
    	}
    	//去重方法
    	public void noDups(long[] arr){
    		
    		for(int i = 1; i < nElems - 1; i++)//遍历数组元素,从第二项开始
    		{
    			for(int j = 0; j < i ; j++)//每一轮比较的次数
    			{
    				if(a[j] == a[i]) //如果遍历项和之前某一项相等
    				{
    					int k = i;//取出遍历项的索引
    					while(k+1 < nElems){//遍历 遍历项之后的元素项
    						a[k] = a[k+1];//将遍历项之后的元素项前移一位
    						k++;						
    					}
    					nElems --;//数组长度减一
    					i--;//因为遍历项后一项前移到遍历项,如果i不减一则少比较一项
    				}
    				
    			}
    		}
    	}
    }
    public class InsertSortApp {
    	public static void main(String[] args){
    		int maxSize = 100;
    		InsertSort arr = new InsertSort(maxSize);
    		
    		arr.insert(77);
    		arr.insert(99);
    		arr.insert(44);
    		arr.insert(55);
    		arr.insert(22);		
    		arr.insert(88);
    		arr.insert(11);
    		arr.insert(00);
    		arr.insert(77);
    		arr.insert(77);
    		
    		arr.display();
    		long[] sortarr = arr.insertionSort();
    		arr.display();
    		arr.noDups(sortarr);
    		arr.display();
    	}
    	
    }
    

      

  • 相关阅读:
    asp.net区
    推荐大家一个保护视力的Visual Studio皮肤
    微软Team Foundation Server相关技术
    TFS2008安装图解(详细版本)
    VS2008 ,TFS2008破解序列号
    分析器错误信息: 发现不明确的匹配。
    ASP.NET MVC Routing概述
    软件工程文档编写
    Visual SourceSafe学习小结
    aspnetpager 详解 (转)
  • 原文地址:https://www.cnblogs.com/iwebkit/p/7533206.html
Copyright © 2011-2022 走看看