注意:这是自己java的学习过程的记录,如有问题欢迎指正。
目录
day08:矩阵相乘
在计算机中,矩阵实际上就是二维数组,在使用之前我们需要先引入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 1306805091@qq.com.
*/
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<m;i++){
for(int j=0;j<p;j++){
for (int k = 0;k<n;k++){
resultMatrix[i][j]+=paraFirstMatrix[i][k]*paraSecondMatrix[k][j];
}//Of for k
}//Of for j
}//Of for i
return resultMatrix;
}//Of multiplication
/*
* Unit test for respective method.
*/
public static void matrixMultiplicationTest(){
int[][] tempFirstMatrix=new int[2][3];//Initialization of array
for(int i=0;i<tempFirstMatrix.length;i++){
for(int j=0;j<tempFirstMatrix[0].length;j++){
tempFirstMatrix[i][j]=i+j;
}//Of for j
}//Of for i
System.out.println("The first matrix is:\n\r"+Arrays.deepToString(tempFirstMatrix));
int[][] tempSecondMatrix=new int[3][2];
for(int i=0;i<tempSecondMatrix.length;i++){
for(int j=0;j<tempSecondMatrix[0].length;j++){
tempSecondMatrix[i][j]=10*i+j;
}//Of for j
}//Of for i
System.out.println("The second matrix is:\r\n"+Arrays.deepToString(tempSecondMatrix));
int[][] tempThirdMatrix=multiplication(tempFirstMatrix,tempSecondMatrix);
System.out.println("The third matrix is:\r\n"+Arrays.deepToString(tempThirdMatrix));
System.out.println("trying to multiply first matrix with itself.\r\n");
tempThirdMatrix=multiplication(tempFirstMatrix, tempSecondMatrix);
System.out.println("The result matrix is:\r\n"+Arrays.deepToString(tempThirdMatrix));
}//Of matrixMultiplicationTest
}//Of class matrixMultiplication
在这段代码中还涉及到一个问题:
\r\n相当于硬回车,会另起一行
而\n\r跟\r\n不同,会在中间空出一行后再另起一行,效果如下:
day09:while语句
while语句有两种写法
第一种是:while(判断条件){
语句块;
}
或者是 while(true){
语句块;
if(判断条件){
break;
}//若满足条件则跳出循环
}
代码如下:
package basic;
/*
* This is the ninth code.
* @author Xuanlin Zhu 1306805091@qq.com.
*/
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<tempSum) {
break;
}//Of if
}//Of while
tempSum-=tempValue;
System.out.println("The sum not exceeding "+tempMax+" is:"+tempSum);
}//Of while StatementTest
}//Of class WhileStatement
day10:综合任务
今天的工作是将前几天学过的基础知识综合使用,复习的同时加深对基础知识的理解。
如果学生中最高分的成绩不止一个,那么就没有成绩最好的学生,只有最高的分数。成绩最差的学生同理,必须都保证其唯一性。(这仅为个人观点,方便读者理解代码而已)在此想法之下的代码如下:
package basic;
import java.util.Arrays;
import java.util.Random;
/*
* This is the tenth code.
* @author Xuanlin Zhu 13036805091@qq.com.
*/
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;i<n;i++) {
for(int j=0;j<m;j++) {
data[i][j]=lowerBound+tempRandom.nextInt(upperBound-lowerBound+1);//Randomly generate a range of values
}//Of for j
}//Of for i
System.out.println("The data is:\r\n"+Arrays.deepToString(data));
int[] totalScores=new int[n];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
if(data[i][j]<threshold) {
totalScores[i]=0;
break;
}//Of if
totalScores[i]+=data[i][j];//Calculate the total score for each student and exclusion of failing students
}//Of for j
}//Of for i
System.out.println("The totalScores are:\r\n"+Arrays.toString(totalScores));
int tempBestIndex=-1;
int tempWorstIndex=-1;
int tempBestScore=0;
int tempWorstScore=m*upperBound+1;//Variable initialization
/*for(int i=0;i<9;i++) {
for(int j=0;j<9;j++) {
if(totalScores[i]==0||totalScores[j]==0) {
break;
}
else if(totalScores[i]==totalScores[j]) {
System.out.println("Cannot find the best student,All of the students have failed");
}
}
}*/
for(int i=0;i<n;i++) {
if(totalScores[i]==0) {//
continue;
}//Of if
if(tempBestScore<totalScores[i]) { //find out the best student
tempBestScore=totalScores[i];
tempBestIndex=i;
}//Of if
if(tempWorstScore>totalScores[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语句出现最多。若有所遗忘请及时复习之前学过的知识!
day11:顺序表(一)
代码如下:
package datastructure.list;
/*
* Sequential list
*
* @author Xuanlin Zhu @1306805091@qq.com.
*/
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<paraArray.length;i++) {
data[i]=paraArray[i];
}//Of for i
}//Of the second constructor
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
public void reset() {
length=0;
}//Of reset
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
day12:顺序表(二)
今天是在昨天所学知识的基础上新增查找、插入、删除功能。
代码如下:(只给出了新增部分)
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
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 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<length-1;i++) {
data[i]=data[i+1];
}//Of for i
length--;
return true;
}//Of delete
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 positon 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
需要注意的地方是输出部分""中最后面留空格,不然就会导致结果看起来太紧凑。
delete中的for语句的循环条件,为什么是i<length-1而不是i<=length-1呢?实际上是因为如果删除的是最后一个元素,那么后面的length--已经完成了这个工作,所以可以写成i<length-1,而i<=length-1也是可以的。
运行结果如下:
day13:链表
链表与顺序表有许多相似之处,比如初始化、插入、删除等操作,但链表在插入和删除时不会改变元素,而是改变指针。总体上来说与前两天的顺序表还是有很大联系。
代码如下:
package datastructure.list;
/*
* Linked list
*
* @author Xuanlin Zhu 1306805091@qq.com.
*/
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<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);
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 an element from 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
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
运行结果:
day14:栈
栈与之前的顺序表和链表不同,只能对栈顶进行入栈(push)和出栈(pop)操作,且栈的时间复杂度为O(1)。
代码如下:
package datastructure.list;
/*
* Char Stack
*
* @author Xuanlin Zhu 1306805091@qq.com.
*/
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<depth;i++) {
resultString+=data[i];
}//Of for i
return resultString;
}//Of toString
public boolean push(char paraChar) {
if(depth==MAX_DEPTH) {
System.out.println("Stack full.");
return false;
}//Of if
data[depth]=paraChar;
depth++;
return true;
}//Of push
public char pop() {
if(depth==0) {
System.out.println("Nothing to pop.");
return '\0';
}//Of if
char resultChar=data[depth-1];
depth--;
return resultChar;
}//Of pop
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
}//Of for main
}//Of class CharStack