cs108 03 ( 调试, java通用性)
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;
}
cs108 03 ( 调试, java通用性)的更多相关文章
- Eclipse快速入门:远程调试Java应用
Eclipse快速入门:远程调试Java应用 2012年03月27日00:00 it168网站原创 作者:皮丽华 编辑:皮丽华 我要评论(0) 标签: Eclipse , Java , Java框架, ...
- 远程debug调试java代码
远程debug调试java代码 日常环境和预发环境遇到问题时,可以用远程调试的方法本地打断点,在本地调试.生产环境由于网络隔离和系统稳定性考虑,不能进行远程代码调试. 整体过程是通过修改远程服务JAV ...
- [原创] 如何用Eclispe调试java -jar xxx.jar 方式执行的jar包
有时候,我们经常会需要调试 java -jar xxx.jar方式运行的代码,而不是必须在Eclipse中用Debug或者Run的方式运行.比如我们拿到的SourceCode不完整.Java提供了一种 ...
- paip. java resin 远程 调试 java resin remote debug
paip. java resin 远程 调试 java resin remote debug 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 ...
- 使用JDB调试Java程序
Java程序中有逻辑错误,就需要使用JDB来进行调试了.调试程序在IDE中很方便了,比如这篇博客介绍了在Intellj IDEA中调试Java程序的方法. 我们课程内容推荐在Linux环境下学习,有同 ...
- 如何在Eclipse中Debug调试Java代码
背景 有的时候你想debug调试Java的源代码,就想试图在Java源代码中设置断点,在Eclipse中常常会出现Unable to insert breakpoint Absent Line Num ...
- 像调试java一样来调试Redis lua
高并发的系统中,redis的使用是非常频繁的,而lua脚本则更是锦上添花.因为lua脚本本身执行的时候是一个事务性的操作,不会掺杂其他外部的命令,所以很多关键的系统节点都会用redis+lua来实现一 ...
- 【转】如何用Eclispe调试java -jar xxx.jar 方式执行的jar包
原文地址:https://www.cnblogs.com/zzpbuaa/p/5443269.html 有时候,我们经常会需要调试 java -jar xxx.jar方式运行的代码,而不是必须在Ecl ...
- Eclipse调试Java的10个技巧【转】
clipse调试Java的10个技巧 先提三点 不要使用System.out.println作为调试工具 启用所有组件的详细的日志记录级别 使用一个日志分析器来阅读日志 1.条件断点 想象一下我们平时 ...
随机推荐
- 想控制GIF图片动画播放吗?试试gifffer.js
在线演示:http://www.gbtags.com/gb/demoviewer/3578/c6bec39a-61ae-4017-8e23-e0bc1eeb740f/example|index.htm ...
- android.content.res.Resources$NotFoundException: String resource ID #0x0
仔细检查是不是在settext的时候设置进去的时int属性的值,所以android会认为这是在strings中的值,所以会拿着这个int值当做string的id值去找,结果当然是找不到的.
- MySQL 中now()时间戳用法
MySQL 中now()时间戳用法 UPDATE news set addtime = unix_timestamp(now()); #结果:1452001082
- OSI模型图文说明
网关工作在第四层传输层及其以上 网络层:路由器 数据链路层:网桥,交换机 物理层:网卡,网线,集线器,中继器,调制解调器 OSI共7层:应用层,表示层,会话层,传输层,数据链路层,物理层. [7]:应 ...
- 【转发】未能加载文件或程序集“Oracle.DataAccess”或它的某一个依赖项。试图加载格式不正确的程序。
http://www.cnblogs.com/joey0210/archive/2012/09/29/2708420.html 上一篇文章说到了DLL引用问题,主要是说的程序中如果使用过了反射, ...
- SuperMap iObjects for Spark使用
本文档环境基于ubuntu16.04版本,(转发请注明出处:http://www.cnblogs.com/zhangyongli2011/ 如发现有错,请留言,谢谢) 1. 基础环境搭建 基础环境搭建 ...
- c# 隐藏Tab控件的标签
public void HideTabcontrolLabel(TabControl tabControl1) { tabControl1.Appearance = TabAppearance.Fla ...
- IT行业面试指导 计算机行业面试技巧 面试技巧
简历篇 简历是你的的第一张脸,简历写的是否合理,是否吸引人,将决定你能否赢得宝贵的面试机会,迈出第一步! l 姓名,性别,学历,居住地,工作年限,邮箱,手机号 l 填“现居住地”,不要填成“户籍所 ...
- CodeMirror与jquery UI-Tabs混合使用 注意事项
第一步:.将代码高亮渲染 第二步:jquery Tab输出: 第三步:点击Tab切换时,将代码块刷新: 参考:http://jtmorris.net/2013/06/codemirror-editor ...
- Linux下编译、使用静态库和动态库 自己测过的
每个程序实质上都会链接到一个或者多个的库.比如使用C函数的程序会链接到C运行时库,GUI程序会链接到窗口库等等.无论哪种情况,你都会要决定是链接到静态库(static libary)还是动态库(dyn ...