0
点赞
收藏
分享

微信扫一扫

Java-03

月白色的大狒 2022-04-05 阅读 94
java学习

学习来源:日撸 Java 三百行(11-20天,线性数据结构)_闵帆的博客-CSDN博客

11 顺序表(一)

在《数据结构》中,使用“抽象数据类型”来描述不同的数据结构。在《面向对象程序设计》中,用对象来存储数据及其上的操作。

11.1 对象: 数据及其上操作的总和。例如,我是一个对象,具有身高、体重、年龄、跑步速度等数据;同时,我具有吃饭、睡觉、送快递等功能。从计算机的发展来看,第一阶段以操作 (函数) 为中心,一个计算导弹轨迹的函数,根据不同输入获得不同输出。第二阶段以数据为中心,即数据存放于数据库,使用不同的算法来处理它。第三阶段认为数据及其上的操作是统一不可分的,这就到了面向对象。

11.2 类。前面已经使用过 int i;这类代码,int 就是类型,i 是一个具体的整数变量。同理,对象就是属于某种类的变量。也可以用集合的方式来理解: 类是集合,对象是其中的元素;int 是指所有整数的集合,i 是其中的一个元素。

11.3 包。包并非程序设计必须的东西,其作用仅仅是将类进行合理的组织。但是,在计算机界,往往这种可有可无的东西才是最重要的。如文档、注释、编码规范。可有可无是针对程序的运行而言,其核心是计算机;而重要是针对程序的易读性、可维护性而言,其核心是程序员。

11.4 常量用 final 修饰。

11.5 用 new 生成新的对象。

11.6 有一个成员变量叫做 length。程序里还有用 length 表示一个整数数组的长度。实际上,同一个变量名可以被不同的类所使用,例如: 人有体重,西瓜也有重量。由于限定了不同的类、不同的对象,它们之间就不会有冲突。张三的体重、李四的体重,有关联才奇怪了。这段描述写出来怪怪的,明明现实生活中就是如此。但这也正是体现了面向对象的特点: 比面向过程的程序设计更贴合我们的人类认知,也就更远离机器底层。

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

package 日撸Java300行_11_20;

/**
 * Sequential list.
 *@time 2022/4/4
 *@author Hui Xiao
 */

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

截图:

 

12 顺序表(二)(完整代码)

12.1 查找给定元素所处的位置。找不到就返回 -1。

12.2 在给定位置增加元素。如果线性表已满,或位置不在已有位置范围之内,就拒绝增加。该位置可以是在最后一个元素之后一个。

12.3 删除定定位置的元素。要处理给定位置不合法的情况。该位置必须是已经有数据的。

12.4 函数 要求同样的输入参数获得同样的输出结果,但 方法 所依赖的数据既包括参数列表中给出的,也依赖于对象的成员变量。因此,面向对象所涉及的参数列表要短些。例如,locate 方法就有效利用了 length 和 data 这两个成员变量。

package 日撸Java300行_11_20;

/**
 * Sequential list.
 *@time 2022/4/4
 *@author Hui Xiao
 */

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
	
	/**
	 **************
	 * Find the index of the given value. If it appears in multiple positions,
	 * simply return the first one.
	 * 
	 * @param paraValue The given value.
	 * @return The position. -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 given position.
	 * @param paraValue    The given value.
	 * @return Success or not.
	 */
	public boolean insert(int paraPosition, int paraValue) {
		if (length == MAX_LENGTH) {
			System.out.println("List 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 position.
	 * @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
		
		//From head to tail.
		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 );
		
		tempPosition = 2;
		tempValue =5;
		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

截图:

13 链表

13.1 支持与顺序表相同的操作:初始化、插入、删除等。

13.2 为节点建一个类。

13.3 引用与指针的异同。前者只能使用;后者可以支持 p ++ 危险操作。

13.4 引用数据类型的赋值,都不会产生新的对象空间。

13.5 链表与线性表在插入、删除时的不同:前者不移动元素,只改变引用 (指针)。

package 日撸Java300行_11_20;

/**
 * Linked list.
 *@time 2022/4/5
 *@author Hui Xiao
 */

public class LinkedList {
	/**
	 * An inner class.
	 */
	class Node {
		/**
		 * The data.
		 */
		int data;
		
		/**
		 * The reference to the next node.
		 */
		Node next;
		
		/**
		 *************
		 * The constructor
		 * 
		 * @param paraValue Thedata.
		 *************
		 */
		public Node(int paraValue) {
			data = paraValue;
			next = null;
		} // Of the constructor
	} // Of class Node
	
	/**
	 * The header node. The data is never used.
	 */
	Node header;
	
	/**
	 *************
	 * Construct an empty linked list.
	 *************
	 */
	public LinkedList() {
		header = new Node(0);
		//header.next = null; //Redundant
	} // Of the first constructor
	
	/**
	 *************
	 * Overrides the method claimed in Object, the superclass of any class.
	 *************
	 */
	public String toString() {
		String resultString = "";
		
		if (header.next == null) {
			return "empty";
		} // Of if
		
		Node tempNode = header.next;
		while (tempNode != null) {
			resultString += tempNode.data + ", ";
			tempNode = tempNode.next;
		} // Of while
		
		return resultString;
	} // Of toString
	
	/**
	 *************
	 * Reset to empty. Free the space through garbage collection.
	 *************
	 */
	public void reset() {
		header.next = null;
	} // Of reset
	
	/**
	 *************
	 * Locate the given value. If is appears in mutiple positions,simply
	 * return the first one.
	 * 
	 * @param paraValue
	 * 			The given value.
	 * @return The position. -1 for not found.
	 *************
	 */
	public int locate(int paraValue) {
		int tempPosition = -1;
		
		Node tempNode = header.next;
		int tempCurrentPosition = 0;
		while (tempNode != null) {
			if (tempNode.data == paraValue) {
				tempPosition = tempCurrentPosition;
				break;
			} // Of if
			
			tempNode = tempNode.next;
			tempCurrentPosition++;
		} // Of while
		
		return tempPosition;
	} // Of locate
	
	/**
	 *************
	 * Insert a value to a position.
	 * 
	 * @param paraPosition
	 * 			The given position.
	 * @param paraValue
	 * 			The given value.
	 * @return Success or not.
	 *************
	 */
	public boolean insert(int paraPosition, int paraValue) {
		Node tempNode = header;
		Node tempNewNode;
		
		for (int i = 0; i < paraPosition; i++) {
			if (tempNode.next == null) {
				System.out.println("The position " + paraPosition + " is illegal.");
				return false;
			} // Of if
			
			tempNode = tempNode.next;
		} // Of for i
		
		// Construct a new node.
		tempNewNode = new Node(paraValue);
		
		// Now link them.
		tempNewNode.next = tempNode.next;
		tempNode.next = tempNewNode;
		
		return true;
	} // Of insert
	
	public boolean delete(int paraPosition) {
		if (header.next == null) {
			System.out.println("Cannot delete element from an empty list.");
			return false;
		} // Of if
		
		Node tempNode = header;
		
		for (int i = 0; i < paraPosition; i++) {
			if (tempNode.next.next == null) {
				System.out.println("The position " + paraPosition + "is illegal.");
				return false;
			} // Of if
			
			tempNode = tempNode.next;
		} // Of for i
		
		tempNode.next = tempNode.next.next;
		return true;
	} // Of delete
	
	/**
	 *************
	 * The entrance of the program.
	 * 
	 * @param args
	 * 			Not used now.
	 *************
	 */
	public static void main(String[] args) {
		LinkedList tempFirstList = new LinkedList();
		System.out.println("Initialized, the list is: " + tempFirstList.toString());
		
		for (int i = 0; i < 5; i++) {
			tempFirstList.insert(0, i);
		} // Of for i
		System.out.println("Inserted, the list is: " + tempFirstList.toString());
		
		tempFirstList.insert(6, 9);
		
		tempFirstList.delete(4);
		
		tempFirstList.delete(2);
		System.out.println("Deleted, the list is: " + tempFirstList.toString());
		
		tempFirstList.delete(0);
		System.out.println("Deleted, the list is: " + tempFirstList.toString());
		
		for (int i = 0; i < 5; i++) {
			tempFirstList.delete(0);
			System.out.println("Looped delete, the list is: " + tempFirstList.toString());
		} // Of for i
	} // Of main
} // Of class LinkedList

截图:

 

对于常量需要用final来修饰。

并且java有String类,可以很方便的操作字符串。可以直接用+号就可以将两个字符串连接在一起。而在c语言中只有定义字符数组或者指针,操作就相对来说比较麻烦。

Java垃圾回收机制的特点:1.垃圾回收是一个自动的系统行为,程序员不能控制垃圾回收的功能和行为。2.有一些跟垃圾回收相关的方法,比如:System.gc( ) ,调用这些方法,仅仅是在通知垃圾回收程序,至于垃圾回收程序运不运行,什么时候运行,都是无法控制的。3.程序员可以通过设置对象为 null,来标示某个对象不再被需要了, 这只是表示这个对象可以被回收了,并不是马上被回收。

举报

相关推荐

Java数组03

JAVA基础03

Java梳理-03

03_Java Web——Maven

Java语言练习答案03

java流程控制03

Java基础day03

03Java面向对象

0 条评论