目录
一、基本知识准备
1.类和对象
在面向对象的程序设计中,类是创建对象的模板,对象是类的实例,通俗一点解释,类就是设计图,对象则是根据该设计图创建的真实存在的东西。
- 如果用变量的方式来理解:通过前面的学习,我们已经知道在int i;这个语句中,int是类型,而i是int类型的一个具体的变量,类似的,对象也可以理解为是某个类的具体变量。
- 如果用集合的方式来理解:类就是一个集合,而对象则是该集合中的具体元素。
1.1定义类
public class 类名 {
成员变量 //代表属性,一般是名词
成员方法 //代表行为,一般是动词
...
}
类可以定义对象的属性、行为等,例如我们有一个名为Phone的类,它定义其对象有品牌、价格的属性,以及可以打电话、玩游戏的行为,代码如下:
public class Phone {
//属性(成员变量)
String brand;
double price;
//行为(成员方法)
public void call() {
System.out.println("用手机打电话");
} // of call
public void playGame() {
System.out.println("用手机打游戏");;
} // of playGame
} // of class Phone
1.2创建对象
在java中,通常是利用new关键字来创建对象,基本格式如下:
类名 对象名 = new 类名();
继续以上面的Phone类为例,创建一个对象p,代码如下:
Phone p = new Phone();
1.3使用对象
一旦对象创建完毕,我们就可以通过点操作符(.)来访问对象的属性和行为,格式如下:
- 访问对象的属性
对象名.成员变量
- 访问对象的行为
对象名.方法名()
以上述创建的对象p为例,访问它的属性和行为,代码如下:
//访问属性
p.brand = "小米";
p.price = 1999.9;
//访问行为
p.call();
p.playGame();
//在java中,必须先设计类,才能获得对象
2.顺序表
首先我们先来看看线性表,线性表是由零个或多个相同数据类型元素组成的有限序列,在逻辑上,线性表呈线性结构,也就是一条连续的直线,但是在物理空间中却不一定是连续的,根据不同的存储方式,我们将线性表分为了顺序表与链表。
这里我们主要讨论顺序表。顺序表是用一组地址连续的存储单元来依次存放数据元素,这就使得在逻辑结构上相连的数据元素在物理空间中也相连,也就构成了线性表的顺序存储。
//一般而言,我们都是通过数组来模拟顺序表。
3.方法重载
在同一个类中,如果定义了多个同名的方法,这些同名的方法具有相同的功能,并且每个方法有不同的参数,那么这些同名的方法就构成了重载关系。简化一下就是,同一个类中,方法名相同,但参数不同,这就叫做方法重载。
//这里的参数不同有三种形式:个数不同、类型不同、顺序不同(顺序不同可以构成重载,但是不建议)
//java虚拟机会根据参数的不同来区分这些同名的方法
//是否构成重载关系与返回值无关
4.方法重写
方法重写是指在子类中对父类中允许访问的方法的实现过程进行重新编写,要求子类中的方法名、返回值类型、参数列表都必须与父类相同,只有方法体中的实现不同,其实说白了就是“外壳一样,内核不同”。
//在重写父类中的方法时,可以在方法前面加上@Override注释来说明
二、代码实现
1.定义顺序表的属性
public class SequentialList {
/**
* The maximal length of the list. It is a constant.
*/
public static final int MAX_LENGTH = 10;
/**
* The actual length not exceeding MAX_LENGTH. Attention:length is not only
* the member variable of Sequential list, but also the member variable of
* Array. In fact, a name can be the member variable of different classes.
*/
int length;
/**
* The data stored in an array.
*/
int[] data;
这里我们使用了final关键字,目的是为了保证顺序表的最大长度在后续操作中不会被更改。
2.定义顺序表的方法
/**
*********************
*Construct an empty sequential list.
*********************
*/
public SequentialList() {
length = 0;
data = new int[MAX_LENGTH];
} // of the first constructor
/**
*********************
*Construct a sequential list using an array.
*
* @param paraArray
* The given array. Its length should not exceed MAX_LENGTH.
* For simplicity now we do not check it.
*********************
*/
public SequentialList(int[] paraArray) {
data = new int[MAX_LENGTH];
length = paraArray.length;
// Copy data.
for (int i = 0; i < paraArray.length; i++) {
data[i] = paraArray[i];
} // of for i
} // of the second constructor
/**
*********************
*Overrides the method claimed in Object, the superclass of any class.
*********************
*/
public String toString() {
String resultString = "";
if (length == 0) {
return "empty";
} // of if
for (int i = 0; i < length - 1; i++) {
resultString += data[i] + ",";
} // of for i
resultString += data[length - 1];
return resultString;
} // of toString
/**
*********************
*Reset to empty.
*********************
*/
public void reset() {
length = 0;
} // of reset
可以看到在上述代码中,我们使用了方法重载,public SequentialList()与public SequentialList构成了重载关系,前者构造的是无参函数,便于我们对顺序表进行初始化,后者构造了参数为数组的函数,方便我们进行数据复制;同时,我们也进行了toString()方法的重写。
3.完整的程序代码
package datastructure.list;
/**
*Sequential list.
*
*@auther Xin Lin 3101540094@qq.com.
*/
public class SequentialList {
/**
* The maximal length of the list. It is a constant.
*/
public static final int MAX_LENGTH = 10;
/**
* The actual length not exceeding MAX_LENGTH. Attention:length is not only
* the member variable of Sequential list, but also the member variable of
* Array. In fact, a name can be the member variable of different classes.
*/
int length;
/**
* The data stored in an array.
*/
int[] data;
/**
*********************
*Construct an empty sequential list.
*********************
*/
public SequentialList() {
length = 0;
data = new int[MAX_LENGTH];
} // of the first constructor
/**
*********************
*Construct a sequential list using an array.
*
* @param paraArray
* The given array. Its length should not exceed MAX_LENGTH.
* For simplicity now we do not check it.
*********************
*/
public SequentialList(int[] paraArray) {
data = new int[MAX_LENGTH];
length = paraArray.length;
// Copy data.
for (int i = 0; i < paraArray.length; i++) {
data[i] = paraArray[i];
} // of for i
} // of the second constructor
/**
*********************
*Overrides the method claimed in Object, the superclass of any class.
*********************
*/
public String toString() {
String resultString = "";
if (length == 0) {
return "empty";
} // of if
for (int i = 0; i < length - 1; i++) {
resultString += data[i] + ",";
} // of for i
resultString += data[length - 1];
return resultString;
} // of toString
/**
*********************
*Reset to empty.
*********************
*/
public void reset() {
length = 0;
} // of reset
/**
*********************
*The entrance of the program.
*
* @param args Not used now.
*********************
*/
public static void main(String[] args) {
int[] tempArray = {1,4,6,9};
SequentialList tempFirstList = new SequentialList(tempArray);
System.out.println("Initialized, the list is: " + tempFirstList.toString());
System.out.println("Again, the list is: " + tempFirstList);
tempFirstList.reset();
System.out.println("After reset, the list is: " + tempFirstList);
} // of main
} // of class SequentialList
运行结果:
总结
可能由于自己以前没有接触过数据结构,所以今天的学习并没有前段时间那么水到渠成,但是众所周知顺序表是数据结构中最基础最简单的,其重要程度不言而喻,所以必须迎难而上。明天将继续学习顺序表。