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类是与数学计算有关的类,里面的方法都是静态方法,直接使用类 ...
随机推荐
- 小程 序swiper自适应宽高
https://blog.csdn.net/qq_31604363/article/details/73715944 小程 序swiper自适应宽高 小程 序swiper自适应宽高
- Vim配色方案报错解决方案
求Linux大神解答,我刚刚从网上下载了个vim的配色方案,配置好后启动vim就报如下错误,怎么处理呢?小白,求不被鄙视~ 处理 /usr/share/vim/vim72/colors/rainbow ...
- Android使用SpannableString设置多样式文本
Android将一行文本设置为多种样式时,可以使用 SpannableString 来实现 private void setTips(){ String big = "大字深色"; ...
- 由ngx.say和ngx.print差异引发的血案
Jan 16, 2018openresty点击 最近上线一个项目,利用openresty在前面做反向代理,部分地址通过lua的http请求后端接口进行返回,在线下测试都没问题,公司预发灰度测试都通过了 ...
- 2018-2019-1 20189221《Linux内核原理与分析》第三周作业
2018-2019-1 20189221<Linux内核原理与分析>第三周作业 实验二 完成一个简单的时间片轮转多道程序内核代码 实验过程 在实验楼中编译内核 编写mymain.c函数和m ...
- 【Redis】持久化
Redis提供了为持久化提供了两种方法:第一种是快照:他可以将存在某一时刻的所有数据都写入硬盘里面.第二种是只追加文件(AOF):它会在执行命令时,将被执行的写命令复制到硬盘里面. Redis支持持久 ...
- 隐藏apache服务器信息
安装完apache一般第一时间都是关闭apache的版本信息,黑客会通过apache暴露出来的信息针对性的入侵,为了服务器的安全这些信息一定要及时关闭. 1.隐藏PHP版本 修改php.ini exp ...
- Nodejs基础(5-6)HTTP概念进阶
1.什么是回调? 是异步编程最基本的方法,对于nodejs来说需要按顺序执行异步逻辑的时候一般采用后续传递的方式,也就是将后续逻辑封装在回调函数中作为起始函数的参数逐层去嵌套.通过这种方式来让程序按照 ...
- OAuth2.0标准类库汇总
转载官网: https://oauth.net/code/ https://www.w3cschool.cn/oauth2/5ghz1jab.html 服务端类库 .NET .NET DotNetOp ...
- 从零开始一起学习SLAM | C++新特性要不要学?
LAM,C++编程是必备技能.不过,大家在学校里学习的书本一般比较老,主要还是C++98那些老一套. 本文所谓的C++新特性是指C++11及其以后的C++14.C++17增加的新关键字和新语法特性.其 ...