Debuger

Great questions

These questions will solve most bugs:

what method shows the symptom ? what lines of code produces that symptom ?

what is the state of the receiver object in that code ? what were the param values passed in ?

if it’s an exception, what does the exception error message say – null pointer? array access? Somethimes the exception name can be very informative.

调优方法

Eclipse debugger , 好用

println()

注释掉部分代码

Truths of Debugging

  • 直觉很重要, 你可以测试你直觉的想法, 但当直觉与实际发生碰撞时, 实际发生获胜.
  • 简单的代码也会引起重大的bug, 不要忽视那些简单的代码, 往往是它们出现问题.
  • 注意你自己定义的变量, 程序中出现的bug, 往往是你定义的变量不是你想要的值.
  • 如果你的程序1分钟前还可以正常执行, 但现在不行了, 那么你上次改动过什么? 注意: 如果你每写50行code就测试一下, 那么当程序出问题时, 你就 知道是哪50行出现的问题.
  • 不要随便修改代码来追踪bug, 这样有可能带来新的bug.
  • 如果你发现一些错误跟你一直追踪的错误没有关系, 那么先来将这些错误搞定吧, 没准这些错误与你一直追踪的bug是有关系的, 只是你还没想到.

Java 通用性(Generics)

  • Using a generic class, like using ArrayList<String>
  • Writing generic code with a simple <T> or <?> type parameter
  • Writing generic code with a <T extends Foo> type parameter

Use Generic Class

ArrayList<String> strings = new ArrayList<String>();
strings.add("hi");
strings.add("there");
String s = strings.get(0);

循环使用

List<String> strings = ...
for (String s: strings) {
System.out.println(s);
}

例子:

public static void dempList() {
List<String> a = new ArrayList<String>();
a.add("Don't");
a.add("blame");
a.add("me"); for (String str: a) {
System.out.println(str);
} Iterator<String> it = a.iterator();
while (it.hasNext()) {
String string = it.next();
System.out.println(String);
}
List<Integer> ints = new ArrayList<Integer>();
for (int i = 0; i<10; i++) {
ints.add(new Integer(i * i));
}
int sum = ints.get(0).intValue() + ints.get(1).intValue(); sum = ints.get(0) + ints.get(1); // Generic Map Example Code
public static void demoMap() {
HashMap<Integer, String>map = new HashMap<Integer, String>();
map.put(new Integer(1), "one");
map.put(new Integer(2), "two");
map.put(3, "three"); // 自动包装
map.put(4, "four"); String s = nap.get(new Integer(3));
s = map.get(3) // 自动包装 HashMap<String, List<Integer>> counts = new HashMap<String, List<Integer>>();
List<Integer> evens = new ArrayList<Integer>();
evens.add(2);
evens.add(4);
evens.add(6);
counts.put("even", evens); List<Integer> evens2 = counts.get("evens");

Define a Generic<T> Class/Method

you can define your own class as a generic class. the class definithion code is parameterized by a type, typically called<T>. This is more or less what ArrayList does. At the very start of the class, the parameter is added like this: public calss Foo<T>

这个 T 有以下限制: (T 的本质就好比是一个普通的类型)

- declare variables, parameters, and return types of the type T

- use = on T pointers

- call methods that work on all Objects, like .equals()

记住: where you see “T”, it is just replaced by “Object” to produce the code for runtime. So the ArrayList<String>code and the ArrayList<Integer>code … those two are actually just the ArrayList<Object>code at runtime.

例子:

public class Pair<T> {
private T a;
private T b;
private List<T> unused; public Pair(T a, T b) {
this. a = a;
this.b = b;
}
public T getA() {
return a;
}
public T getB() {
return b;
}
public void swap() {
T temp = a;
a = b;
b = temp;
} public boolean isSame() {
return a.equals(b);
} public boolean contains(T elem) {
return (a.equals(elem) || b.equals(elem));
}
public static void main(String[] args) { // integer 类型的可以
Pair<Integer> ipair = new Pair<Integer>(1, 2);
Integer a = ipair.getA();
int b = ipair.getB(); // 没包装 // String 类型的也可以
Pair<String> spair = new Pair<String>("hi", "there");
String s = spair.getA();
}

Generic <T> Method

与整个类都使用通用来说, 可以针对某个方法来使用通用, 语法: public <T> void foo(List<T> list)

例如:

public static <T> void removeAdjacent(Collection<T> coll) {
Iterator<T> it = coll.iterator();
T last = null;
while (it.hasNext()) {
T curr = it.next();
if (curr == las) it.remove();
last = curr;
}
}

<T> Method – use a <T> type on the method to identify what type of element is in the collection. The <T> goes just before the return type. T can be used to decalre variables, return types, etc. This is ok, but slightly heavyweight, since in this case we actually don’t care what type of thing is in there. This removes elements that are == to an adjacent element.

?/T with “extends” Generics

extends 可以限制 T, 例如 with a <T extends Number> – for any T value , we can assume it is a Number subclass, so .intValue()

例如:

public class PairNumber <T extends Number> {
private T a;
private T b; public PairNumber( T a, T b) {
this.a = a;
this.b = b;
}
public int sum() {
return (a.intValue() + b.intValue());
}

?/T Extends Method

public static int sumAll(Collection<? extends Number> nums) {
int sum = 0;
for (Number num : nums) {
sum += num.intValue(0;
}
return sum;
}

最新文章

  1. 记一次联想A820t救砖线刷
  2. ns3模拟无线Ad hoc 网络通信
  3. (2015秋) 作业6:(电梯系统之结对编程 I 总分=2*50 分)
  4. SQL Server 性能调优(一)——从等待状态判断系统资源瓶颈【转】
  5. 浅析ODS与EDW关系(转载)
  6. Android 解析XML
  7. Python脚本控制的WebDriver 常用操作 &lt;二十八&gt; 超时设置和cookie操作
  8. 【HDU 5370】 Tree Maker(卡特兰数+dp)
  9. python重要的函数代码块
  10. Windows 7下 搭建 基于 ssh 的sftp 服务器
  11. C++ STL中的常用容器浅谈
  12. Sql Server函数全解&lt;五&gt;之系统函数
  13. 通过xib自定义UITableViewCell
  14. JAVA基础——数组详解
  15. Python基础2 编码和逻辑运算符
  16. Linux 调试打印时间和颜色
  17. zipkin 整合elastic
  18. HashMap的实现原理-----哈希讲解
  19. linux chmod 给目录或文件赋权限 可读可写可执行
  20. Sql server management studio: cannot find one or more components

热门文章

  1. ImageResizer for .net 图片处理强大类库
  2. MySQL 官方文档
  3. canves 图片旋转 demo
  4. javascript 的继承实例
  5. PHP-静态方法(static)继承等分析
  6. jquery api 常见api 元素操作例子
  7. WCF深入浅出学习1
  8. MySQL 添加外键约束,不检查现有数据
  9. js闭包的应用
  10. EF6 Code First 模式更新数据库架构