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 <SIZE; m++) {
isPrime[m*n] = false;
}
}
}
}

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

  private static final int SIZE=1000;
private static Vector<Boolean> isPrime = new Vector<Boolean>(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 <SIZE; m++) {
isPrime.clear(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

【DataStructure】Some useful methods for arrays的更多相关文章

  1. 【DataStructure】Some useful methods about linkedList(二)

    Method 1: Add one list into the other list. For example, if list1is {22, 33, 44, 55} and  list2 is { ...

  2. 【DataStructure】Some useful methods about linkedList(三)

    Method 4: Gets the value of element number i For example, if list is {22, 33, 44, 55, 66, 77, 88, 99 ...

  3. 【DataStructure】Some useful methods about linkedList.

    /** * Method 1: Delete the input element x  * and meanwhile keep the length of array after deleted n ...

  4. 【DataStructure】Description and usage of queue

    [Description] A queue is a collection that implements the first-in-first-out protocal. This means th ...

  5. 【DataStructure】Description and Introduction of Tree

    [Description] At ree is a nonlinear data structure that models a hierarchical organization. The char ...

  6. 【DataStructure】One of queue usage: Simulation System

    Statements: This blog was written by me, but most of content  is quoted from book[Data Structure wit ...

  7. 【DataStructure】The difference among methods addAll(),retainAll() and removeAll()

    In the Java collection framework, there are three similar methods, addAll(),retainAll() and removeAl ...

  8. 【DataStructure】Charming usage of Set in the java

    In an attempt to remove duplicate elements from list, I go to the lengths to take advantage of  meth ...

  9. 【leetcode】Median of Two Sorted Arrays

    题目简述: There are two sorted arrays A and B of size m and n respectively. Find the median of the two s ...

随机推荐

  1. Linux 0.12 内核管理存储器

    Linux 0.12 内核管理存储器 其分段,用分段的机制把进程间的虚拟地址分隔开. 每一个进程都有一张段表LDT.整个系统有一张GDT表.且整个系统仅仅有一个总页表. 其地址翻译过程为: 程序中给出 ...

  2. C/C++大型项目错误管理

    在C/C++大型项目中,错误管理在项目中起着举足轻重的作用,以我自己的项目经验以及观摩其它项目,错误管理对项目框架以及开发效率有着非常大的影响.对于错误管理的认识大致分为三类: 刚刚開始敲代码的新手, ...

  3. HTML5实际和离线应用分析

    当前离线Web申请书,即,该装置不能访问因特网时的应用的执行.HTML5离线应用重点,主要开发人员希望.步骤离线应用开发有:首先我们应该知道设备是否可以连接;然后,它也应该可以访问某些资源(像.CSS ...

  4. IE按F12,开发者工具已经在最低点,那么国家就不会出错

    ie浏览器.按F12,开发工具将问世. 没有,试试下面的方法就可以解决: 开开发者工具图标,它会出来缩略图,于缩略图右键点-你可以最大限度地提高.然后选择IE8兼容模式可 版权声明:本文博主原创文章. ...

  5. Apple Watch 1.0 开发介绍 2.1 WatchKit Apps UI要点

    实现app的开始是定义storyboard场景.每个场景定义了app的一部分界面.可以为不同的尺寸自定义场景. 组装storyboard界面 WatchKit app和iOS app的布局模式不同.组 ...

  6. Hello ASP.NET5

    2015年11月30日, ASP.NET 5 RC1 已经发布,本文尝试了一下ASP.NET5项目的创见一发布到IIS.开发环境,win10 64位,visual studio2015(已更新upda ...

  7. 分布式服务框架 dubbo/dubbox 入门示例(转)

    dubbo是一个分布式的服务架构,可直接用于生产环境作为SOA服务框架. 官网首页:http://dubbo.io/ ,官方用户指南 http://dubbo.io/User+Guide-zh.htm ...

  8. 文章之间的基本总结:Activity生命周期

    孔子:温故而知新.它可以作为一个教师.<论语> 同样的学习技巧.对于技术文件或书籍的经典技术,期待再次看到它完全掌握,这基本上是不可能的,所以,我们常常回来几次,然后仔细研究,为了理解作者 ...

  9. 探索C/C++大数快(自然数)模板

    本文fcbruce个人原创整理.转载请注明出处http://blog.csdn.net/u012965890/article/details/40432511,谢谢. 我们知道在C/C++中int型可 ...

  10. [生产环境数据恢复]innobackupex: fatal error: OR no &#39;datadir&#39; option in group &#39;mysqld&#39; in MySQL options

    1 运行恢复命令  [xxx@xxx-c001db1 tmp]$ time /usr/bin/innobackupex --rsync --user="user" --passwo ...