Java中使用自定义类封装数组,添加类方法实现数据操作
1、具体见注释
2、后续或有更新
public class MyArray {
private long[] array;
private int cnt; // 自定义数组类的元素个数
/**
使用自定义类封装数组,添加类方法实现数据操作
*/
public MyArray() {
array = new long[50];
}
public MyArray(int size) {
array = new long[size];
}
/**
插入数据,返回值为空
*/
public void insert(long insertValue) {
array[cnt++] = insertValue;
}
/**
显示数据,返回值为空
*/
public void display() {
System.out.print("[");
for (int i = 0; i < cnt ; ++i) {
System.out.print(array[i]);
if (i != cnt - 1) {
System.out.print(",");
}
}
System.out.println("]");
}
/**
按值查找数据,返回索引值
算法:线性查找
*/
public int search(long targetValue) {
int i;
int searchResult;
for (i = 0; i < cnt; ++i) {
if (targetValue == array[i]) {
break;
}
}
if (i == cnt) {
searchResult = -1;
} else {
searchResult = i;
}
return searchResult; // 保持单一出口
}
/**
按索引查找数据,返回值为目标数据
*/
public long get(int targetIndex) {
if (targetIndex < 0 || targetIndex >= cnt) {
throw new ArrayIndexOutOfBoundsException();
} else {
return array[targetIndex];
}
}
/**
按值删除数据,返回其索引值
*/
public int deleteByValue(long deleteValue) {
int i;
int deleteResult;
for (i = 0; i < cnt; ++i) {
if (array[i] == deleteValue) {
int j;
for (j = i; j < cnt-1; ++j) {
array[j] = array[j+1];
}
array[j] = array[--cnt];
break; // 仅删除从左到右第一个找到的目标值
}
}
if (i == cnt) {
deleteResult = -1;
} else {
deleteResult = i;
}
return deleteResult; // 保持单一出口
}
/**
按索引删除数据,返回值为空
*/
public void delete(int index) {
if (index < 0 || index >= cnt) {
throw new ArrayIndexOutOfBoundsException();
} else {
int i;
for (i = index; i < cnt - 1; ++i) {
array[i] = array[i + 1];
}
//array[i] = array[cnt - 1];
//cnt--;
array[i] = array[--cnt]; // 替换上两行
}
}
/**
根据索引值,更新数据,返回值为空
*/
public void update(int index, int newValue) {
if (index < 0 || index >= cnt) {
throw new ArrayIndexOutOfBoundsException();
} else {
array[index] = newValue;
}
}
public static void main(String[] args) {
MyArray array = new MyArray(3);
array.insert(13);
array.insert(34);
array.insert(90);
array.display();
array.deleteByValue(34);
array.display();
}
}
3、添加自定义有序数组类
public class MyOrderArray {
private long[] array;
private int cnt; // 自定义数组类的元素个数
/**
使用自定义类封装数组,添加类方法实现数据操作
*/
public MyOrderArray() {
array = new long[50];
}
public MyOrderArray(int size) {
array = new long[size];
}
/**
按序插入数据,返回值为空
*/
public void insert(long insertValue) {
int i;
for (i = 0; i < cnt; ++i) {
if (array[i] > insertValue) {
break;
}
}
int j;
for (j = cnt; j > i; --j) {
array[j] = array[j - 1];
}
array[i] = insertValue;
cnt++;
}
/**
显示数据,返回值为空
*/
public void display() {
System.out.print("[");
for (int i = 0; i < cnt ; ++i) {
System.out.print(array[i]);
if (i != cnt - 1) {
System.out.print(",");
}
}
System.out.println("]");
}
/**
按值查找数据,返回索引值
算法:线性查找
*/
public int search(long targetValue) {
int i;
int searchResult;
for (i = 0; i < cnt; ++i) {
if (targetValue == array[i]) {
break;
}
}
if (i == cnt) {
searchResult = -1;
} else {
searchResult = i;
}
return searchResult; // 保持单一出口
}
/**
按索引查找数据,返回值为目标数据
*/
public long get(int targetIndex) {
if (targetIndex < 0 || targetIndex >= cnt) {
throw new ArrayIndexOutOfBoundsException();
} else {
return array[targetIndex];
}
}
/**
按值删除数据,返回其索引值
*/
public int deleteByValue(long deleteValue) {
int i;
int deleteResult;
for (i = 0; i < cnt; ++i) {
if (array[i] == deleteValue) {
int j;
for (j = i; j < cnt-1; ++j) {
array[j] = array[j+1];
}
array[j] = array[--cnt];
break; // 仅删除从左到右第一个找到的目标值
}
}
if (i == cnt) {
deleteResult = -1;
} else {
deleteResult = i;
}
return deleteResult; // 保持单一出口
}
/**
按索引删除数据,返回值为空
*/
public void delete(int index) {
if (index < 0 || index >= cnt) {
throw new ArrayIndexOutOfBoundsException();
} else {
int i;
for (i = index; i < cnt - 1; ++i) {
array[i] = array[i + 1];
}
//array[i] = array[cnt - 1];
//cnt--;
array[i] = array[--cnt]; // 替换上两行
}
}
/**
根据索引值,更新数据,返回值为空
*/
public void update(int index, int newValue) {
if (index < 0 || index >= cnt) {
throw new ArrayIndexOutOfBoundsException();
} else {
array[index] = newValue;
}
}
public static void main(String[] args) {
MyOrderArray array = new MyOrderArray(3);
array.insert(90);
array.insert(13);
array.insert(34);
array.display();
array.deleteByValue(34);
array.display();
}
}
4、MyArray类与MyOrderArray类目前仅区别于insert方法,后续或有更新
5、MyOrderArray类新增二分查找方法binarySearch,具体细节见该方法代码
public class MyOrderArray {
private long[] array;
private int cnt; // 自定义数组类的元素个数
/**
使用自定义类封装数组,添加类方法实现数据操作
*/
public MyOrderArray() {
array = new long[50];
}
public MyOrderArray(int size) {
array = new long[size];
}
/**
按序插入数据,返回值为空
*/
public void insert(long insertValue) {
int i;
for (i = 0; i < cnt; ++i) {
if (array[i] > insertValue) {
break;
}
}
int j;
for (j = cnt; j > i; --j) {
array[j] = array[j - 1];
}
array[i] = insertValue;
cnt++;
}
/**
显示数据,返回值为空
*/
public void display() {
System.out.print("[");
for (int i = 0; i < cnt ; ++i) {
System.out.print(array[i]);
if (i != cnt - 1) {
System.out.print(",");
}
}
System.out.println("]");
}
/**
按值查找数据,返回索引值
算法:线性查找
*/
public int search(long targetValue) {
int i;
int searchResult;
for (i = 0; i < cnt; ++i) {
if (targetValue == array[i]) {
break;
}
}
if (i == cnt) {
searchResult = -1;
} else {
searchResult = i;
}
return searchResult; // 保持单一出口
}
/**
按值查找数据,返回索引值
算法:二分查找
*/
public int binarySearch(long targetValue) {
int middle = 0;
int low = 0;
int top = cnt;
while (true) {
middle = (top + low) / 2;
if (targetValue == array[middle]) {
return middle;
} else if (low > top) {
return -1;
} else if (targetValue < array[middle]) {
top = middle - 1; // 切记减一
} else if (targetValue >= array[middle]) {
low = middle + 1; // 切记加一
}
}
}
/**
按索引查找数据,返回值为目标数据
*/
public long get(int targetIndex) {
if (targetIndex < 0 || targetIndex >= cnt) {
throw new ArrayIndexOutOfBoundsException();
} else {
return array[targetIndex];
}
}
/**
按值删除数据,返回其索引值
*/
public int deleteByValue(long deleteValue) {
int i;
int deleteResult;
for (i = 0; i < cnt; ++i) {
if (array[i] == deleteValue) {
int j;
for (j = i; j < cnt-1; ++j) {
array[j] = array[j+1];
}
array[j] = array[--cnt];
break; // 仅删除从左到右第一个找到的目标值
}
}
if (i == cnt) {
deleteResult = -1;
} else {
deleteResult = i;
}
return deleteResult; // 保持单一出口
}
/**
按索引删除数据,返回值为空
*/
public void delete(int index) {
if (index < 0 || index >= cnt) {
throw new ArrayIndexOutOfBoundsException();
} else {
int i;
for (i = index; i < cnt - 1; ++i) {
array[i] = array[i + 1];
}
//array[i] = array[cnt - 1];
//cnt--;
array[i] = array[--cnt]; // 替换上两行
}
}
/**
根据索引值,更新数据,返回值为空
*/
public void update(int index, int newValue) {
if (index < 0 || index >= cnt) {
throw new ArrayIndexOutOfBoundsException();
} else {
array[index] = newValue;
}
}
public static void main(String[] args) {
MyOrderArray array = new MyOrderArray(3);
array.insert(90);
array.insert(13);
array.insert(34);
array.display();
//array.deleteByValue(34);
System.out.println(array.binarySearch(90));
array.display();
}
}
Java中使用自定义类封装数组,添加类方法实现数据操作的更多相关文章
- java中基于TaskEngine类封装实现定时任务
主要包括如下几个类: 文章标题:java中基于TaskEngine类封装实现定时任务 文章地址: http://blog.csdn.net/5iasp/article/details/10950529 ...
- Java中的Collections类
转载:https://blog.csdn.net/yangxingpa/article/details/80515963 从[Java]Java中的Collections类——Java中升级版的数据结 ...
- java中 列表,集合,数组之间的转换
java中 列表,集合,数组之间的转换 java中 列表,集合,数组之间的转换 java中 列表,集合,数组之间的转换 List和Set都是接口,它们继承Collection(集合),集合里面任何数据 ...
- java 中常用的类
java 中常用的类 Math Math 类,包含用于执行基本数学运算的方法 常用API 取整 l static double abs(double a) 获取double 的绝对值 l sta ...
- 【Java并发】Java中的原子操作类
综述 JDK从1.5开始提供了java.util.concurrent.atomic包. 通过包中的原子操作类能够线程安全地更新一个变量. 包含4种类型的原子更新方式:基本类型.数组.引用.对象中字段 ...
- 【Java】Java中的Collections类——Java中升级版的数据结构【转】
一般来说课本上的数据结构包括数组.单链表.堆栈.树.图.我这里所指的数据结构,是一个怎么表示一个对象的问题,有时候,单单一个变量声明不堪大用,比如int,String,double甚至一维数组.二维数 ...
- [转]Java中实现自定义的注解处理器
Java中实现自定义的注解处理器(Annotation Processor) 置顶2016年07月25日 19:42:49 阅读数:9877 在之前的<简单实现ButterKnife的注解功能& ...
- Java中的继承、封装、多态的理解
Java中的继承.封装.多态 继承的理解: 1.继承是面向对象的三大特征之一,也是实现代码复用的重要手段.Java的继承具有单继承的特点,每个子类只有一个直接父类. 2.Java的继承通过extend ...
- java中的常用类(二)
java中的常用类(二) Math类 Math类的声明:public final class Math extends Object Math类是与数学计算有关的类,里面的方法都是静态方法,直接使用类 ...
随机推荐
- vant - 弹框 【Popup 弹出层】【DatetimePicker 时间选择】
[HelloWorld.vue] <template> <div class="hello"> <van-row class="m-head ...
- 【LeetCode每天一题】Next Permutation(下一个排列)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 亲爱的,我是一条Linux运维技术学习路径呀。
根据我的经验,人在年轻时,最头疼的一件事就是决定自己这一生要做什么.在这方面,我倒没有什么具体的建议:干什么都可以,但最好不要写小说,这是和我抢饭碗.总而言之,干什么都是好的:但要干出个样子来,这才是 ...
- [Java in NetBeans] Lesson 06. Custom classes
这个课程的参考视频和图片来自youtube. 主要学到的知识点有: Constructors: A special method called when an object of the class ...
- H5缩放效果的问题和缓存问题
https://segmentfault.com/q/1010000000305316 http://blog.csdn.net/hudashi/article/details/50963585 四. ...
- Win10 JDK 配置
分两行建,点击新建, %JAVA_HOME%\bin %JAVA_HOME%\jre\bin
- Hybrid设计--如何落地一个Hybrid项目
前后分离 -> 统一前端框架 -> 同一个账号体系 -> 登录注册的公共页 -> 有了这些公共业务后 推行 -> Hybrid 技术 底层容器开发出来后 -&g ...
- cocos2dx 3.x(打开网页webView)
#include "ui/CocosGUI.h" using namespace cocos2d::experimental::ui; WebView *webView = Web ...
- Oracle之SQL优化专题02-稳固SQL执行计划的方法
首先构建一个简单的测试用例来实际演示: create table emp as select * from scott.emp; create table dept as select * from ...
- Mysql修改字段类型,修改字段名
mysql修改字段名: ALTER TABLE 表名 CHANGE 旧字段名 新字段名 新数据类型; 参考:https://blog.csdn.net/u010002184/article/detai ...