有以下规律的矩阵
如以下4维矩阵。
0 0 0 0
0 1 1 1
0 1 2 2
0 1 2 3
首先要找到规律,在对角线上的数字向右向下都是一样的数字。可以用二维数组表示矩阵,二维数组有n行n列。当一个折线上的数字为0时,二维数组的元素有a[0][0],a[0][1],a[0][2],a[0][3],a[1][0],a[2][0],a[3][0]。以此类推,当数字都是1时。。。。。
程序实现:
package aop; /** * Created with IntelliJ IDEA. * User: ASUS * Date: 14-8-21 * Time: 上午10:47 * To change this template use File | Settings | File Templates. */ public class TestArray { public static void main(String[] args) { int n; n = Integer.parseInt(args[0]); //n表示维度 int[][] arrayName = new int[n][n]; for (int k = 0; k < n; k++) { int i, j; i = k; j = k; while (j < n) { arrayName[i][j] = k; j++; } i = k + 1; j = k; while (i < n) { arrayName[i][j] = k; i++; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) System.out.print(arrayName[i][j] + " "); System.out.println(); } } }
不知道还有没更好的算法,请指教。。。
直接看程序:
抽象Person类,test方法声明抛出了两个异常NumberFormatException,IoException。
package binarytree; import java.io.IOException; public abstract class Person { public abstract void test() throws NumberFormatException, IOException; }
看子类覆写方法如何声明抛出异常
package binarytree; /** * Created with IntelliJ IDEA. * User: ASUS * Date: 14-8-22 * Time: 下午5:19 * To change this template use File | Settings | File Templates. */ public class Teacher extends Person { //一个方法在父类中声明了抛出异常,子类覆盖该方法的时候, // 要么不声明抛出异常,要么声明被抛出的异常继承自它所覆盖的父类中的方法抛出的异常。 // 在这里不声明抛出异常 @Override public void test() { //To change body of implemented methods use File | Settings | File Templates. System.out.println("test exception"); } }
package binarytree; import java.io.IOException; public class TeacherBac extends Person { @Override public void test() throws IOException { //To change body of implemented methods use File | Settings | File Templates. } }
如下面这段代码,
package binarytree; public class TeacherCba extends Person { @Override public void test() throws NumberFormatException { //To change body of implemented methods use File | Settings | File Templates. } }
package binarytree; import java.io.IOException; public class TeacherAbc extends Person { @Override public void test() throws NumberFormatException, IOException { //To change body of implemented methods use File | Settings | File Templates. } }
现在在考虑这么一个情况,此时写一个类继承自Teacher类,同时覆写test方法,test方法要不要声明抛出异常??
实际上不要被Person类的test方法迷惑,以为此时是继承Teacher类,覆写的方法也是Teacher的test方法,所以要看Teacher的test方法是否声明抛出异常了。由于Teacer的test方法没有声明抛出人任何异常,所以。。。。。
package binarytree; public class Teacher0 extends Teacher { //由于父类Teacher不声明抛出异常,所以这里也不能声明抛出异常 //虽然在Person类中test方法有声明抛出异常 @Override public void test() { } }
Unlike if-then and if-then-else statements, the switch statement can have a number of possible execution paths. A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types (discussed in Enum Types), the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer (discussed in Numbers and Strings).
先引用Java tutorial中的一段代码
Using Strings in switch Statements
In Java SE 7 and later, you can use a String object in the switch statement's expression. The following code example, StringSwitchDemo, displays the number of the month based on the value of the String named month:
public class StringSwitchDemo { public static int getMonthNumber(String month) { int monthNumber = 0; if (month == null) { return monthNumber; } switch (month.toLowerCase()) { case "january": monthNumber = 1; break; case "february": monthNumber = 2; break; case "march": monthNumber = 3; break; case "april": monthNumber = 4; break; case "may": monthNumber = 5; break; case "june": monthNumber = 6; break; case "july": monthNumber = 7; break; case "august": monthNumber = 8; break; case "september": monthNumber = 9; break; case "october": monthNumber = 10; break; case "november": monthNumber = 11; break; case "december": monthNumber = 12; break; default: monthNumber = 0; break; } return monthNumber; } public static void main(String[] args) { String month = "August"; int returnedMonthNumber = StringSwitchDemo.getMonthNumber(month); if (returnedMonthNumber == 0) { System.out.println("Invalid month"); } else { System.out.println(returnedMonthNumber); } } }
The output from this code is 8.
The String in the switch expression is compared with the expressions associated with each case label as if the String.equals method were being used. In order for the StringSwitchDemo example to accept any month regardless of case, month is converted to lowercase (with the toLowerCase method), and all the strings associated with the case labels are in lowercase.
Note: This example checks if the expression in the switch statement is null. Ensure that the expression in any switch statement is not null to prevent a NullPointerException from being thrown.
好了,switch的用法都是大同小异,现在来看switch的陷阱。。
package binarytree; /** * Created with IntelliJ IDEA. * User: ASUS * Date: 14-8-22 * Time: 下午6:01 * To change this template use File | Settings | File Templates. */ public class TestSwitch { public static void main(String[] args) { int m = Integer.parseInt(args[0]); System.out.println("m=" + m); switch (m) { case 1: System.out.println(1); case 2: System.out.println(2); System.out.println(2); case 3: System.out.println(3); case 4: System.out.println(4); break; case 5: System.out.println(5); break; case 6: System.out.println(6); case 7: System.out.println(7); default: System.out.println("default out"); } } }
有这样一段代码,问:m为何值时,会打印default out。。
想当然我们会认为当m的值和case值不匹配时,但你要想到这里是有陷阱的。。
在使用switch时,有这么一条规则:
一旦case匹配,就会顺序执行后面的程序代码,而不管后面的case是否匹配,直到遇见break
由这条规则我们可知:
首先当m的值不匹配时,肯定会打印default out,但是当m = 6 , m = 7时,也会打印default out。。
看程序测试结果:
m=86
default out
m=6
6
7
default out
m=7
7
default out
但m = 1 时,会是什么情况:
m=1
1
2
2
3
4
m = 1 匹配,但该case没有break,所以知道遇到break才结束。。。
当有finally语句块,总是会执行finally语句块
下面这段程序:
package aop; public class TestException { public static void main(String[] args) { int flag = Integer.parseInt(args[0]); System.out.println("flag=" + flag); int n = exception(flag); System.out.println(n); } public static int exception(int flag) { try { if (flag == 1) { throw new Exception(); } else if (flag == 2) { return flag; } } catch (Exception e) { return flag; } finally { flag = 3; return flag; } } }
catch语句块有return语句,但没有返回,而是继续执行finally语句块,从finally语句块返回结果。。
try块中有return语句,但即使没有异常发生,也会执行finally语句块,从finally语句块返回结果。。
程序运行结果:
当flag = 1 时,
flag=1
3
当flag = 2 时,
flag=2
3
当flag = 3 时,
flag=3
3
finally语句块总是会执行也是有特殊情况的,如下面这种情况:
package aop; public class TestException { public static void main(String[] args) { int flag = Integer.parseInt(args[0]); System.out.println("flag=" + flag); int n = exception(flag); System.out.println(n); } public static int exception(int flag) { try { if (flag == 1) { throw new Exception(); } else if (flag == 2) { return flag; } else { System.exit(0); } } catch (Exception e) { return flag; } finally { flag = 3; return flag; } } }
程序结果:
flag=3
Process finished with exit code 0
没有执行finally语句块,这是因为在else语句块中执行了程序退出操作,所以不会执行finally语句块。。
====END===