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类是与数学计算有关的类,里面的方法都是静态方法,直接使用类 ...
随机推荐
- 解决ScrollView中包含ListView,导致ListView显示不全
ScrollView 中包含 ListView 的问题 : ScrollView和ListView会冲突,会导致ListView显示不全 <?xml version="1.0" ...
- get请求乱码解决
1.修改tomcat的配置文件 <ConnectorURIEncoding="utf-8" connectionTimeout="20000" port= ...
- 【LeetCode每天一题】Combination Sum(组合和)
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...
- JMeter登录总是提示用户名不能为空的解决
已传入参数了呀,还是提示用户名不能为空 解决: 将url拼接上参数 --
- [LeetCode] 209. Minimum Size Subarray Sum_Medium
Given an array of n positive integers and a positive integer s, find the minimal length of a contigu ...
- gparted 不能起作用的时候,用fdisk
我用的是vmware,从网上下载的centos 6.3版本,通过yum update,其最后更新为6系列的最终版本6.7. 没有仔细看磁盘空间,因为某种原因,为了远程登录,我安装了xfce4桌面,后来 ...
- 6.C# 释放非托管资源2
C# 释放非托管资源 C#中资源分为托管资源和非托管资源. 托管资源由垃圾回收器控制如何释放,不需要程序员过多的考虑(当然也程序员也可以自己释放). 非托管资源需要自己编写代码来释放.那么编写好的释放 ...
- 解决React Native使用Fetch API请求网络报Network request failed
问题来源: 1 . 在测试fetch数据请求时,Xcode9.0以上的无法请求https, 需要在Xcode中加载项目后修改Info.plist的相关配置,具体如下参考 问题及解决方法一模一样,不再重 ...
- Bootstrap-按钮相关的class
.btn 基础class.btn-default 白底黑字的按钮.btn-warning 红色按钮.btn-success 绿色按钮.btn-info 浅蓝色按钮.bt ...
- python拼接变量、字符串的3种方法
第一种,加号(“+”): print 'py'+'thon' # output python str = 'py' print str+'thon' # output python 第二种 ,空格: ...