循环结构
循环结构:根据循环条件,重复执行某段代码。
常见的循环结构:for循环、while循环、do-while循环、foreach语句
常见循环结构的语法格式:
for循环
for (①初始化部分; ②循环条件部分; ④迭代部分){
③循环体部分;
}
说明:
②循环条件部分为boolean类型表达式,当值为false时,退出循环
①初始化部分可以声明多个变量,但必须是同一个类型,用逗号分隔
④可以有多个变量更新,用逗号分隔
while循环
①初始化部分
while(②循环条件部分){
③循环体部分;
④迭代部分;
}
说明:
注意不要忘记声明④迭代部分。否则,循环将不能结束,变成死循环。
for循环和while循环可以相互转换
do-while循环
①初始化部分;
do{
③循环体部分
④迭代部分
}while(②循环条件部分);
说明:
do-while循环至少执行一次循环体。
foreach语句
for(元素类型 元素变量x:待遍历的集合或数组) {
引用了变量x的java语句
}
特殊关键字的使用
break、continue、return
break的使用:
- break语句用于终止某个语句块的执行。
- break语句出现在多层嵌套的语句块中时,可以通过标签指明要终止的是哪一层语句块。
- break只能用于switch语句和循环语句中。
continue的使用:
- continue只能使用在循环结构中
- continue语句用于跳过其所在循环语句块的一次执行,继续下一次循环
- continue语句出现在多层嵌套的循环语句体中时,可以通过标签指明要跳过的是哪一层循环。
- continue 只能用于循环语句中。
continue与break二者功能类似,但continue是终止本次循环,break是终止本层循环。
return的使用:
- return的功能是结束一个方法。当一个方法执行到一个return语句时,这个方法将被结束。
包
包声明语句用于把Java类放到特定的包中。一个Java源文件中,最多有一个package语句,且必须位于正式代码的第一行,如果没有提供package语句,就表明Java类位于默认包中,默认包没有名字。
包的作用:
- 区分名字相同的类;
- 控制访问权限;
- 组织和划分Java中的各个类。
包的命名规范:
包的名字通常全用小写,且包含以下信息:
- 类的创建者和拥有者的信息
- 类所属的软件项目的信息
- 类在具体软件项目所处的位置
包的命名规范采用了网上URL命名规范的反转形式,如:https://netstore.abc.com,Java包名的形式为:com.abc.netstore。
在本次代码中,就用到了Java基本包:java.util包,用到了该包的Arrays类,通过import语句把这个类引入。
Arrays.deepToString()方法将一个多为数组转换为字符串。
数组
数组(Array)是指一组相同类型数据并且按一定顺序排列的集合,数组中的每一个数据称为元素。元素可以是任意类型(基本类型或引用类型)。
创建数组步骤:
- 声明一个数组类型的引用变量,简称为数组变量(注意:声明时不能指定数组长度)
- 用new语句构造数组的实例。new语句为数组分配内存,并且为数组中的每个元素赋予默认值。
- 初始化,即为数组的每个元素设置合适的初始值。
注意:
- 数组本身是引用数据类型,而其元素可以是任意类型。
- 创建数组对象会在内存中开辟一整块连续的空间,而数组名中引用的是这块连续地址空间的首地址。
- 数组的长度一旦确定,就不能修改,数组有一个length属性,可以通过该属性获得数组长度。
- 可以直接通过下标(索引)的方式指定位置访问元素。
- 创建多维数组时,必须按照从低维度到高维度的顺序创建每一维数组。如:int[][][] a=new int[2][][3];//编译出错,要先创建第二维数组,再创建第三维数组
元素的默认值:
字符类型
char型数据用来表示通常意义上的“字符”(两个字节),在Java中的所有字符都使用Unicode编码。
字符型变量的三种表现形式:
- 用单引号包裹的单个字符,例如:char c1=‘a’;char c2=‘国’;
使用转义字符\将字符转变为特殊字符型常量,例如:char c3=‘\n’;//表示换行符 - 直接使用Unicode值来表示字符型常量:‘\uXXXX’(XXXX表示一个十六进制数),例如:\u000a表示\n。
- char类型可以通过它对应的Unicode码进行运算。
常见的转义字符:
转义字符 | 说明 |
---|---|
\b | 退格符 |
\n | 换行符 |
\r | 回车符 |
\t | 制表符 |
\" | 双引号 |
\’ | 单引号 |
\\ | 反斜线 |
矩阵操作
矩阵加法:两个同型的矩阵(行和列对应相等)可以相加,得到一个新的矩阵。如:
A
m
×
n
=
[
a
11
a
12
⋯
a
1
n
a
21
a
22
⋯
a
2
n
⋮
⋮
⋯
⋮
a
m
1
a
m
2
⋯
a
m
n
]
A_{m\times n}=\left[ \begin{matrix} a_{11} & a_{12} & \cdots & a_{1n}\\ a_{21} & a_{22} & \cdots & a_{2n}\\ \vdots & \vdots & \cdots & \vdots\\ a_{m1} & a_{m2} & \cdots & a_{mn}\\ \end{matrix} \right]
Am×n=⎣⎢⎢⎢⎡a11a21⋮am1a12a22⋮am2⋯⋯⋯⋯a1na2n⋮amn⎦⎥⎥⎥⎤
B
m
×
n
=
[
b
11
b
12
⋯
b
1
n
b
21
b
22
⋯
b
2
n
⋮
⋮
⋯
⋮
b
m
1
b
m
2
⋯
b
m
n
]
B_{m\times n}=\left[ \begin{matrix} b_{11} & b_{12} & \cdots & b_{1n}\\ b_{21} & b_{22} & \cdots & b_{2n}\\ \vdots & \vdots & \cdots & \vdots\\ b_{m1} & b_{m2} & \cdots & b_{mn}\\ \end{matrix} \right]
Bm×n=⎣⎢⎢⎢⎡b11b21⋮bm1b12b22⋮bm2⋯⋯⋯⋯b1nb2n⋮bmn⎦⎥⎥⎥⎤
C m × n = A + B = [ a 11 + b 11 a 12 + b 12 ⋯ a 1 n + b 1 n a 21 + b 21 a 22 + b 22 ⋯ a 2 n + b 2 n ⋮ ⋮ ⋯ ⋮ a m 1 + b m 1 a m 2 + b m 2 ⋯ a m n + b m n ] C_{m\times n}=A+B=\left[ \begin{matrix} a_{11}+b_{11} & a_{12}+b_{12} & \cdots & a_{1n}+ b_{1n}\\ a_{21}+b_{21} & a_{22}+b_{22} & \cdots & a_{2n}+b_{2n}\\ \vdots & \vdots & \cdots & \vdots\\ a_{m1}+b_{m1} & a_{m2}+b_{m2} & \cdots & a_{mn}+b_{mn}\\ \end{matrix} \right] Cm×n=A+B=⎣⎢⎢⎢⎡a11+b11a21+b21⋮am1+bm1a12+b12a22+b22⋮am2+bm2⋯⋯⋯⋯a1n+b1na2n+b2n⋮amn+bmn⎦⎥⎥⎥⎤
矩阵乘法:两个矩阵相乘,第一个矩阵的列数要等于第二个矩阵的行数。设,矩阵
A
m
×
n
A_{m\times n}
Am×n,矩阵
B
n
×
p
B_{n\times p}
Bn×p相乘,其中矩阵C中的第i行第j列元素表示为:
C
i
j
=
∑
k
=
1
n
a
i
k
b
k
j
=
a
i
1
b
1
j
+
a
i
2
b
2
j
+
⋯
+
a
i
n
b
n
j
C_{ij}=\sum_{k=1}^{n}a_{ik}b_{kj}=a_{i1}b_{1j}+a_{i2}b_{2j}+\cdots +a_{in}b_{nj}
Cij=k=1∑naikbkj=ai1b1j+ai2b2j+⋯+ainbnj
例如:
A
m
×
n
=
[
a
11
a
12
⋯
a
1
n
a
21
a
22
⋯
a
2
n
⋮
⋮
⋯
⋮
a
m
1
a
m
2
⋯
a
m
n
]
A_{m\times n}=\left[ \begin{matrix} a_{11} & a_{12} & \cdots & a_{1n}\\ a_{21} & a_{22} & \cdots & a_{2n}\\ \vdots & \vdots & \cdots & \vdots\\ a_{m1} & a_{m2} & \cdots & a_{mn}\\ \end{matrix} \right]
Am×n=⎣⎢⎢⎢⎡a11a21⋮am1a12a22⋮am2⋯⋯⋯⋯a1na2n⋮amn⎦⎥⎥⎥⎤
B
n
×
p
=
[
b
11
b
12
⋯
b
1
p
b
21
b
22
⋯
b
2
p
⋮
⋮
⋯
⋮
b
n
1
b
n
2
⋯
b
n
p
]
B_{n\times p}=\left[ \begin{matrix} b_{11} & b_{12} & \cdots & b_{1p}\\ b_{21} & b_{22} & \cdots & b_{2p}\\ \vdots & \vdots & \cdots & \vdots\\ b_{n1} & b_{n2} & \cdots & b_{np}\\ \end{matrix} \right]
Bn×p=⎣⎢⎢⎢⎡b11b21⋮bn1b12b22⋮bn2⋯⋯⋯⋯b1pb2p⋮bnp⎦⎥⎥⎥⎤
C
m
×
p
=
A
m
×
n
×
B
n
×
p
=
[
∑
k
=
1
n
a
1
k
b
k
1
∑
k
=
1
n
a
1
k
b
k
2
⋯
∑
k
=
1
n
a
1
k
b
k
p
∑
k
=
1
n
a
2
k
b
k
1
∑
k
=
1
n
a
2
k
b
k
2
⋯
∑
k
=
1
n
a
2
k
b
k
p
⋮
⋮
⋯
⋮
∑
k
=
1
n
a
m
k
b
k
1
∑
k
=
1
n
a
m
k
b
k
2
⋯
∑
k
=
1
n
a
m
k
b
k
p
]
C_{m\times p}=A_{m\times n}\times B_{n\times p}=\left[ \begin{matrix} \sum_{k=1}^{n}a_{1k}b_{k1} & \sum_{k=1}^{n}a_{1k}b_{k2} & \cdots & \sum_{k=1}^{n}a_{1k}b_{kp}\\ \sum_{k=1}^{n}a_{2k}b_{k1} & \sum_{k=1}^{n}a_{2k}b_{k2} & \cdots & \sum_{k=1}^{n}a_{2k}b_{kp}\\ \vdots & \vdots & \cdots & \vdots\\ \sum_{k=1}^{n}a_{mk}b_{k1}& \sum_{k=1}^{n}a_{mk}b_{k2} & \cdots & \sum_{k=1}^{n}a_{mk}b_{kp}\\ \end{matrix} \right]
Cm×p=Am×n×Bn×p=⎣⎢⎢⎢⎡∑k=1na1kbk1∑k=1na2kbk1⋮∑k=1namkbk1∑k=1na1kbk2∑k=1na2kbk2⋮∑k=1namkbk2⋯⋯⋯⋯∑k=1na1kbkp∑k=1na2kbkp⋮∑k=1namkbkp⎦⎥⎥⎥⎤
基本for循环
package day04;
public class ForStatement {
/**
* The entrance of the program.
*
* @param args Not used now.
*/
public static void main(String args[]) {
forStatement();
}// Of main
/**
* Method unit test.
*/
public static void forStatement() {
int tempN = 10;
System.out.println("1 add to " + tempN + " is: " + addToN(tempN));
tempN = 0;
System.out.println("1 add to " + tempN + " is: " + addToN(tempN));
int tempStepLength = 1;
tempN = 10;
System.out.println("1 add to " + tempN + " with step length " + tempStepLength + " is: "
+ addToNWithStepLength(tempN, tempStepLength));
tempStepLength = 2;
System.out.println("1 add to " + tempN + " with step length " + tempStepLength + " is: "
+ addToNWithStepLength(tempN, tempStepLength));
}// Of forStatement
/**
* Add from 1 to N.
*
* @param paraN The given upper bound.
* @return The sum.
*/
public static int addToN(int paraN) {
int resultSum = 0;
for (int i = 1; i <= paraN; i++) {
resultSum += i;
} // Of for i
return resultSum;
}// Of addToN
/**
* Add from 1 to N with a step length.
*
* @param paraN The given upper bound.
* @param paraStepLength The given step length.
* @return The sum.
*/
public static int addToNWithStepLength(int paraN, int paraStepLength) {
int resultSum = 0;
for (int i = 1; i <= paraN; i += paraStepLength) {
resultSum += i;
} // Of for i
return resultSum;
}// Of addToNwithStepLength
}// Of class ForStatement
运行效果:
二重循环——矩阵元素相加
package day04;
import java.util.Arrays;
public class MatrixAddition {
/**
* The entrance of the program.
*
* @param args Not used now.
*/
public static void main(String args[]) {
matrixElementSumTest();
matrixAdditionTest();
}// Of main
/**
* Sum the elements of a matrix.
*
* @param paraMatrix The given matrix.
* @return The sum of its elements.
*/
public static int matrixElementSum(int[][] paraMatrix) {
int resultSum = 0;
for (int i = 0; i < paraMatrix.length; i++) {
for (int j = 0; j < paraMatrix[0].length; j++) {
resultSum += paraMatrix[i][j];
} // Of for j
} // Of for i
return resultSum;
}// Of matrixElementSum
/**
* Unit Test for respective method.
*/
public static void matrixElementSumTest() {
int[][] tempMatrix = new int[3][4];
for (int i = 0; i < tempMatrix.length; i++) {
for (int j = 0; j < tempMatrix[0].length; j++) {
tempMatrix[i][j] = i * 10 + j;
} // Of for j
} // Of for i
System.out.println("The matrix is: \r\n" + Arrays.deepToString(tempMatrix));
System.out.println("The matrix element sum is: " + matrixElementSum(tempMatrix));
}// Of for matrixElementSumTest
/**
* Add two matrixes. Attention: No error check is provided at the moment.
*
* @param paraMatrix1 The first matrix.
* @param paraMatrix2 The second matrix. It should have the same size as the
* first one's.
* @return The addition of these matrixes.
*/
public static int[][] matrixAddition(int[][] paraMatrix1, int[][] paraMatrix2) {
int[][] resultMatrix = new int[paraMatrix1.length][paraMatrix1[0].length];
for (int i = 0; i < paraMatrix1.length; i++) {
for (int j = 0; j < paraMatrix1[0].length; j++) {
resultMatrix[i][j] = paraMatrix1[i][j] + paraMatrix2[i][j];
} // Of for j
} // Of for i
return resultMatrix;
}// Of matrixAddition
/**
* Unit test for respective method.
*/
public static void matrixAdditionTest() {
int[][] tempMatrix = new int[3][4];
for (int i = 0; i < tempMatrix.length; i++) {
for (int j = 0; j < tempMatrix[0].length; j++) {
tempMatrix[i][j] = i * 10 + j;
} // Of for j
} // Of for i
System.out.println("The matrix is: \r\n" + Arrays.deepToString(tempMatrix));
int[][] tempNewMatrix = matrixAddition(tempMatrix, tempMatrix);
System.out.println("The new matrix is: \r\n" + Arrays.deepToString(tempNewMatrix));
}// Of matrixAdditionTest
}// Of class MatrixAddition
运行效果:
三重循环——矩阵相乘
package day04;
import java.util.Arrays;
public class MatrixMultiplication {
/**
* The entrance of the program.
*
* @param args Not used now.
*/
public static void main(String args[]) {
matrixMultiplicationTest();
}
/**
* Matrix multiplication. The columns of the first matrix should be equal to the
* rows of the second one.
*
* @param paraFirstMatrix The first matrix.
* @param paraSecondMatirx The second matrix.
* @return The result matrix.
*/
public static int[][] multiplication(int[][] paraFirstMatrix, int[][] paraSecondMatirx) {
int m = paraFirstMatrix.length;
int n = paraFirstMatrix[0].length;
int p = paraSecondMatirx[0].length;
// Step 1. Dimension check.
if (paraSecondMatirx.length != n) {
System.out.println("The two matrices cannot be multiplied.");
return null;
} // Of if
// Step 2. The loop.
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] * paraSecondMatirx[k][j];
} // Of for k
} // Of for j
} // Of for i
return resultMatrix;
}// Of multiplication
public static void matrixMultiplicationTest() {
int[][] tempFirstMatrix = new int[2][3];
for (int i = 0; i < tempFirstMatrix.length; i++) {
for (int j = 0; j < tempFirstMatrix[0].length; j++) {
tempFirstMatrix[i][j] = i * 10 + j;
} // Of for j
} // Of for i
System.out.println("The first matrix is:\r\n" + Arrays.deepToString(tempFirstMatrix));
int[][] tempSecondtMatrix = new int[3][2];
for (int i = 0; i < tempSecondtMatrix.length; i++) {
for (int j = 0; j < tempSecondtMatrix[0].length; j++) {
tempSecondtMatrix[i][j] = i * 10 + j;
} // Of for j
} // Of for i
System.out.println("The second matrix is:\r\n" + Arrays.deepToString(tempSecondtMatrix));
int[][] tempThirdMatrix = multiplication(tempFirstMatrix, tempSecondtMatrix);
System.out.println("The third matrix is:\r\n" + Arrays.deepToString(tempThirdMatrix));
System.out.println("Trying to mutiplt the first with itself.\r\n");
tempThirdMatrix = multiplication(tempFirstMatrix, tempFirstMatrix);
System.out.println("The result matrix is: \r\n" + Arrays.deepToString(tempThirdMatrix));
}// Of matrixMultiplicationTest
}// Of class MatrixMultiplication
运行效果:
基本while循环
package day04;
public class WhileStatement {
/**
* The entrance of the program.
*
* @param args Not used now.
*/
public static void main(String args[]) {
whileStatemnetTest();
}// Of main
/**
* The sum not exceeding a given value.
*/
public static void whileStatemnetTest() {
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);
// Approach 2.
System.out.println("\r\nAlternative approach.");
tempValue = 0;
tempSum = 0;
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 whileStatemnetTest
}// Of class WhileStatement
运行效果:
总结:当我们写一个程序的时候,首先要明白自己写的程序要达到什么效果,再根据实际情况选择合适的数据结构,然后是思考我们人要实现这样的效果该怎么处理数据,整理逻辑,然后设计代码让计算机来替我们实现目标,最后检查。