注意:这是自己java的学习过程的记录,如有问题欢迎指正。
目录
day08:矩阵相乘
day09:while语句
day10:综合任务
day11:顺序表(一)
day12:顺序表(二)
day13:链表
day14:栈
在计算机中,矩阵实际上就是二维数组,在使用之前我们需要先引入java中的array类,然后才能使用。
代码如下:
package basic;
import java.util.Arrays;
/*
* If we want to use two-dimensional array,we should import array class first.
*/
/*
* This is the eigth code.
*
* @author Xuanlin Zhu [email protected].
*/
public class MatrixMultiplication {
public static void main(String[] args){
matrixMultiplicationTest();
}//Of main
/**
*********************
* Matrix multiplication. The columns of the first matrix should be equal to the
* rows of the second one.
*
* @param paraFirstMatrix The first matrix.
* @param paraSecondMatrix The second matrix.
* @return The result matrix.
*********************
*/
public static int[][] multiplication(int[][] paraFirstMatrix,int[][] paraSecondMatrix){
int m=paraFirstMatrix.length;
int n=paraFirstMatrix[0].length;
int p=paraSecondMatrix[0].length;
if(n!=paraSecondMatrix.length){
System.out.println("The two matrices cannot be multiplied");
return null;
}//Of if
int[][] resultMatrix=new int[m][p];
for(int i=0;i
在这段代码中还涉及到一个问题:
\r\n相当于硬回车,会另起一行
而\n\r跟\r\n不同,会在中间空出一行后再另起一行,效果如下:
while语句有两种写法
第一种是:while(判断条件){
语句块;
}
或者是 while(true){
语句块;
if(判断条件){
break;
}//若满足条件则跳出循环
}
代码如下:
package basic;
/*
* This is the ninth code.
* @author Xuanlin Zhu [email protected].
*/
public class WhileStatement {
public static void main(String[] args) {
WhileStatementTest();
}//Of main
public static void WhileStatementTest() {
int tempMax=100;
int tempValue=0;
int tempSum=0;
//Approach 1
while(tempSum<=tempMax) {
tempValue++;
tempSum+=tempValue;
System.out.println("tempValue="+tempValue+",tempSum="+tempSum);
}//Of while
tempSum-=tempValue;
System.out.println("The sum not exceeding "+tempMax+" is:"+tempSum);
System.out.println("/r/n Alternative approach");
while(true) {
tempValue++;
tempSum+=tempValue;
System.out.println("tempValue="+tempValue+",tempSum="+tempSum);
if(tempMax
今天的工作是将前几天学过的基础知识综合使用,复习的同时加深对基础知识的理解。
如果学生中最高分的成绩不止一个,那么就没有成绩最好的学生,只有最高的分数。成绩最差的学生同理,必须都保证其唯一性。(这仅为个人观点,方便读者理解代码而已)在此想法之下的代码如下:
package basic;
import java.util.Arrays;
import java.util.Random;
/*
* This is the tenth code.
* @author Xuanlin Zhu [email protected].
*/
public class Task1 {
public static void main(String[] args) {
task1();
}//Of main
public static void task1() {
int m=3;
int n=10;
int lowerBound=50;
int upperBound=100;
int threshold=60;
Random tempRandom=new Random();
int[][] data=new int[n][m];
for(int i=0;itotalScores[i]) { //find out the worst student
tempWorstScore=totalScores[i];
tempWorstIndex=i;
}//Of if
}//Of for i
int Max=1;
for(int i=0;i<=9;i++) {
if(totalScores[i]==tempBestScore) {
Max--;
}//Of if
}//Of for i
//Judge the best student is unique or not
if((tempBestIndex==-1)||(Max!=0)) {
System.out.println("Cannot find the best student.But the highest score is:"+tempBestScore);
}
else {
tempBestIndex++;
System.out.println("The best student is No." + tempBestIndex + " with scores:"+Arrays.toString(data[tempBestIndex-1]));
}//Of if
int Min=1;
for(int j=0;j<=9;j++) {
if(totalScores[j]==tempWorstScore) {
Min--;
}//Of if
}//Of for j
//Judge the worst student is unique or not
if((tempWorstIndex==-1)||(Min!=0)) {
System.out.println("Cannot find the worst student.But the worst score is:"+tempWorstScore);
}
else {
tempWorstIndex++;
System.out.println("The worst student is No." + tempWorstIndex + " with scores:"+Arrays.toString(data[tempWorstIndex-1]));
}//Of if
}//Of task1
}//Of class Task1
注意:此代码中涉及到for循环,if语句,数组的初始化、输出,随机数的生成(random类)等,其中for循环和if语句出现最多。若有所遗忘请及时复习之前学过的知识!
代码如下:
package datastructure.list;
/*
* Sequential list
*
* @author Xuanlin Zhu @[email protected].
*/
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
public SequentialList(int[] paraArray) {
data=new int[MAX_LENGTH];
length=paraArray.length;
//Copy data
for(int i=0;i
今天是在昨天所学知识的基础上新增查找、插入、删除功能。
代码如下:(只给出了新增部分)
public int indexOf(int paraValue) {
int tempPosition=-1;
for(int i=0;ilength)) {
System.out.println("The paraPosition "+paraPosition+" is out of bounds");
return false;
}//Of if
for(int i=length;i>paraPosition;i--) {
data[i]=data[i-1];
}//Of for i
data[paraPosition]=paraValue;
length++;
return true;
}//Of insert
public boolean delete(int paraPosition) {
if((paraPosition<0)||(paraPosition>=length)) {
System.out.println("The paraPosition "+paraPosition+" is out of bounds");
return false;
}//Of if
for(int i=paraPosition;i
需要注意的地方是输出部分""中最后面留空格,不然就会导致结果看起来太紧凑。
delete中的for语句的循环条件,为什么是i 运行结果如下: 链表与顺序表有许多相似之处,比如初始化、插入、删除等操作,但链表在插入和删除时不会改变元素,而是改变指针。总体上来说与前两天的顺序表还是有很大联系。 代码如下: 运行结果: 栈与之前的顺序表和链表不同,只能对栈顶进行入栈(push)和出栈(pop)操作,且栈的时间复杂度为O(1)。 代码如下:day13:链表
package datastructure.list;
/*
* Linked list
*
* @author Xuanlin Zhu [email protected].
*/
public class LinkedList {
class Node{
int data;
Node next;
public Node(int paraValue) {
data=paraValue;
next=null;
}//Of the constructor
}//Of class Node
Node header;
public LinkedList() {
header=new Node(0);
}//Of the first constructor
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
public void reset() {
header.next=null;
}//Of reset
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
}//Of while
return tempPosition;
}//Of locate
public boolean insert(int paraPosition,int paraValue) {
Node tempNode=header;
Node tempNewNode;
for(int i=0;i
day14:栈
package datastructure.list;
/*
* Char Stack
*
* @author Xuanlin Zhu [email protected].
*/
public class CharStack {
public static final int MAX_DEPTH=10;
int depth;
char[] data;
public CharStack() {
depth=0;
data=new char[MAX_DEPTH];
}//Of the first constructor
public String toString() {
String resultString="";
for(int i=0;i