JAVA学习(小白向)—顺序表1—2021.5.30

JAVA学习(小白向)—顺序表1—2021.5.30

顺序表是指用一组地址连续的存储单元依次存储各个元素,使得在逻辑结构上相邻的数据元素存储在相邻的物理存储单元中的线性表。
顺序表的基本操作有:增(add),删(remove),改(set),查(find),插(insert)等。

在《数据结构》中, 使用“抽象数据类型”来描述不同的数据结构. 在《面向对象程序设计》中, 用对象来存储数据及其上的操作. 我认为,
它们的本质都是相同的.

  1. 对象: 数据及其上操作的总和. 例如, 我是一个对象, 具有身高、体重、年龄、跑步速度等数据; 同时,我具有吃饭、睡觉、送快递等功能. 从计算机的发展来看, 第一阶段以操作 (函数) 为中心,
    一个计算导弹轨迹的函数,根据不同输入获得不同输出. 第二阶段以数据为中心, 即数据存放于数据库, 使用不同的算法来处理它.
    第三阶段认为数据及其上的操作是统一不可分的, 这就到了面向对象.
  2. 类. 前面已经使用过 int i; 这类代码, int 就是类型, i 是一个具体的整数变量. 同理, 对象就是属于某种类的变量. 也可以用集合的方式来理解: 类是集合, 对象是其中的元素; int 是指所有整数的集合, i是其中的一个元素.
  3. 包. 包并非程序设计必须的东西, 其作用仅仅是将类进行合理的组织. 但是, 在计算机界, 往往这种可有可无的东西才是最重要的. 如文档、注释、编码规范. 可有可无是针对程序的运行而言, 其核心是计算机; 而重要是针对程序的易读性、可维护性而言, 其核心是程序员.
  4. 常量用 final 修饰. 这里故意把 MAX_LENGTH 设置得比较少, 方便调拭后面的越界检查代码.
  5. 用 new 生成新的对象.
  6. 有一个成员变量叫做 length. 程序里还有用 length 表示一个整数数组的长度. 实际上, 同一个变量名可以被不同的类所使用, 例如: 人有体重, 西瓜也有重量. 由于限定了不同的类、不同的对象,
    它们之间就不会有冲突.张三的体重、李四的体重,有关联才奇怪了. 这段描述写出来怪怪的, 明明现实生活中就是如此.
    但这也正是体现了面向对象的特点:比面向过程的程序设计更贴合我们的人类认知, 也就更远离机器底层.
  7. toString 这个方法很特殊, 它覆盖了 Object 类的相应方法. 可以看到, 在 println 里面使用 tempFirstList 里, 由于是用另一个字符串与其相加, 系统会自动调用 tempFirstList.toString().

———————————————— learn form @minfanphd 原文

代码:

package a11;

/**
 * 
 * @author hengyuzuo
 *
 */
public class datastructure {

	/**
	 * ******************* 
	 * The maximal length of the list. It's a constant.
	 * 
	 * *******************
	 */
	public static final int MAX_LENGTH = 10;
	/**
	 * *******************
	 * The actual length not exceeding MAX_LENTH.Attention:lenth 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 datastructure() {
		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 datastructure(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, 5, 9};
		datastructure tempFirstList = new datastructure(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 datastructure

运行结果:

Initialized, the list is: 1,4,5,9
Again, the list is: 1,4,5,9
After reset, the list is: empty
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值