java算法:数组
2012-10-26 12:43
239 查看
java算法:数组
数组是最基本的数据结构。在java和大多数编程语言中都被定义为简单类型。数组的使用是开发有效算法的基础。数组是相同类型数据的固定集合,它是连续存储的,通过下标来访问数组元素。由于它是与计算机的内存系统直接通讯,可以看成是最基本的数据结构。
例一:埃拉托色尼筛,打印出小于给定N的所有素数。
Java代码
public class Primes { public static void main(String[] args) { int N = Integer.parseInt(args[0]); boolean [] a = new boolean ; for(int i = 2; i < N ; i++){ a[i] = true; } for(int i = 2; i < N ; i++){ if(a[i] != false){ for(int j = i; j * i < N; j++){ a[i * j] = false; } } } for(int i = 2; i < N ; i++){ if(i > N - 100){ if(a [i]){ System.out.println(" " + i); } } } } }
public class Primes { public static void main(String[] args) { int N = Integer.parseInt(args[0]); boolean [] a = new boolean ; for(int i = 2; i < N ; i++){ a[i] = true; } for(int i = 2; i < N ; i++){ if(a[i] != false){ for(int j = i; j * i < N; j++){ a[i * j] = false; } } } for(int i = 2; i < N ; i++){ if(i > N - 100){ if(a [i]){ System.out.println(" " + i); } } } } }
上述代码,计算一个布尔型的数组,如果i是素数,a[i]为true,否则,a[i]为false。首先,把数组中的所有元素置为true,然后把对应下标为非素数(是已知素数的倍数)的数组元素设为false。如果在所有较小素数的倍数都被设为false值后,a[i]的值仍为true,那么它肯定是一个素数。
对于其他对象而言,对数组的引用很有价值,因为它们可以把数组作为高级对象来有效地使用。
例二:健壮的数组分配
如果程序使用者敲入一个巨大的数作为命令行参数,它可能产生OutOfMemoryError异常错误,因此:
Java代码
boolean [] a; try{ a = new boolean ; }catch(OutOfMemoryException e){ System.out.println("Out of memory."); }
boolean [] a; try{ a = new boolean ; }catch(OutOfMemoryException e){ System.out.println("Out of memory."); }
数组不仅准确地反映在大多数计算机上访问内存数据的基本方法,而且在应用中获得了广泛的应用,如,数组直接对应着向量,是描述具有下标的对象列表的数学术语。
例三:掷硬币模拟
Java代码
public class CoinFlip { static boolean heads(){ return Math.random() < 0.5; } public static void main(String[] args) { int i,j,cnt = 0; int N = Integer.parseInt(args[0]); int M = Integer.parseInt(args[1]); int [] f = new int[N + 1]; for (j = 0; j <= N; j++) { f[j]=0; } for (i = 0; i < M; i++, f[cnt]++) { for(cnt = 0, j = 0; j <= N; j++){ if(heads()){ cnt++ ; } } } for(j = 0; j <= N; j++){ if(f[j] == 0){ System.out.print("."); } for(i = 0; i < f[j]; i+= 10){ System.out.print("*"); } System.out.println(""); } } }
public class CoinFlip { static boolean heads(){ return Math.random() < 0.5; } public static void main(String[] args) { int i,j,cnt = 0; int N = Integer.parseInt(args[0]); int M = Integer.parseInt(args[1]); int [] f = new int[N + 1]; for (j = 0; j <= N; j++) { f[j]=0; } for (i = 0; i < M; i++, f[cnt]++) { for(cnt = 0, j = 0; j <= N; j++){ if(heads()){ cnt++ ; } } } for(j = 0; j <= N; j++){ if(f[j] == 0){ System.out.print("."); } for(i = 0; i < f[j]; i+= 10){ System.out.print("*"); } System.out.println(""); } } }
从某种意义上说,当我们使用一个计算过的值来访问大小为N的数组时,只用一个操作就可以把N种可能性都考虑进去,效率上有了很大的提高。
例四:最近点计算
Java代码
public class ClosePoints { public static void main(String[] args) { int cnt = 0; int N = Integer.parseInt(args[0]); double d = Double.parseDouble(args[1]); Point [] a = new Point ; for(int i = 0; i < N; i++){ a[i] = new Point(); } for(int i = 0; i < N; i++){ for(int j = i + 1; j < N; j++){ if(a[i].distance(a[j]) < d){ cnt++; } } } System.out.println(cnt + " pairs closer than " + d); } }
public class ClosePoints { public static void main(String[] args) { int cnt = 0; int N = Integer.parseInt(args[0]); double d = Double.parseDouble(args[1]); Point [] a = new Point ; for(int i = 0; i < N; i++){ a[i] = new Point(); } for(int i = 0; i < N; i++){ for(int j = i + 1; j < N; j++){ if(a[i].distance(a[j]) < d){ cnt++; } } } System.out.println(cnt + " pairs closer than " + d); } }
作为一个原型二次算法,它检查了N个数据项集合的所有数据对时,因此所花的时间与N平方成正比。
相关文章推荐
- 一道对称数组的JAVA算法题
- Java算法实现之最大子数组
- 数组逆序java算法
- Java算法——数组
- Java算法 从一个数组中随机选择一部分数
- java算法6~在其他数都出现偶数次的数组中找到出现奇数次的数
- [JAVA算法]求子数组的最大和
- 数组随机求和比较的Java算法
- Java算法给定一个整数数组,找出其中两个数相加等于目标值
- Java算法 从一个数组中随机选择一部分数
- java算法:数组
- java算法3(关于递归的算法和数组的排序)
- Java算法 奇数偶数数组删除操作
- 三个数组求中位数,以及中位数的中位数----java算法实现
- java算法——数组中只实现一次的数字
- Java算法学习-------插入排序算法(整数数组类型,可比较数组类型,可比较线性表类型)
- java算法:数组
- Java算法系列1--基于数组自定义栈
- java算法之下压栈来动态调整数组大小
- java算法7~求子数组的最大累加和问题