数据结构1:数组专题
来源:互联网 发布:c语言头文件stdio 编辑:程序博客网 时间:2024/05/22 01:37
本文主要是针对数据结构中最基本的数组的增删改查工具类。
基本数组的增删改查工具类:
package ch01;public class MyArray { private long[] arr; //表示有效数据的长度 private int elements; public MyArray() { arr = new long[50]; } public MyArray(int maxsize) { arr = new long[maxsize]; } /** * 添加数据 */ public void insert(long value) { arr[elements] = value; elements++; } /** * 显示数据 */ public void display() { System.out.print("["); for(int i = 0; i < elements; i++) { System.out.print(arr[i] + " "); } System.out.println("]"); } /** * 查找数据 */ public int search(long value) { int i; for(i = 0; i < elements; i++) { if(value == arr[i]) { break; } } if(i == elements) { return -1; } else { return i; } } /** * 查找数据,根据索引来查 */ public long get(int index) { if(index >= elements || index < 0) { throw new ArrayIndexOutOfBoundsException(); } else { return arr[index]; } } /** * 删除数据 */ public void delete(int index) { if(index >= elements || index < 0) { throw new ArrayIndexOutOfBoundsException(); } else { for(int i = index; i < elements; i++) { arr[index] = arr[index + 1]; } elements--; } } /** * 更新数据 */ public void change(int index, int newvalue) { if(index >= elements || index < 0) { throw new ArrayIndexOutOfBoundsException(); } else { arr[index] = newvalue; } }}
排序数组的增删改查,由于已经排好序了,所以可以通过二分查找方法查找指定的元素值所在数组的位置:
package ch01;public class MyOrderArray { private long[] arr; //表示有效数据的长度 private int elements; public MyOrderArray() { arr = new long[50]; } public MyOrderArray(int maxsize) { arr = new long[maxsize]; } /** * 添加数据 */ public void insert(long value) { int i; for(i = 0; i < elements; i++) { if(arr[i] > value) { break; } } for(int j = elements; j > i; j--) { arr[j] = arr[j - 1]; } arr[i] = value; elements++; } /** * 显示数据 */ public void display() { System.out.print("["); for(int i = 0; i < elements; i++) { System.out.print(arr[i] + " "); } System.out.println("]"); } /** * 查找数据 */ public int search(long value) { int i; for(i = 0; i < elements; i++) { if(value == arr[i]) { break; } } if(i == elements) { return -1; } else { return i; } } /** * 二分法查找数据 */ public int binarySearch(long value) { int middle = 0; int low = 0; int pow = elements; while(true) { middle = (pow + low) / 2; if(arr[middle] == value) { return middle; } else if(low > pow) { return -1; } else { if(arr[middle] > value) { pow = middle - 1; } else { low = middle + 1; } } } } /** * 查找数据,根据索引来查 */ public long get(int index) { if(index >= elements || index < 0) { throw new ArrayIndexOutOfBoundsException(); } else { return arr[index]; } } /** * 删除数据 */ public void delete(int index) { if(index >= elements || index < 0) { throw new ArrayIndexOutOfBoundsException(); } else { for(int i = index; i < elements; i++) { arr[index] = arr[index + 1]; } elements--; } } /** * 更新数据 */ public void change(int index, int newvalue) { if(index >= elements || index < 0) { throw new ArrayIndexOutOfBoundsException(); } else { arr[index] = newvalue; } }}
阅读全文
1 0
- 数据结构1:数组专题
- 数据结构2:数组排序专题
- 数据结构专题
- 数据结构专题
- 数据结构专题
- 数据结构专题
- 数据结构专题
- 数组专题
- 数组专题
- 专题1,2 数据结构(学习笔记)
- 唐老师数据结构专题八1
- 【一、Leetcode数组专题(1)easy】
- 数据结构数组(1)
- 数据结构---数组(1)
- javascript数据结构1-数组
- 专题集训--数据结构
- UESTC2015数据结构专题
- 【暑假专题训练#数据结构】
- ubuntu安装wifi驱动
- Mac SVN
- 面向对象
- java实现二进制转十六进制
- 关于iReport5.6.0无法正常启动或者闪退或者JDK8不兼容的解决方案
- 数据结构1:数组专题
- 问题 A: 龟兔赛跑预测
- React Native 环境搭建
- 20170729Python03_字符串
- 八个算法题
- kubernetes安全控制认证与授权(二)
- Java多线程系列3(线程同步)
- 个人对“同格”的看法
- 笔试1