Java数据结构---基于数组的表
作者:景科儒_189 | 来源:互联网 | 2017-08-09 03:43
Java数据结构---基于数组的表--Linux通用技术-Linux编程与内核信息,下面是详情阅读。
我没看过 其他语言版的数据结构,但觉得java的实现方法很巧妙--用类和对象来实现.基于数组的表,思想很简单就是定义一个类用来存储一组数据,我定义的是ArrayListClass类,在类中定义用来操作数组的方法.其实就是 这么简单,但具体操作起来就会遇到很多麻烦了!
我们这个ArrayListClass类中首先应该包括一个数组型的域list,用来存放数据,这样放在同一数组中数据之间就产生了位置上的联系,使对数据的操作便的简单.然而这个数组到底是什么数据类型的,我们期望这个表能用于所有的数据类型,我们不能将他单纯的固定成某一种.所以我们必须将这个数据普通化,解决的办法就是定义一个类,作为所有数据类型的超类.看这个DataElement:
public abstract class DataElement {
public abstract boolean equals(DataElement otherElement);
public abstract int compareTo(DataElement otherElement);
public abstract void makeCopy(DataElement otherElement);
public abstract DataElement getCopy();
}
将他定义成为抽象的,再在定义其他数据类型时继承并实现它,我定义了两个数据类型IntElement和StringElement:
IntElement:
public class IntElement extends DataElement {
protected int num;
//constructors
public IntElement(){
num=0;
}
public IntElement(int number){
num=number;
}
public IntElement(IntElement otherElement){
num=otherElement.num;
}
///get-set Methods
public void setNum(int number){
num=number;
}
public int getNum(){
return num;
}
/* (non-Javadoc)
* @see DataElement#equals(DataElement)
*/
public boolean equals(DataElement otherElement) {
// TODO Auto-generated method stub
IntElement newe=(IntElement)otherElement;
return (this.num==newe.num);
}
/* (non-Javadoc)
* @see DataElement#compareTo(DataElement)
*/
public int compareTo(DataElement otherElement) {
// TODO Auto-generated method stub
IntElement newe=(IntElement)otherElement;
if(this.num==newe.num)
return 0;
else if(this.num>newe.num)
return 1;
else
return -1;
}
/* (non-Javadoc)
* @see DataElement#makeCopy(DataElement)
*/
public void makeCopy(DataElement otherElement) {
// TODO Auto-generated method stub
IntElement newe=(IntElement)otherElement;
this.num=newe.num;
}
/* (non-Javadoc)
* @see DataElement#getCopy()
*/
public DataElement getCopy() {
// TODO Auto-generated method stub
IntElement newElement=new IntElement();
newElement.num=this.num;
return newElement;
}
public String toString(){
return String.valueOf(num);
}
}
StringElement:
public class StringElement extends DataElement {
/**
*
*/
private String str;
//constructors
public StringElement() {
str=null;
}
public StringElement(String string){
str=string;
}
public StringElement(StringElement otherElement){
str=otherElement.str;
}
//get-set Methods
public void setStr(String string){
str=string;
}
public String getStr(){
return str;
}
/* (non-Javadoc)
* @see DataElement#equals(DataElement)
*/
public boolean equals(DataElement otherElement) {
// TODO Auto-generated method stub
StringElement newe=(StringElement)otherElement;
return (str==newe.str);
}
/* (non-Javadoc)
* @see DataElement#compareTo(DataElement)
*/
public int compareTo(DataElement otherElement) {
// TODO Auto-generated method stub
StringElement newe=(StringElement)otherElement;
return (str.compareTo(newe.str));
}
/* (non-Javadoc)
* @see DataElement#makeCopy(DataElement)
*/
public void makeCopy(DataElement otherElement) {
// TODO Auto-generated method stub
StringElement newe=(StringElement)otherElement;
str=newe.str;
}
/* (non-Javadoc)
* @see DataElement#getCopy()
*/
public DataElement getCopy() {
// TODO Auto-generated method stub
StringElement othere=new StringElement();
othere.str=str;
return othere;
}
public String toString(){
return str;
}
}
已经定义好了数据类型,所以list的数据类型我们就可以定义为DateElement[]了,这样就可以包括所以你想要的了,只要你在用的时候定义一个DataElement的子类就行了,这正是java继承的精髓所在.我们接着定义ArrayListClass类:
protected int length;
protected int maxSize;
protected DataElement[] list;这就是它的所有域了.
接下来就是它的方法了,我们对表的操作应该有很多种,比如插入、查询、删减等等,我们要逐个的实现,具体方法不再赘述,且看最后完成代码
public abstract class ArrayListClass {
//fields
protected int length;
protected int maxSize;
protected DataElement[] list;
//defalt constructors
public ArrayListClass(){
length=0;
maxSize=100;
list=new DataElement[maxSize];
}
//constructors
public ArrayListClass(int size){
if(size<=0){
System.err.println("The arry size must be positive.Creating an array of size 100.");
maxSize=100;
}
else
maxSize=size;
length=0;
list=new DataElement[maxSize];
}
public ArrayListClass(ArrayListClass otherList){
maxSize=otherList.maxSize;
length=otherList.length;
list=new DataElement[maxSize];
for(int i=0;i list=otherList.list.getCopy();
}
}
//methods
public boolean isEmpty(){
return (length==0);
}
public boolean isFull(){
return (length==maxSize);
}
public int listSize(){
return length;
}
public int maxListSize(){
return maxSize;
}
public void print(){
for(int i=0;i System.out.print(list+" ");
}
System.out.println();
}
public boolean isItemAtEqual(int location,DataElement item){
return(list[location].equals(item));
}
public void insrtAt(int location,DataElement insertItem){
if(location<0||location>+maxSize){
System.out.println("The position of the item to be inserted is out of range!!");
}
else
if(length>=maxSize)
System.err.println("Can’t insert in a full list!!");
else{
for(int i=length;i>location;i--){
list=list[i-1];
}
list[location]=insertItem.getCopy();
length++;
}
}
public void insertEnd(DataElement insertItem){
if(length>=maxSize){
System.err.println("Can’t insert in a full list!!");
}
else{
list[length]=insertItem.getCopy();
length++;
}
}
public void removeAt(int location){
if(location<0||location>=length){
System.err.println("The location you want to remove is out of range!!");
}
else{
for(int i=location;i list=list[i+1];
}
list[length]=null;
length--;
}
}
public DataElement retrieveAt(int location){
if(location<0||location>=length){
System.err.println("The location of item to be retrieved is out of range!!");
return null;
}
else{
return list[location].getCopy();
}
}
public void replacAt(int location,DataElement repItem){
if(location<0||location>=length)
System.out.println("The position of item to be replaced is out of range!!");
else
list[location]=repItem.getCopy();
}
public void clearList(){
for(int i=0;i list=null;
}
length=0;
System.gc();
}
public void copyList(ArrayListClass otherList){
if(this!=otherList){
for(int i=0;i list=null;
System.gc();
maxSize=otherList.maxSize;
length=otherList.length;
list=new DataElement[maxSize];
for(int j=0;j list[j]=otherList.list[j].getCopy();
}
}
public abstract int seqSearch(DataElement seqItem);
public abstract void insert(DataElement insertItem);
public abstract void remove(DataElement removeItem);
}
看到代码的最后你回发现这个类其实是一个抽象类,为什么要这样定义呢?之所以这样我们是为了针对不同是类型:顺序表和非顺序表.不难想象他们的一些方法是存在差异的,先看一下非顺序表:
public class UnorderedArrayList extends ArrayListClass{
/**
*
*/
public UnorderedArrayList() {
super();
// TODO Auto-generated constructor stub
}
/* (non-Javadoc)
* @see ArrayListClass#seqSearch(DataElement)
*/
public int seqSearch(DataElement seqItem) {
// TODO Auto-generated method stub
int loc;
boolean found=false;
for(loc=0;loc if(list[loc].equals(seqItem))
{
found=true;
break;
}
if(found)
return loc;
else
return -1;
}
/* (non-Javadoc)
* @see ArrayListClass#insert(DataElement)
*/
public void insert(DataElement insertItem) {
// TODO Auto-generated method stub
int loc;
if(length==0)
list[length++]=insertItem.getCopy();
else
if(length==maxSize)
System.err.println("Can’t insert in a full list!!");
else{
loc=seqSearch(insertItem);
if(loc==-1)
list[length++]=insertItem.getCopy();
else
System.err.println("The item to be inserted is allready in the list!!");
}
}
/* (non-Javadoc)
* @see ArrayListClass#remove(DataElement)
*/
public void remove(DataElement removeItem) {
// TODO Auto-generated method stub
int loc;
if(length==0)
System.err.println("Can’t delete from a empty list!!");
else{
loc=seqSearch(removeItem);
if(loc!=-1)
removeAt(loc);
else
System.err.println("The item to be deleted is not in the list!!");
}
}
}
就是这么简单!!相信顺序表也可以轻松高顶了.
推荐阅读
-
本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ...
[详细]
蜡笔小新 2023-12-14 18:18:21
-
本文介绍了Python高级网络编程及TCP/IP协议簇的OSI七层模型。首先简单介绍了七层模型的各层及其封装解封装过程。然后讨论了程序开发中涉及到的网络通信内容,主要包括TCP协议、UDP协议和IPV4协议。最后还介绍了socket编程、聊天socket实现、远程执行命令、上传文件、socketserver及其源码分析等相关内容。 ...
[详细]
蜡笔小新 2023-12-14 18:16:27
-
-
对当年关羽败走麦城,恰好路过马超的封地,为啥马超不救他?很感兴趣的小伙伴们,趣历史小编带来详细的文章供大家参考。说到英雄好汉,便要提到一本名著了,没错,那就是《三国演义》。书中虽 ...
[详细]
蜡笔小新 2023-12-14 16:29:09
-
本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ...
[详细]
蜡笔小新 2023-12-14 15:56:00
-
本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ...
[详细]
蜡笔小新 2023-12-14 15:43:50
-
本文介绍了PHP设置MySQL字符集的方法,详细介绍了使用mysqli_set_charset函数来规定与数据库服务器进行数据传送时要使用的字符集。通过示例代码演示了如何设置默认客户端字符集。 ...
[详细]
蜡笔小新 2023-12-14 15:30:33
-
本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ...
[详细]
蜡笔小新 2023-12-14 15:25:15
-
本文介绍了橱窗设计的表现手法,包括直接展示、寓意与联想、夸张与幽默等。通过对商品的折、拉、叠、挂、堆等陈列技巧,橱窗设计能够充分展现商品的形态、质地、色彩、样式等特性。同时,寓意与联想可以通过象形形式或抽象几何道具来唤起消费者的联想与共鸣,创造出强烈的时代气息和视觉空间。合理的夸张和贴切的幽默能够明显夸大商品的美的因素,给人以新颖奇特的心理感受,引起人们的笑声和思考。通过这些表现手法,橱窗设计能够有效地传达商品的个性内涵,吸引消费者的注意力。 ...
[详细]
蜡笔小新 2023-12-14 15:14:03
-
本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ...
[详细]
蜡笔小新 2023-12-14 15:08:18
-
本文介绍了faceu激萌变老特效的使用方法,包括打开faceu激萌app、点击贴纸、选择热门贴纸中的变老特效,然后对准人脸进行拍摄,即可给照片添加变老特效。操作简单,适合新用户使用。 ...
[详细]
蜡笔小新 2023-12-14 15:01:56
-
本文介绍了Android中高级面试的必知必会内容,并总结了相关经验。文章指出,如今的Android市场对开发人员的要求更高,需要更专业的人才。同时,文章还给出了针对Android岗位的职责和要求,并提供了简历突出的建议。 ...
[详细]
蜡笔小新 2023-12-14 14:53:02
-
九月十五日,大连微软技术社区举办了《.net core始于足下》活动,共有51人报名参加,实际到场人数为43人,还有一位专程从北京赶来的同学。活动得到了微软赛百味和易迪斯的赞助,场地也由易迪斯提供。活动中大家积极交流,取得了非常成功的效果。 ...
[详细]
蜡笔小新 2023-12-14 14:51:26
-
给定一个二叉树,要求随机选择树上的一个节点。解法:遍历树的过程中,随机选择一个节点即可。具体做法参看:从输入 ...
[详细]
蜡笔小新 2023-12-14 14:36:38
-
本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ...
[详细]
蜡笔小新 2023-12-14 14:25:33
-
本文介绍了在微店中如何修改分销产品的价格以及设置价格的方法。客户在拍下商品后,在1小时内可以进行修改价格的操作,通过进入订单管理,点击未付款子项,可以找到订单信息并进行改价操作。修改价格后,买家会收到改价后的短信通知,在微店订单中进行付款即可。 ...
[详细]
蜡笔小新 2023-12-14 14:10:39
-