博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【DataStructure】Some useful methods for arrays
阅读量:5306 次
发布时间:2019-06-14

本文共 3986 字,大约阅读时间需要 13 分钟。

Last night it took me about two hours to learn arrays. For the sake of less time, I did not put emphaises on the practice question, just now when reading the book, I found that some methods referred to arrays are so beneficial to us. So in here make a simple summary.

Method 1: Check whether the array is sorted. 

private static boolean isSorted(int[] a) {    if (a.length < 2) {      return true;    }    for (int i = 1; i < a.length; i++) {      if (a[i] < a[i-1]) {        return false;      }    }    return true;  }}
     Method 2:  Use the start number and range to init the array

public static void load(int[] a, int start, int range) {    for (int i = 0; i < a.length; i++) {      a[i] = start + random.nextInt(range);  // random 5-digit numbers    }  }
    Method 3:  Get the min number from the array

private static int minimum(int[] a) {    int min = a[0];    for (int i = 1; i < a.length; i++) {      if (a[i] < min) {        min = a[i];      }    }    return min;  }
  Method 4: Remove the duplicate elements from object

private static int[] withoutDuplicates(int[] a) {    int n = a.length;    if (n < 2) {      return a;    }    for (int i = 0; i < n-1; i++) {      for (int j = i+1; j < n; j++) {        if (a[j] == a[i]) {          --n;          System.arraycopy(a, j+1, a, j, n-j);          --j;        }      }    }    int[] aa = new int[n];    System.arraycopy(a, 0, aa, 0, n);    return aa;  }
  Method 5: Finds the prime number according to certain range

private static final int SIZE=1000;  private static boolean[] isPrime = new boolean[SIZE];  private static void initializeSieve() {      for (int i = 2; i < SIZE; i++) {        isPrime[i] = true;      }      for (int n = 2; 2*n < SIZE; n++) {        if (isPrime[n]) {          for (int m = n; m*n 

Another way of implement the function of finding the prime number(Vector)

private static final int SIZE=1000;  private static Vector
isPrime = new Vector
(SIZE); private static void initializeSieve() { isPrime.add(false); // 0 is not prime isPrime.add(false); // 1 is not prime for (int i = 2; i < SIZE; i++) { isPrime.add(true); } for (int n = 2; 2*n < SIZE; n++) { if ((isPrime.get(n))) { for (int m = n; m*n < SIZE; m++) { isPrime.set(m*n, false); } } } }

Another way of implement the function of finding the prime number(BitSet)

private static final int SIZE=1000;  private static BitSet isPrime = new BitSet(SIZE);  private static void initializeSieve() {      for (int i = 2; i < SIZE; i++) {        isPrime.set(i);      }      for (int n = 2; 2*n < SIZE; n++) {        if (isPrime.get(n)) {          for (int m = n; m*n 
Method 6: Print out the result according to the certain format:

public static void printSieve() {    int n=0;    for (int i = 0; i < SIZE; i++) {      if (isPrime[i]) {        System.out.printf("%5d%s", i, ++n%16==0?"\n":"");      }    }    System.out.printf("%n%d primes less than %d%n", n, SIZE);  }
Notes: There exists five spaces between each number, and it will change line when the length of char  % 6 is zero.

2    3    5    7   11   13   17   19   23   29   31   37   41   43   47   53   59   61   67   71   73   79   83   89   97  101  103  107  109  113  127  131  137  139  149  151  157  163  167  173  179  181  191  193  197  199  211  223  227  229  233  239  241  251  257  263  269  271  277  281  283  293  307  311  313  317  331  337  347  349  353  359  367  373  379  383  389  397  401  409  419  421  431  433  439  443  449  457  461  463  467  479  487  491  499  503  509  521  523  541  547  557  563  569  571  577  587  593  599  601  607  613  617  619  631  641  643  647  653  659  661  673  677  683  691  701  709  719  727  733  739  743  751  757  761  769  773  787  797  809  811  821  823  827  829  839  853  857  859  863  877  881  883  887  907  911  919  929  937  941  947  953  967  971  977  983  991  997

转载于:https://www.cnblogs.com/mfrbuaa/p/4091859.html

你可能感兴趣的文章
Scala入门(1)Linux下Scala(2.12.1)安装
查看>>
如何改善下面的代码 领导说了很耗资源
查看>>
Quartus II 中常见Warning 原因及解决方法
查看>>
php中的isset和empty的用法区别
查看>>
Android ViewPager 动画效果
查看>>
pip和easy_install使用方式
查看>>
博弈论
查看>>
Redis sentinel & cluster 原理分析
查看>>
我的工作习惯小结
查看>>
把word文档中的所有图片导出
查看>>
浏览器的判断;
查看>>
ubuntu 18.04取消自动锁屏以及设置键盘快捷锁屏
查看>>
Leetcode 589. N-ary Tree Preorder Traversal
查看>>
机器学习/深度学习/其他开发环境搭建记录
查看>>
xml.exist() 实例演示
查看>>
判断是否为空然后赋值
查看>>
zabbix监控日志文件
查看>>
正则表达式
查看>>
pip install torch on windows, and the 'from torch._C import * ImportError: DLL load failed:' s...
查看>>
环套树
查看>>