Java线性数据结构day1

顺序表(一)

学习来源:https://blog.csdn.net/minfanphd/article/details/116974889

顺序表是在计算机内存中以数组的形式保存的线性表,线性表的顺序存储是指用一组地址连续的存储单元依次存储线性表中的各个元素、使得线性表中在逻辑结构上相邻的数据元素存储在相邻的物理存储单元中,即通过数据元素物理存储的相邻关系来反映数据元素之间逻辑上的相邻关系,采用顺序存储结构的线性表通常称为顺序表。

对象: 数据及其上操作的总和,也可以说就是Java的实例。

面向对象:面向对象是把整个需求按照特点、功能划分,将这些存在共性的部分封装成对象,创建了对象不是为了完成某一个步骤,而是描述某个事物在解决问题的步骤中的行为。

类与对象的关系:对象抽象化为类,类实例化为对象

关键字 final 。final修饰的变量是一个常量。只能被赋值一次。final修饰的类不能被继承。final修饰的方法不能被重写。

有一个成员变量叫做 length. 程序里还有用 length 表示一个整数数组的长度. 实际上, 同一个变量名可以被不同的类所使用

toString 这个方法很特殊, 它覆盖了 Object 类的相应方法. 可以看到, 在 println 里面使用 tempFirstList 里, 由于是用另一个字符串与其相加, 系统会自动调用 tempFirstList.toString().

package datastructure;

/**
 * @time 2022/4/1
 * @author Liang Huang
 */

public class SequentialList {

	/**
	 * The maximal length in the list. It is a constant.
	 */
	private static final int MAX_LENGTH = 10;
	
	/**
	 * The actual length is 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 give array. It`s 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<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] + ", ";
		}
		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

顺序表(二)

在顺序表(一)的代码上进行一些操作
1、查找给定元素所处的位置. 找不到就返回 -1。
2、在给定位置增加元素. 如果线性表已满, 或位置不在已有位置范围之内, 就拒绝增加. 该位置可以是在最后一个元素之后一个。
3、删除定定位置的元素. 要处理给定位置不合法的情况. 该位置必须是已经有数据的。

package datastructure;

/**
 * @time 2022/4/1
 * @author Liang Huang
 */

public class SequentialList {

	/**
	 * The maximal length in the list. It is a constant.
	 */
	private static final int MAX_LENGTH = 10;
	
	/**
	 * The actual length is 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 give array. It`s 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<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] + ", ";
		}
		resultString += data[length - 1];
		
		return resultString;
	}//Of toString
	
	/**
	 * Reset to empty.
	 */
	public void reset() {
		length = 0;
	}//Of reset
	
	/**
	 ***********
	 * Find the index of value. If it appears in multiple positions, simply return
	 * the first one
	 * 
	 * @param paraValue The given value.
	 * @return The postion. -1 for not found.
	 */
	public int indexOf(int paraValue) {
		int tempPosition = -1;
		
		for(int i=0; i<length; i++) {
			if(data[i] == paraValue) {
				tempPosition = i;
				break;//找到第一个满足的值,就退出
			}//Of if
		}//Of for i
		
		return tempPosition;
	}//Of indexOf
	
	/**
	 *************
	 *Insert a value to a position. If the list is already full, do nothing.
	 * 
	 * @param paraPosition The give position.
	 * @param paraValue THe give value.
	 * @return Success or not.
	 *************
	 */
	public boolean insert(int paraPosition, int paraValue) {
		if(length == MAX_LENGTH) {
			System.out.println("List is full.");
			return false;
		}//Of if
		
		if((paraPosition < 0) || (paraPosition > length)) {
			System.out.println("The position" + paraPosition + "is out of bounds.");
			return false;
		}//Of if
		
		// From tail to head. The last one is moved to a new position. Because length < MAX_LENGTH, no exceeding occurs.
		for(int i=length; i>paraPosition; i--) {
			data[i] = data[i - 1];
		}//Of for i
		
		data[paraPosition] = paraValue;
		length++;
		
		return true;
	}//Of insert
	
	/**
	 **********
	 *Delete a value at a position.
	 *
	 * @param paraPosition The given value.
	 * @return Success or not
	 **********
	 */
	public boolean delete(int paraPosition) {
		if((paraPosition < 0) || (paraPosition >= length)) {
			System.out.println("The position " + paraPosition + "is out of bounds.");
			return false;
		}//Of if
		
		for(int i=paraPosition; i<length - 1; i++) {
			data[i] = data[i + 1];
		}//Of for i
		
		length--;
		
		return true;
	}//Of delete
	
	/**
	 * ********
	 * 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);
		
		int tempValue = 4;
		int tempPosition = tempFirstList.indexOf(tempValue);
		System.out.println("The position of " + tempValue + " is " + tempPosition);
		
		tempValue = 5;
		tempPosition = tempFirstList.indexOf(tempValue);
		System.out.println("The position of " + tempValue + " is " + tempPosition);
		
		tempPosition = 2;
		tempValue = 5;
		tempFirstList.insert(tempPosition, tempValue);
		System.out.println(
	   			"After inserting " + tempValue + " to position " 
	   					+ tempPosition + ", the list is: " + tempFirstList);
		
		tempPosition = 8;
	   	tempValue = 10;
	   	tempFirstList.insert(tempPosition, tempValue);
	   	System.out.println("After inserting " + tempValue + " to position " 
	   					+ tempPosition + ", the list is: " + tempFirstList);

	   	tempPosition = 3;
	   	tempFirstList.delete(tempPosition);
	   	System.out.println("After deleting data at position " + tempPosition
	   			+ ", the list is: " + tempFirstList);
	
	   	for(int i=0; i<8; i++) {
	   		tempFirstList.insert(i, i);
	   		System.out.println("After inserting " + i + " to position "
	   				+ i + ", the list is: " + tempFirstList);
	   	}//Of for i
		
		
		tempFirstList.reset();
		System.out.println("After reset, the list is: " + tempFirstList);
	}//Of main

}//Of class SequentialList

注意事项:在上面的2段代码中,data.length是不等于length的,因为data在初始化时,长度就设置为了MAX_LENGTH。而length是在插入数据时才曾加值。所以很多for循环里面用到的length不能用data.length来代替。这是我踩过的坑,没有仔细研究前以为二者是一样的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

别偷我的猪_09

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值