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类是与数学计算有关的类,里面的方法都是静态方法,直接使用类 ...
随机推荐
- PHP策略模式2
<?php /** PHP 策略模式 * 策略模式是对象的行为模式,用意是对一组算法的封装.动态的选择需要的算法并使用. * 策略模式指的是程序中涉及决策控制的一种模式.策略模式功能非常强大,因 ...
- centos7安装Lua
网官有介绍安装:http://www.lua.org/start.html 由于之前我遇到过致命错误并纪录在:https://blog.csdn.net/bingbingtea/article/det ...
- SQL中查询前几条或者中间某几行数据limit
SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset 使用查询语句的时候,要返回前几条或者中间某几行数据,用Llimit 可 ...
- git-代码同步至github
代码同步到github上 Last login: Wed Mar 27 13:53:06 on ttys001localhost:~ ligaijiang$ gitusage: git [--vers ...
- CentOS下samba配置心得(smb和nmb都要启动)
印象中以前多次配置成功过,重新配置就把以前的资料找出来: yum安装 samba samba-client samba-swat,然后配置参见:http://www.cnblogs.com/mchin ...
- 修改easydialog标题
使用easyui作为前台框架极大的节省了项目资源,easyui官网文档中基本上囊括了所有的方法,但一些灵活性的方法文档中是找不到的,比如说动态替换窗口的属性,下边简单介绍些如何快速替换窗体的title ...
- scala mysql jdbc oper
package egsql import java.util.Properties import com.sun.org.apache.xalan.internal.xsltc.compiler.ut ...
- c#如何判断字符串是否含中文
如代码: static bool ContainChinese(string input) { string pattern = "[\u4e00-\u9fbb]"; return ...
- 软工网络15团队作业4——Alpha阶段敏捷冲刺4.0
软工网络15团队作业4--Alpha阶段敏捷冲刺4.0 1.每天举行站立式会议,提供当天站立式会议照片一张. 2.项目每个成员的昨天进展.存在问题.今天安排. 成员 昨天已完成 今天计划完成 郭炜埕 ...
- Vue系列之 => 自定义全局指定让文本框自动获取焦点
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...