⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sortandprunecomposites.java

📁 Java开发最新的日志记录工具slf4j的源码
💻 JAVA
字号:
package org.slf4j.profiler;

import java.util.ArrayList;
import java.util.Arrays;

public class SortAndPruneComposites {

  static String NESTED_PROFILER_NAME = "SORT_AND_PRUNE";
  
  final int[] originalArray;
  final int originalArrrayLength;
  
  public SortAndPruneComposites(int[] randomArray) {
    this.originalArray = randomArray;
    this.originalArrrayLength = randomArray.length;
   
  }
  
  public int[] sortAndPruneComposites() {
    // retrieve previously registered profiler named "SORT_AND_PRUNE"
    ProfilerRegistry profilerRegistry = ProfilerRegistry.getThreadContextInstance();
    Profiler sortProfiler = profilerRegistry.get(NESTED_PROFILER_NAME);

    // start a new stopwatch called SORT
    sortProfiler.start("SORT");
    int[] sortedArray = sort();
    // start a new stopwatch called PRUNE_COMPOSITES
    sortProfiler.start("PRUNE_COMPOSITES");
    int result[] = pruneComposites(sortedArray);
    
    return result;
  }

  private int[] sort() {
    int[] sortedArray = new int[originalArrrayLength];
    System.arraycopy(originalArray, 0, sortedArray, 0, originalArrrayLength);
    Arrays.sort(sortedArray);
    return sortedArray;
  }
  
  int[] pruneComposites(int[] sortedArray) {
    ArrayList<Integer> primesArray = new ArrayList<Integer>();
    for(int i = 0; i < originalArrrayLength; i++) {
      int n = sortedArray[i];
      if(isPrime(n)) {
        primesArray.add(n);
      }
    }
    int resultSize = primesArray.size();
    int[] result = new int[resultSize];
    
    for(int i = 0; i < resultSize; i++) {
      result[i] = primesArray.get(i);
    }
    return result;
  }

  public boolean isPrime(int n) {
    if(n < 2) {
      return false;
    }
    if(n%2 == 0) {
      return false;
    }
    for(int i = 3; i*i <=n; i += 2) {
      if(n%i ==0) {
        return false;
      }
    }
    return true;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -