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通用性),布布扣,bubuko.com

时间: 2024-08-10 23:16:06

cs108 03 ( 调试, java通用性)的相关文章

Tomcat使用MyEclipse远程调试Java代码配置详解

Tomcat使用MyEclipse远程调试Java代码总结如下:在做远程调试时,在windows系统和非windows系统下的配置,Tomcat中会有所差别,具体如下: 第一步.配置tomcat一.在windows系统中:打开%CATALINE_HOME%/bin下的文件catalina.bat,加入下面这行:set CATALINA_OPTS=-server -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket

myeclipse调试java

利用junit调试java代码 先打断点,右键debug as junit,打开debug视图 点击F6,执行过变量所在语句,把鼠标移到变量上就可以看到变量的值

Eclipse调试Java的10个技巧

原文地址: http://www.oschina.net/question/82993_69439 在看这篇文章前,我推荐你看一下Eclipse 快捷键手册,我的eclipse版本是4.2 Juno. 先提三点 不要使用System.out.println作为调试工具 启用所有组件的详细的日志记录级别 使用一个日志分析器来阅读日志 1.条件断点 想象一下我们平时如何添加断点,通常的做法是双击行号的左边.在debug视图中,BreakPoint View将所有断点都列出来,但是我们可以添加一个bo

使用 Eclipse 调试 Java 程序的技巧【9】

若有不正之处,请多多谅解并欢迎批评指正,不甚感激.请尊重作者劳动成果: 本文原创作者:pipi-changing本文原创出处:http://www.cnblogs.com/pipi-changing/ 本文版权归作者和博客园共有,未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接 ,否则保留追究法律责任的权利. 使用 Eclipse 调试 Java 程序的技巧 不要调试太多 只调试你觉得有问题的代码或者一部分一部分功能的调试: [ 断点视图 : 条件断点] 如果你只对应用中的某部分感

【Eclipse】eclipse调试java程序的九个技巧

本文转自[半夜乱弹琴],原文地址:http://www.cnblogs.com/lingiu/p/3802391.html 九个技巧: 逻辑结构 条件debug 异常断点 单步过滤 跳到帧 Inspect expressions display 远程debug 最早开始用eclipse的debug的时候,只会F5 F6 F7 F8,甚至F7都不是很搞的明白是怎么用的,那时候资浅,碰不到需要复杂debug的代码,慢慢工作深入了,场景碰多了,就需要各种debug技巧来提升定位bug效率,以前找人帮忙

远程debug调试java代码

远程debug调试java代码 日常环境和预发环境遇到问题时,可以用远程调试的方法本地打断点,在本地调试.生产环境由于网络隔离和系统稳定性考虑,不能进行远程代码调试. 整体过程是通过修改远程服务JAVA_OPTS参数,然后本地通过Eclipse或IDEA等工具调试. 下面简单介绍下理论. 理论 JPDA(Java Platform Debugger Architecture)是Java平台调试体系结构的缩写.由3个规范组成,分别是JVMTI(JVM Tool Interface),JDWP(Ja

使用Eclipse调试Java 程序的10个技巧

你应该看过一些如<关于调试的N件事>这类很流行的帖子 .假设我每天花费1小时在调试我的应用程序上的话,那累积起来的话也是很大量的时间.由于这个原因,用这些时间来重视并了解所有使我们调试更方便的功能. 那能为你省下一些时间,也将会使你的生活更安逸.轻松.同时也表明其它关于此主题的帖子也是很有价值的. 第1条:不要调试太多 一个关于调试的疯狂声明作为开头.但它必须是要说的!尝试切分一下你那复杂的逻辑成多个独立的单元,并编写单元测试来检测你代码的正确性.我想像如下 这样的流程应该是发生得非常频繁的-

用GDB 调试Java程序

陈皓 http://blog.csdn.net/haoel 背景 想要使用GDB调试程序,就需要用GNU的编译器编译程序.如:用GCC编译的C/C++的程序,才能用GDB调试.对于Java程序也是一样的,如果想要用GDB调试,那么就需要用GNU的Java编译器--GCJ来编译Java程序. 目前,很多Linux都不会预装Sun的JVM,取而代之是使用GNU的开源编译器来编译和运行Java程序.比如RedHat和Ubuntu,其默认安装都是使用GNU的Java编译器(gcj)和解释器(gij).当

调试Java源代码时变量的值无法追踪怎么办?

问题:调试Java源代码时,只能看到源代码,却无法查看源代码中的变量的即时值 原因:jre为了节省空间,在打包时去掉了class文件中的调试信息. 思路:使用jdk里的src.zip源码重新编译生成完整的带有调试信息的class文件 做法: 1.新建一个Java Project 2.解压缩src.zip,把里面的java和javax两个文件夹复制粘贴到工程里作为source 3.在Eclipse中,打开window/preferences/java/compiler/errors or warn