你的位置:首页 > Java教程

[Java教程]初涉java库

我的车就差一个轮子啦,造好轮子,我就飞上天与太阳肩并肩啦,想想都激动。什么你要自己造轮子,是不是傻,商店里不都是别人造好的吗,又好又方便,只需一点money,你没有money,那你只能做个安静的美男子啦。幸运的是编程世界中的轮子不需要money,今天就来看看如何调用库中的轮子。

今天的内容:

 

一.修改bug
 这篇博客是接着上篇的,没有看的,请点击此出查看:上一篇随笔
现在揭示那个程序的最大的bug:

看测试代码,你可能已经猜到啦。看结果:

没错,你可以连续猜同一个位置来过关,看看哪里错啦,肯定是在checkYourself方法里,看看
源代码:
 1 public String checkYourself(int guess) { 2     String result = "miss"; 3     for(int i: localCells) { 4       if ( i == guess) {//就在这里,猜中后没有做任何处理 5         result = "hit"; 6         numOfHit ++; 7         break; 8       } 9     }10     if (numOfHit == 3) {11       result = "kill";12     }13     return result;14   }

既然知道了问题,就该解决它。你可以有以下解决方案。

1.再创建一个boolean[] hitFlag = {false, false, false},猜中哪个位置,就把对应的hitFlag数组中的位置设置为true, 在猜中之后查看hitFlag中相应位置的状态若为false,则成功,否则猜过,此次失败。

这个方法是最笨的,所以一般聪明的你不会想到。

2.猜中后可以把那个位置的数字设置为 -1,哈哈,瞬间感觉世间好美好啊。但为什么不把猜过的去掉呢?从而只需判断你的猜测结果是否在localCells里就行啦。

3.要是有一种能缩放的数组就好啦,把没必要的东西都从里面剔除掉,这样,程序运行越来越好啊。有没有呢?还真有一个,java库中有个ArrayList,非常符合这个要求。


 


 二、ArrayList:

 测试代码:

 1 import java.util.*; 2 public class Test { 3     public static void main(String[] args) { 4         SimpleDotCom dotCom = new SimpleDotCom(); 5         ArrayList<String> localCells = new ArrayList(); 6         localCells.add("1"); 7         localCells.add("2"); 8         localCells.add("3"); 9         dotCom.setDotCom(localCells);10         11         String result = dotCom.CheckYourself("1");12         System.out.println("result = " + result);13         14         result = dotCom.CheckYourself("1");15         System.out.println("result = " + result);16         17         result = dotCom.CheckYourself("2");18         System.out.println("result = " + result);19         20         result = dotCom.CheckYourself("3");21         System.out.println("result = " + result);22         23         24     }25     26 }

 


 结果就正确啦:

简单版的DotCom已经完成啦,别忘了我们的初衷哦,要写一个完整版的dotcom,不过代码稍微有点多而且有点复杂,要静下心来看,所以就跳转到另一个随笔,去看完整版的代码吧。
 
下面几句话中的知识比较细小,但我认为还是不错的。
 
在说个问题,java中的类都是存在包(package)中的,使用报的优点有三点:
  • 帮你结构化的管理你的项目,避免一大堆文件放在一起。
  • 帮你分类,如与图形库有关的api,都在javax.swing包中,与事件有关的都在java.awt中。
  • 是你的程序更安全,只有包内的文件之间是可见的,你可以认为这个功能和c++ c#中的命名空间一样。

再稍微解释一下,为什么有的包名多出个“X”,如swing的javax。名字为“java"的包是刚开始的标准库, ”java“是后来扩展为java标准库的。


你使用java类必须使用其全名,比如,你使用的System.out。ArrayList 你必须这样是使,java.util.ArrayList<String> list = new java.util.ArrayList<String>();。但诚如你所见,你没有这样写,因为文件前面的 import java.util.*,引入java.util包下的所有东西(不太恰当)。你可能会疑惑 使用String时,你啥也没import 啊,非常常用的类都在java.lang中,所以java.lang已是默认的包。
如果你学过c/c++,你可能认为 import 越多,你的程序越大,那你就错啦。import xxx。可没有把xxx下的东西都拷进来,只是帮你需要xxx全名的地方省去这个全名而已。
 


三、布尔值表达式
java中的true and false 不像 c/c++中的布尔值,c/c++中的true 就是整形值 1, false == 0, 而且条件判断会把非零的值看作真,看看下边的c语言代码:
而java中的boolean本身就是一种类型,条件判断只认boolean类型,上边的代码如果是java肯定会错的。

另一种就是html 文档啦,免费又方面,强烈推荐它,比参考书详细,而且时效性好,上官方随时下最新的文档。

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAA0JCgsKCA0LCgsODg0PEyAVExISEyccHhcgLikxMC4pLSwzOko+MzZGNywtQFdBRkxOUlNSMj5aYVpQYEpRUk//2wBDAQ4ODhMREyYVFSZPNS01T09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09P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