Day11:顺序表(一)
11.1 类里面包括了数据和操作数据的方法
11.2 final关键字修饰常量,常量的命名方式如MAX_LENGTH
11.3 用关键字new生成新的对象
11.4 不同的类中可以有名称相同的方法
11.1 代码
package datastructure;
/**
* Sequential list.
*
* @author XuQiong
*
*/
public class SequentialList {
/**
* The maximal length of the list. It is a constant.
*/
public static final int MAX_LENGTH = 10;
/**
* The actual length of sequential list not exceeding MAX_LENGTH.
* Attention: length is not only the member variable of SequentialList, 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) {
length = paraArray.length;
data = new int[MAX_LENGTH];
for (int i = 0; i < paraArray.length; i++) {
data[i] = paraArray[i];
}// Of for i
}// Of the Second constructor.
/**
* ***********************
* Override the method claimed in Object, the superclass of any class.
* ************************
*/
public String toString() {
String resultString = "";
for (int i = 0; i < data.length - 1; i++) {
resultString += data[i] + ", ";
}//Of for i
resultString += data[data.length-1];
return resultString;
}
/**
* **********************
* Reset to empty.
* **********************
*/
public void reset() {
length = 0;
}//Of empty
/**
* ************************
* 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 SquentialList
11.2 运行结果
11.3 收获
如果 int[] data = new int[4]; 默认为用0初始化。
Day: 12 顺序表(二)
12.1 查找指定元素的位置,找不到就返回-1,只要和索引位置相关,查找失败都是-1.
12.2 在给定位置增加元素。线性表是否满,或位置是否在已有范围之内,但可以是最后一个元素之后一个.
12.3 删除指定位置的元素。判断位置是否合法。
12.4 函数和面向对象方法的不同,方法所依赖的数据既包括参数列表中给出的,也依赖于对象的成员变量,方法对对象中所有成员变量都有影响。
12.1 代码
package datastructure;
/**
* Sequential list.
*
* @author XuQiong
*
*/
public class SequentialList {
/**
* The maximal length of the list. It is a constant.
*/
public static final int MAX_LENGTH = 10;
/**
* The actual length of sequential list not exceeding MAX_LENGTH.
* Attention: length is not only the member variable of SequentialList, 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) {
length = paraArray.length;
data = new int[MAX_LENGTH];
for (int i = 0; i < paraArray.length; i++) {
data[i] = paraArray[i];
}// Of for i
}// Of the Second constructor.
/**
* ***********************
* Override the method claimed in Object, the superclass of any class.
* ************************
*/
public String toString() {
String resultString = "";
if (length == 0)
return null;
for (int i = 0; i < length - 1; i++) {
resultString += data[i] + ", ";
}//Of for i
resultString += data[length-1];
return resultString;
}
/**
* **********************
* Reset to empty.
* **********************
*/
public void reset() {
length = 0;
}//Of empty
/**
* ************************
* 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 tempPostion = -1;
for(int i = 0; i < data.length; i++) {
if(paraValue == data[i]) {
tempPostion = i;
return tempPostion;
}// Of if
}// Of for i
return tempPostion;
}// 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 paraPositon, int paraValue) {
if(paraPositon < 1 || paraPositon > data.length + 1 || paraPositon > MAX_LENGTH ) {
System.out.println("The given position is invalid! ");
return false;
}// Of if
//From tail to head. The last one is moved to a new position.
for (int i = length; i >= paraPositon; i--) {
data[i] = data[i-1];
}// Of for i
data[paraPositon-1] = 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 < 1 || paraPosition > length) {
System.out.println("The given position is invalid! ");
return false;
}// Of if
//From head to tail. The head position is the paraPosition.
for(int i = paraPosition; i <= length; i++) {
data[i-1] = data[i];
}// Of for i
length--;
return true;
}
/**
* ************************
* 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 temPosition = tempFirstList.indexOf(tempValue);
System.out.println("The position of " + tempValue + " is " + temPosition);
tempValue = 5;
temPosition = tempFirstList.indexOf(tempValue);
System.out.println("The position of " + tempValue + " is " + temPosition);
tempValue = 5;
temPosition = 2;
tempFirstList.insert(temPosition,tempValue);
System.out.println("After inserting " + tempValue + " to postion " + temPosition + ", the list is : "
+ tempFirstList);
tempValue = 10;
temPosition = 8;
tempFirstList.insert(temPosition,tempValue);
System.out.println("After inserting " + tempValue + " to postion " + temPosition + ", the list is : "
+ tempFirstList);
temPosition = 3;
tempFirstList.delete(temPosition);
System.out.println("After deleting data at postion " + temPosition + ", the list is : "
+ tempFirstList);
tempFirstList.reset();
System.out.println("After reset, the list is: " + tempFirstList);
}// Of main
}// Of class SquentialList
12.2 运行结果
12.3 收获
在toString 方法里面,没有考虑到顺序表为空的情况。
在判断位置是否合法的时候,应对各种可能的错误的情况输出提示信息,增强程序的交互性!实在太饿了,赶去吃饭,就先不改了。
Day13: 链表
13.1 基本操作: 初始化、插入、删除
13.2 为节点建一个类
13.3 引用与指针的异同
13.1 代码
package datastructure;
/**
* Linked list.
*
* @author XuQiong
*
*/
public class LinkList {
/**
* An inner class
*/
class Node {
/**
* The data.
*/
int data;
/**
* The reference to the next node.
*/
Node next;
/**
* The constructor
*
* @param paraValue
* The data.
*/
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 LinkList() {
header = new Node(0);
}//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 it appears in multiple 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;
}// Of if
tempNode = tempNode.next;
tempCurrentPosition++;
}
return tempPosition;
}// Of locate
/**
* ****************************
* 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) {
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
/**
* ***********************************
* Delete a value at a position.
*
* @param paraPosition
* The given position.
* @return Success or not.
* **********************************
*/
public boolean delete(int paraPosition) {
if (header.next == null) {
System.out.println("Cannot delete any 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. ");
}// 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[]) {
LinkList tempFirstList = new LinkList();
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());
for (int i = 0; i < 5; i++) {
tempFirstList.delete(0);
System.out.println("Loop deleted, the list is :" + tempFirstList.toString());
}// Of for i
}// Of main
}// Of class LinkList
13.2 运行结果
Day 14:栈
14.1代码
package datastructure;
/**
* Char stack.
* @author XuQiong
*
*/
public class CharStack {
/**
* The depth.
*/
public static final int MAX_DEPTH = 10;
/**
* The actual depth.
*/
int depth;
/**
* The data.
*/
char[] data;
/**
* *********************
* Construct an empty char stack.
***********************
*/
public CharStack() {
depth = 0;
data = new char[MAX_DEPTH];
}// Of the first constructor
/**
* **********************
* Overrides the method claimed in Object, the superclass of any class.
* **********************
*/
public String toString() {
String resultString = "";
for (int i = 0; i < depth; i++) {
resultString += data[i];
}// Of for i
return resultString;
}// Of toString
/**
* *********************
* Push an element.
*
* @param paraChar The given char.
* @return Success or not.
* *********************
*/
public boolean push(char paraChar) {
if(depth == MAX_DEPTH) {
System.out.println("Stack full!");
return false;
}// Of if
data[depth] = paraChar;
depth++;
return true;
}
/**
* ***************
* Pop an element.
*
* @return The popped char.
* ***************
*/
public char pop() {
if(depth == 0) {
System.out.println("Empty stack!");
return '\0';
}// Of if
char resultChar = data[depth-1];
depth--;
return resultChar;
}// Of pop
/**
* ******************
* The entrance of the program.
*
* @param args Not used now.
* ******************
*/
public static void main(String args[]) {
CharStack tempStack = new CharStack();
for (char ch = 'a'; ch < 'm'; ch++) {
tempStack.push(ch);
System.out.println("The current stack is: " + tempStack);
}// Of for ch
char tempChar;
for (int i = 0; i < 12; i++) {
tempChar = tempStack.pop();
System.out.println("Poped: " + tempChar);
System.out.println("The current stack is: " + tempStack);
}// Of for i
}
}
14.2运行结果
14.3 疑问
char[] 和 String 在定义时不同,前者需要new申请空间,后者不需要。
Day 15: 栈的应用(括号匹配)
static:静态修饰符,被static修饰的变量和方法类似于全局变量和全局方法,可以在不创建对象时调用,当然也可以在创建对象之后调用。本例类中bracketMatching 方法,在调用时可以直接bracketMatching() 调用,而不需要通过 类名.bracketMatching() 这样的方式调用。常见的可以用于工具类的工具方法中等,譬如:Math类中的绝大多数方法都是静态方法,他们扮演了工具方法的作用。
public:声明当前被修饰的对象、方法、变量为公有的。这里的公有指的是可以被公有访问,举个例子:一个类就像是一台电脑,公有的部分就是除去电脑本身之外用户可见的部分,譬如:你知道点击哪里可以登录QQ,摁哪里可以开关机,等等,你可以使用这个类所有的可见的东西都是被声明为public的,公有可见且公有可被访问的。
15.1代码
/**
* ************************
* Is the bracket matching?
*
* @param paraString The given expression.
* @return Match or not.
* ************************
*/
public static boolean bracketMatching(String paraString) {
//Step 1. Initialize the stack through pushing a '#' at the bottom.
char tempChar,tempPopedChar ;
CharStack tempStack = new CharStack();
tempStack.push('#');
//Step 2. Process the string. For a string, length() is a method.
//instead of a member variable.
for(int i = 0; i < paraString.length(); i++)
{
tempChar = paraString.charAt(i);
switch (tempChar) {
case '(':
case '[':
case '{':
tempStack.push(tempChar);
break;
case ')':
tempPopedChar = tempStack.pop();
if (tempPopedChar != '(' ) {
return false;
}// Of if
break;
case ']':
tempPopedChar = tempStack.pop();
if (tempPopedChar != '[' ) {
return false;
}// Of if
break;
case '}':
tempPopedChar = tempStack.pop();
if (tempPopedChar != '{' ) {
return false;
}// Of if
break;
default:
//Do nothing.
}// Of switch
}// Of for i
tempPopedChar = tempStack.pop();
if (tempPopedChar != '#') {
return false;
}// Of if
return true;
}
/**
* ******************
* The entrance of the program.
*
* @param args Not used now.
* ******************
*/
public static void main(String args[]) {
CharStack tempStack = new CharStack();
for (char ch = 'a'; ch < 'm'; ch++) {
tempStack.push(ch);
System.out.println("The current stack is: " + tempStack);
}// Of for ch
char tempChar;
for (int i = 0; i < 12; i++) {
tempChar = tempStack.pop();
System.out.println("Poped: " + tempChar);
System.out.println("The current stack is: " + tempStack);
}// Of for i
boolean tempMatch;
String tempExpression = "[2 + ( 1 - 3 )] * 4";
tempMatch = bracketMatching(tempExpression);
System.out.println("Is the expression " + tempExpression + " bracket matching? "
+ tempMatch);
tempExpression = "( ( ) ";
tempMatch = bracketMatching(tempExpression);
System.out.println("Is the expression " + tempExpression + " bracket matching? "
+ tempMatch);
tempExpression = "()()(())";
tempMatch = bracketMatching(tempExpression);
System.out.println("Is the expression " + tempExpression + " bracket matching? "
+ tempMatch);
tempExpression = "({}[])";
tempMatch = bracketMatching(tempExpression);
System.out.println("Is the expression " + tempExpression + " bracket matching? "
+ tempMatch);
tempExpression = "())";
tempMatch = bracketMatching(tempExpression);
System.out.println("Is the expression " + tempExpression + " bracket matching? "
+ tempMatch);
}// Of main
15.2 运行结果
Day 16: 递归
关键:写出特殊的初始条件值,再写出递推式子。
16.1 代码
package datastructure;
/**
* Recursion. A method can (directly or indirectly) invoke itself. The system automatically
* creates a stack for it.
*
* @author XuQiong
*
*/
public class Recursion {
/**
* ********************
* Sum to N. No loop, however a stack is used.
*
* @param paraN The given value.
* @return The sum.
* ********************
*/
public static int sumToN(int paraN) {
if (paraN <= 0) {
return 0;
}// Of if
return sumToN(paraN-1) + paraN;
}// Of sumToN
/**
* ********************
* Fibonacci sequence.
*
* @param paraN
* The given value.
* @return The sum.
* ********************
*/
public static int fibonacci(int paraN) {
if (paraN <= 0) {
return 0;
} else if (paraN == 1) {
return 1;
} else {
return fibonacci(paraN - 2) + fibonacci(paraN - 1);
}// Of if
}// Of fibonacci
/**
* ******************
* The entrance of the program.
*
* @param args Not used now.
* ******************
*/
public static void main(String args[]) {
int tempValue = 5;
System.out.println("0 sum to " + tempValue + " = " + sumToN(tempValue));
tempValue = -1;
System.out.println("0 sum to " + tempValue + " = " + sumToN(tempValue));
for(int i = 0; i < 10; i++) {
System.out.println("Fibonacci " + i + ": " + fibonacci(i));
}// Of for i
}// Of main
}// Of class Recursion
16.2 运行结果
Day 17: 链队列
17.1 代码
package datastructure;
/**
* Linked queue.
* @author XuQiong
*
*/
public class LinkedQueue {
/**
* An inner class.
*/
class Node {
/**
* The data.
*/
int data;
/**
* The reference to the next node.
*/
Node next;
/**
****************
* The constructor.
*
* @param paraValue The data.
* **************
*/
public Node(int paraValue) {
data = paraValue;
next = null;
}// Of the constructor
}// Of class Node
/**
* The header of the queue.
*/
Node header;
/**
* The tail of the queue.
*/
Node tail;
/**
* *****************
* Construct an empty sequential list.
* *****************
*/
public LinkedQueue() {
header = new Node(-1);
tail = header;
}// Of the first constructor
/**
* ******************
* Enqueue.
*
* @param paraValue The value of the new node.
* ******************
*/
public void enqueue(int paraValue) {
Node tempNode = new Node(paraValue);
tail.next = tempNode;
tail = tempNode;
}// Of enqueue
/**
* ********************
* Dequeue.
*
* @return The value at the header.
* ********************
*/
public int dequeue() {
if(header == tail) {
System.out.println("The linked queue is empty!");
return -1;
}// Of if
int tempValue = header.next.data;
header.next = header.next.next;
//The queue becomes empty.
if (header.next == null) {
tail = header;
}// Of if
return tempValue;
}// Of dequeue
/**
* *********************
* Overrides the method claimed in Object, the superclass of any class.
* *********************
*/
public String toString() {
String resultString = "";
Node tempNode = header.next;
while(tempNode != null) {
resultString += tempNode.data;
tempNode = tempNode.next;
}// Of while
return resultString;
}// Of toString
/**
***********
*The entrance of the program.
*
*@param args Not used now.
************
*/
public static void main(String args[]) {
LinkedQueue tempQueue = new LinkedQueue();
for (int i = 0; i < 5; i++) {
tempQueue.enqueue(i + 1);
}// Of for i
System.out.println("Enqueue, the queue is: " + tempQueue.toString());
int tempValue = tempQueue.dequeue();
System.out.println("Dequeue " + tempValue + ", the queue is: " + tempQueue.toString());
for (int i = 0; i < 5; i++) {
tempValue = tempQueue.dequeue();
System.out.println("Looped delete " + tempValue + ",the new queue is: " + tempQueue.toString());
}// Of for i
for (int i = 0; i < 3; i++) {
tempQueue.enqueue(i + 10);
}// Of for i
System.out.println("Enqueue, the queue is: " + tempQueue.toString());
}// Of main
}// Of class LinkedQueue
17.2 运行结果
Day 18: 循环队列
这一部分主要涉及的是操作系统中的内存管理,有一个没想到的点是,head 和 tail 在出入队列的过程中一直可以增加,只有使用下标访问的时候才需要取余。
18.1 代码
package datastructure;
/**
* Circle int queue.
*
* @author XuQiong
*
*/
public class CircleIntQueue {
/**
* The total space. One space can never be used.
*/
public static final int TOTAL_SPACE = 10;
/**
* The data.
*/
int[] data;
/**
* The index for calculating the head. The actual head is head % TOTAl_SPACE.
*/
int head;
/**
* The index for calculating the tail.
*/
int tail;
/**
* *************************
* The constructor
* *************************
*/
public CircleIntQueue() {
data = new int[TOTAL_SPACE];
head = 0;
tail = 0;
}// Of the first constructor
/**
* ***************************
* Enqueue.
*
* @param paraValue The value of new code.
* *************************
*/
public void enqueue(int paraValue) {
if((tail + 1) % TOTAL_SPACE == head) {
System.out.println("Quene full.");
return;
}// Of if
data[tail % TOTAL_SPACE] = paraValue;
tail++;
}// Of enqueue
/**
* *********************************
* Dequeue.
*
* @return The value of the head.
* *********************************
*/
public int dequeue() {
int resultValue;
if(head == tail) {
System.out.println("No element in the queue");
return -1;
}// Of if
resultValue = data[head % TOTAL_SPACE];
head++;
return resultValue;
}// Of dequeue
/**
* ***********************************
* Overrides the method claimed in Object, the superclass of any class.
* ***********************************
*/
public String toString() {
String resultString = "";
if(head == tail) {
return "empty";
}
for(int i = head; i < tail; i++) {
resultString += data[i % TOTAL_SPACE] + ", ";
}// Of for i
return resultString;
}// Of toString
/**
* *****************************
* The entrance of the program.
*
* @param args Not used now.
* *****************************
*/
public static void main(String args[]) {
CircleIntQueue tempQueue = new CircleIntQueue();
for (int i = 0; i < 5; i++) {
tempQueue.enqueue(i + 10);
}
System.out.println("Enqueue, the queue is:" + tempQueue.toString());
for (int i = 0; i < 6; i++) {
tempQueue.enqueue(i + 10);
}
System.out.println("Enqueue, the queue is:" + tempQueue.toString());
for (int i = 0; i < 3; i++) {
tempQueue.dequeue();
}
System.out.println("Dequeue, the queue is:" + tempQueue.toString());
for (int i = 0; i < 8; i++) {
tempQueue.dequeue();
}
System.out.println("Dequeue, the queue is:" + tempQueue.toString());
}// Of main
}// Of CircleIntQueue
18.2 运行结果
Day 19:字符串匹配
19.1 代码
package datastructure;
/**
* My string. String is a class provided by the language, so I use another name.
* It is essentially a sequential list with char type elements.
*
* @author XuQiong
*
*/
public class MyString {
/**
* The maximal length.
*/
public static final int MAX_LENGTH = 10;
/**
* The actual length.
*/
int length;
/**
* The data.
*/
char[] data;
/**
* Construct an empty char array.
*/
public MyString() {
length = 0;
data = new char[MAX_LENGTH];
}// Of the first constructor
/**
* ******************************
* Construct using a system defined string.
*
* @param paraString
* The given string. Its length should not exceed MAX_LENGTH - 1.
* ******************************
*/
public MyString(String paraString) {
data = new char[MAX_LENGTH];
length = paraString.length();
for(int i = 0; i < length; i++) {
data[i] = paraString.charAt(i);
}// Of for i
}// Of the Second constructor
/**
* **************************************
* Overrides the method claimed in Object, the superclass of any class.
* **************************************
*/
public String toString() {
String resultString = "";
for (int i = 0; i < length; i++) {
resultString += data[i];
}// Of for i
return resultString;
}// Of toString
/**
* ****************************************
* Locate the position of a substring.
*
* @param paraString
* The given substring.
* @return The first position.
* -1 for no matching.
* **************************************
*/
public int locate(MyString paraMystring) {
boolean tempMatch = false;
for (int i = 0; i < length - paraMystring.length + 1; i++) {
tempMatch = true;
for (int j = 0; j < paraMystring.length; j++) {
if(paraMystring.data[j] != data[i+j]) {
tempMatch = false;
break;
}// Of if
}// Of for j
if (tempMatch) {
return i;
}// Of if
}// Of for i
return -1;
}// Of locate
/**
* *************************************
* Get a substring
*
* @param paraString
* The given substring.
* @param paraStarPosition
* The start position in the original string.
* @param paraLength
* The length of the new string.
* @return The first position. -1 for no matching.
* *************************************
*/
public MyString substring(int paraStartPosition, int paraLength) {
if (paraStartPosition + paraLength > length) {
System.out.println("The bound is exceed.");
return null;
}// Of if
MyString resultMyString = new MyString();
resultMyString.length = paraLength;
for (int i = 0; i < paraLength; i++) {
resultMyString.data[i] = data[paraStartPosition + i];
}// Of for i
return resultMyString;
}// Of substring
/**
* ****************************************
* The entrance of the program.
*
* @param args
* Not used now.
* ****************************************
*/
public static void main(String args[]) {
MyString tempFirstString = new MyString("I like it");
MyString tempSecondString = new MyString("it");
int tempPosition = tempFirstString.locate(tempSecondString);
System.out.println("The position of \"" + tempSecondString +"\" in \"" + tempFirstString
+ "\" is: " + tempPosition);
MyString tempThirdString = tempFirstString.substring(1, 2);
System.out.println("The substring is: \"" + tempThirdString + "\"");
tempThirdString = tempFirstString.substring(5, 5);
System.out.println("The substring is: \"" + tempThirdString + "\"");
tempThirdString = tempFirstString.substring(5, 6);
System.out.println("The substring is: \"" + tempThirdString + "\"");
}// Of main
}// Of class MyString
19.2 运行结果
Day 20:小结
1、面向对象与面向过程的优势:
以售卖商品为例,很多设计师品牌都有自己的消费人群定位,比如针对的是特定年龄段、特定收入水平、特定风格品味、特定身材的人群,所以该品牌需要研究目标消费群体,也就是“对象”的属性,比如:年龄、收入情况、风格偏好、身材等,通过这些属性,去确定衣服的定价、预计销售量等(也就是方法)、制衣流程等。 因为属性界定清楚明了,所以方法的计算结果也是比较准确的。
而一般大众品牌,目标消费人群较广,也就是对象属性定义不清晰,更关注的是如何做出衣服(也就是方法或者过程),弱化了对象的概念,感觉上比较简单,但由于不同类型衣服在制作工艺上是有区别的,所以方法重用性不高。
2、顺序表和链表的异同,优缺点。
顺序表需要在开始时确定好大小,不支持动态增长,方便查找,插入删除时间复杂度较高。
链表不需要提前申请空间,支持动态增长,对插入删除友好,查找时时间复杂度较高。
3、调试程序常见的问题以及解决方案。
如果程序运行不成功,把提示的错误,百度解决即可。
如果运行结果与预期不一致,打断点查看变量的值是怎样变化的,查看是否有逻辑上的错误。
4、链队列与循环队列的优缺点。
循环队列还是基于顺序表实现的,比顺序表的空间利用率高(因为可循环使用空间),会在开始前,确定队列的最大长度。
链队列,用多少申请多少内存空间,基于链表的方式实现。
其他优缺点参考顺序表和链表的异同。
5、对不同的基础数据类型都需要重写一个类,这样合理么?你想怎么样?
不合理。把不同的数据类型作为一个新方法的参数,依据实际的需要确定基础的数据类型。