java求數組元素重複次數和java字串比較大小樣本_java

來源:互聯網
上載者:User

複製代碼 代碼如下:

/**
 * Name: 求數組中元素重複次數對多的數和重複次數
 * Description:
 * 數組中的元素可能會重複,這個方法可以找出重複次數最多的數,同時可以返回重複了多少次。
 * 但需要知道這個數組中最大的元素是多少,如果無法確定,就悲劇啦~
 *
 * @param array目標數組;
 *           max數組中資料的最大值;
 * @return 返回一個包含重複次數最多的數(value)和重複次數(maxCount)的map集合;
 *                  內部出現異常,預設返回0;
 * @throws
 * @Author 楊元
 */
public static Map<String, Integer> arraySearch(int[] array,int max){
  //結果集合
  Map<String, Integer> resultMap = new HashMap<String, Integer>();
  //重複的次數
  int maxCount = 0;
  //重複次數對多的數
  int value = 0;

  try{
    //初始化資料數組,用來存放每個元素出現的次數
    int[] dataArray = new int[max+1];

    //遍曆要尋找的數組,以每個元素為下標,直接定位元據數組,進行+1操作,表示出現了一次
    for(int i : array){
      dataArray[i]++;
    }

    //找到資料數組中最大值
    for(int i=0;i<dataArray.length;i++){
      if(dataArray[i]>maxCount){
        maxCount=dataArray[i];
        value=i;
      }
    }
  }catch (Exception e) {}

  resultMap.put("maxCount", maxCount);
  resultMap.put("value", value);

  return resultMap;
}

/**
 * Name: 比較兩個字串大小
 * Description: 比較的規則和資料庫中的order by效果一致;
 *                 null自動轉為空白,Null 字元串最大;
 *
 * @param first 要比較的第一個字串;
 *           second 要比較的第二個字串;
 * @return first大於second返回正數;
 *            first等於second返回0;
 *         first小於second返回負數;
 *         內部異常預設返回0;
 *         傳回值非固定值哦~~;
 * @throws
 * @Author 楊元
 */
public static int compareString(String first,String second){
  int result = 0;

  try{
    //null轉空
    first = first==null?"":first;
    second = second==null?"":second;

    //預先記錄字串長度,避免反覆讀取
    int firstLength=first.length();
    int secondLength=second.length();

    //處理含有空串的特殊情況
    if("".equals(first) || "".equals(second)){
      //誰長誰小
      result = secondLength-firstLength;
    }else{
      //臨時空間,用來存放ascii碼總和
      int firstCount = 0;
      int secondCount = 0;
      //用純運算得出兩個數中較小的數,實在是bt
      int minLength = (secondLength*(firstLength/secondLength) + firstLength*(secondLength/firstLength))/(firstLength/secondLength + secondLength/firstLength);
      //按兩個字串中較短的位元去逐位截取,防止越界
      for(int i=0;i<minLength;i++){
        //求ascii碼和
        firstCount+=first.substring(i,i+1).getBytes()[0];
        secondCount+=second.substring(i,i+1).getBytes()[0];
        //和不相等,說明已經比較出了大小
        if(firstCount!=secondCount){
          break;
        }
      }

      if(firstCount==secondCount){
        //長度長的大
        result = firstLength-secondLength;
      }else{
        //總和大的大
        result = firstCount-secondCount;
      }
    }
  }catch (Exception e) {}

  return result;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.