留下两个例子作为参考,

1. 追逐小方块的例子

2. HashMap 和 Iterator 的例子

Example one:

import acm.graphics.*;
import acm.program.*;
import java.awt.*;
import java.awt.event.*; public class TargetSeeker extends GraphicsProgram { /* private constants */
private static final int TARGET_SIZE = 10;
private static final int SEEKER_SIZE = 20;
private static final int PAUSE_TIME = 10; public void run() {
initTarget();
initSeeker();
addMouseListeners(); while (true) {
seek();
}
} private void initTarget() {
targetSquare = new GRect(TARGET_SIZE, TARGET_SIZE);
targetSquare.setColor(Color.red);
targetSquare.setFilled(true);
targetX = getWidth() / 2;
targetY = getHeight() / 2;
add(targetSquare, targetX - TARGET_SIZE/2, targetY - TARGET_SIZE/2);
} private void initSeeker() {
seeker = new GRect(SEEKER_SIZE, SEEKER_SIZE);
add(seeker, 0, 0);
} private int moveAmount(double seekerPos, double targetPos) {
int amount = 0;
if (targetPos > seekerPos) {
amount = 1;
} else if (targetPos < seekerPos) {
amount = -1;
}
return amount;
} private void seek() {
pause(PAUSE_TIME);
double seekerMidX = seeker.getX() + SEEKER_SIZE / 2;
int dx = moveAmount(seekerMidX, targetX);
double seekerMidY = seeker.getY() + SEEKER_SIZE / 2;
int dy = moveAmount(seekerMidY, targetY); seeker.move(dx, dy);
} public void mouseClicked(MouseEvent e) {
targetX = e.getX();
targetY = e.getY();
remove(targetSquare);
add(targetSquare, targetX - TARGET_SIZE/2, targetY - TARGET_SIZE/2);
} /* private instance variable */
private int targetX;
private int targetY;
private GRect targetSquare;
private GRect seeker;
}

Example two:

public void PrintMatchingKeys(HashMap<String, String> map) {
ArrayList<String> keys = new ArrayList<String>();
Iterator<String> it = map.keySet().iterator();
while (it.hasNext()) {
// keys is array list, it is the key (String type) of map
keys.add(it.next());
}
// Reset "it" iterator to allow us to iterate over keys again
it = map.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
String value = map.get(key);
// of course contain
if (key.contains(value)) {
println(key + ": " + value);
}
}
}

Java 07 example的更多相关文章

  1. Effective Java 07 Avoid finallizers

    NOTE Never do anything time-critical in a finalizer. Never depend on a finalizer to update critical ...

  2. .Net转Java.07.IDEA和VS常用操作、快捷键对照表

      功能 IDEA 2017.1 快捷键   Visual Studio 2015 快捷键 文档 格式化整个文档 Ctrl+Alt+L   Ctrl+E,D 或者 Ctrl+K,D  文件 显示最近的 ...

  3. 从零开始学JAVA(07)-使用SpringMVC4写helloworld

    一.关于开发环境 Eclipse IDE for Java EE Developers Jdk 1.7 (32位版本) SpringMVC 4.1.5.RELEASE apache-tomcat-7. ...

  4. Effective Java Index

    Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...

  5. Java性能优化权威指南-读书笔记(一)-操作系统性能监控工具

    一:CPU 1. 用户态CPU是指执行应用程序代码的时间占总CPU时间的百分比. 系统态CPU是指应用执行操作系统调用的时间占总CPU时间的百分比.系统态CPU高意味着共享资源有竞争或者I/O设备之间 ...

  6. 检查Linux服务器性能

    如果你的Linux服务器突然负载暴增,告警短信快发爆你的手机,如何在最短时间内找出Linux性能问题所在? 概述通过执行以下命令,可以在1分钟内对系统资源使用情况有个大致的了解. • uptime• ...

  7. 60,000毫秒内对Linux的性能诊断效的方法

    转载于:http://www.itxuexiwang.com/a/liunxjishu/2016/0225/168.html?1456484140 60,000 毫秒内对 Linux 的性能诊断 当你 ...

  8. 数据库ORM框架GreenDao

    常用的数据库: 1). Sql Server2). Access3). Oracle4). Sysbase5). MySql6). Informix7). FoxPro8). PostgreSQL9) ...

  9. Linux系统性能10条命令监控

    Linux系统性能10条命令监控 概述 通过执行以下命令,可以在1分钟内对系统资源使用情况有个大致的了解. uptime dmesg | tail vmstat 1 mpstat -P ALL 1 p ...

随机推荐

  1. JavaScript 正则表达式——基本语法(2)

    来源:http://www.cnblogs.com/dolphinX/p/3486214.html 定义 JavaScript种正则表达式有两种定义方式,定义一个匹配类似 <%XXX%>  ...

  2. org.codehaus.xfire.fault.XFireFault: Could not read XML stream.. Nested exception is javax.xml.strea

    xfire使用中出现故障: 1. [2014-04-16 14:51:07.564]-[ERROR] org.apache.struts2.dispatcher.Dispatcher Exceptio ...

  3. 解压zip,解决中文乱码

    Project p = new Project();        Expand e = new Expand();        e.setProject(p);        e.setSrc(f ...

  4. Unity Web前端研究

    原地址:http://blog.csdn.net/libeifs/article/details/7200630 开发环境 Window7 Unity3D  3.4.1 MB525defy Andro ...

  5. 【Django】pip 安装和卸载 Django

    1.在dos命令中输入pip进行安装 注意:如果提示('pip' 不是内部或外部命令,也不是可运行的程序或批处理文件.) 那么先将pip添加到环境变量中,pip路径一般在python的安装路径下,例如 ...

  6. JVM-01-转载博客

    JVM——Java虚拟机架构 地址:http://blog.csdn.net/seu_calvin/article/details/51404589 JVM——内存管理和垃圾回收 地址:http:// ...

  7. MySQL-锁研究

    隔离级别研究: http://www.cnblogs.com/JohnABC/p/3521061.html 表级:引擎 MyISAM, 理解为锁住整个表, 锁定期间, 其它进程无法对该表进行写操作, ...

  8. Android软件开发之EditText 详解

    EditText在API中的结构 java.lang.Objectandroid.view.Viewandroid.widget.TextView        android.widget.Edit ...

  9. SQL 横转竖 、竖专横 (转载) 使用Dapper.Contrib 开发.net core程序,兼容多种数据库 C# 读取PDF多级书签 Json.net日期格式化设置 ASPNET 下载共享文件 ASPNET 文件批量下载 递归,循环,尾递归 利用IDisposable接口构建包含非托管资源对象 《.NET 进阶指南》读书笔记2------定义不可改变类型

    SQL 横转竖 .竖专横 (转载)   普通行列转换 问题:假设有张学生成绩表(tb)如下: 姓名 课程 分数 张三 语文 74 张三 数学 83 张三 物理 93 李四 语文 74 李四 数学 84 ...

  10. Android 使用Scroller实现绚丽的ListView左右滑动删除Item效果

    转帖请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/17539199),请尊重他人的辛勤劳动成果,谢谢! 我在上一 ...