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. hive load from hdfs出错

    使用hive load从hdfs中load data的时候,hiveql如下: load data inpath 'hdfs://192.168.0.131:9000/hive/test.log' o ...

  2. Blend4精选案例图解教程(五):可视数据管理

    原文:Blend4精选案例图解教程(五):可视数据管理 应用程序中我们会经常需要操作数据,在程序设计之初示例数据一般都是手工添加,Blend4提供了非常方便的数据管理能力,包括丰富的数据类型和内置示例 ...

  3. Ubuntu 13.04 用户安装 gnome 3.8 桌面

    昨天我试用了一把 Ubuntu gnome 13.04,如果你看了那一片文章:Ubuntu Gnome 13.04 体验截图.对 Ubuntu gnome 13.04 并不是采用的gnome 3.8 ...

  4. CF 148D. Bag of mice (可能性DP)

    D. Bag of mice time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  5. HDU4866 Shooting (要持久段树)

    意甲冠军: 给你一些并行x行轴.总是询问坐标x的顶部之前,k一个段高度,.标题是必须在线. 思路: 首先要会可持久化线段树(又称主席树和函数式线段树).不会的能够去做下POJ 2104. 把全部线段高 ...

  6. Directx11学习笔记【四】 封装一个简单的Dx11DemoBase

    根据前面两个笔记的内容,我们来封装一个简单的基类,方便以后的使用. 代码和前面类似,没有什么新的内容,直接看代码吧(由于代码上次都注释了,这次代码就没怎么写注释o(╯□╰)o) Dx11DemoBas ...

  7. HDU 2017 一系列统计数据

    一系列统计数据 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  8. Java读取图像和网络存储

    该公司最近在搞一个Web工程,需要下载网络图片,那么既然恢复了一些最基本的东西.数据传输不同的流,简单,很容易下载网络打破了样品的图片,代码非常easy.贡献给大家! 结论,图片主要就四步: 1:拿到 ...

  9. .net与Java的WebService互调

    本文记录一下.net与Java是如何进行Web Service的互相调用的. 1.准备工作 MyEclipse 10 JDK 1.6.0_13 Visual Studio 2012 .net fram ...

  10. Servlet实例解说

    打开昨天上午,负责人突然问我,client控制信息,如何让在后台?我想回答:假设总体提交form,在C#使用代码request获取表单的内容.假设局部提交,在用JS和Ajax交互,通过Ajax的ope ...