传统的数组常规操作(JAVA实现)
2013-04-27 21:56
411 查看
程杰的《大话数据结构》是以C作为演示代码的。
我觉得如何要通HADOOP及其应用,JAVA的数据结构和算法的基础知识也必不可少的。
于是网上找了本电子书《JAVA算法和数据结构》第二版中文版。跟着看一看。
这书也不差哟。
下面的代码是为了引出类的优势,以传统基于过程的算法作比较。
我个人在里面增加了数组排序和元素增加演示。
我觉得如何要通HADOOP及其应用,JAVA的数据结构和算法的基础知识也必不可少的。
于是网上找了本电子书《JAVA算法和数据结构》第二版中文版。跟着看一看。
这书也不差哟。
下面的代码是为了引出类的优势,以传统基于过程的算法作比较。
我个人在里面增加了数组排序和元素增加演示。
public class hello { /** * @param args */ public static void main(String[] args) { long[] arr; arr = new long[100]; int nElems = 0; int j; int i; long swap; long searchKey; arr[0] = 77; arr[1] = 99; arr[2] = 44; arr[3] = 55; arr[4] = 22; arr[5] = 88; arr[6] = 11; arr[7] = 00; arr[8] = 66; arr[9] = 33; nElems = 10; for(j = 0; j < nElems; j++) System.out.print(arr[j] + " "); System.out.println(""); searchKey = 66; for(j = 0; j < nElems; j++) if(arr[j] == searchKey) break; if(j == nElems) System.out.println("Can't find " + searchKey ); else System.out.println("Found " + searchKey); searchKey = 55; for(j = 0; j < nElems; j++) if(arr[j] == searchKey) break; for(int k = j; k < nElems; k++) arr[k] = arr[k+1]; nElems--; System.out.println("Delete " + searchKey); for(j = 0; j < nElems; j++) System.out.print(arr[j] + " "); System.out.println(""); searchKey = 55; nElems++; arr[nElems-1] = searchKey; System.out.println("Add " + searchKey + " to the end;"); for(j = 0; j < nElems; j++) System.out.print(arr[j] + " "); System.out.println(""); for(i = 0; i < nElems; i++) { for(j = i + 1; j < nElems; j++) { if (arr[i] < arr[j]) { swap = arr[j]; arr[j] = arr[i]; arr[i] = swap; } } } System.out.println("After sort:"); for(j = 0; j < nElems; j++) System.out.print(arr[j] + " "); System.out.println(""); } }
相关文章推荐
- java实现数组的六项操作
- 数组操作大全<Java实现>
- Java 用数组实现栈 (Stack),包括栈的初始化,入栈、出栈等操作
- 堆排序,堆增删操作,Java数组实现堆排序
- Java实现的数组去重与排序操作详解
- Java与JavaScript 完美实现字符串拆分(利用数组存储)与合并的互逆操作
- Java实现数组栈的操作
- Java 数据结构之数组的操作三:实现各种排序方法
- 用java实现数组的循环逐步移位操作
- Java学习,数组的相关操作,封装类,Class类,反射API
- 实现基于静态数组的顺序表的以下基本操作(进阶部分)
- 链表的各种操作(Java实现)
- java (数组操作)
- Java 数据结构链表操作实现代码
- Java学习 jstl循环操作数组
- java操作数组的常用类
- Java开发学习 Java数组操作工具
- (旋转数组问题)给定一个整数类型的循环有序数组,求循环数组的特定值,使用二分查找法(JAVA实现)
- java操作cookie 实现两周内自动登录 加密安全性问题
- Java语言实现数组的键盘输入(综合几种方法实现)