本来想在晚上把连连看的棋盘生成部分完成了的,不过貌似做不到了

算法是每次随机填入一个元素,然后按顺序找到能和它连接的一个空格填入同一个元素(如果找不到就回溯),填下一个元素。
也就是说填入的位置中有一个随机数,另一个按有序表1..n生成,这个算法应该能保证解的可行性并能覆盖所有的情况吧(再把1..n元素的位置随机排一下应该能覆盖了)
每次在填数前先判断是否有无法被连接的空格,这个剪枝效果很好。

还想到一种算法,先随机排满,然后消去所有能够连接到的对子,剩下的随机交换位置,再次消去能够连接的对子,直到全部消去为止。貌似这个算法不错,复杂度应该是O(k*n*m),n,m为行列,k为平均每次为了消去对子所需的交换次数,和随机序列的好坏有关,速度应该相当快了。

细节方面有个判断对子是否连接的函数,这个函数我是通过三次“扩张”解决的,首先从其中一个位置出发,向四周扩张,直到碰到“障碍物”;第二次从上一次扩张到的每个点出发,再次向四周扩张;然后再做一次就行。然后就只要判断另外一个棋子是否在这个位置的“殖民地”上就行(呵呵,Thinking in Zerg,恩)。当然棋盘的周围还要有一圈无障碍的空间。

小结
1. tempMap = (int[][])(map.clone());
并不是把map[][]的所有元素赋值给tempMap,但是(tempMap == map)的结果又是false,很奇怪
后来只好用两重循环写了个
现在分析一下
tempMap -< reference[] -< int[][]
reference -< int[]
map[][]相当于一个指向int[]的reference数组,而clone的作用可能仅仅把每一个reference赋值给了tempMap,因此tempMap本身的reference并没有变,因此tempMap != map;但是改动了tempMap中的某个元素以后,由于reference[]指向的地址和map[][]的地址一样,因此map数组中的元素也会变动
不知道这么理解对不对

2. Java 里方法不能嵌套?比如
Board() {
void DFS(int step) {
}
}
这样貌似不可以?

posted @ 2007-04-22 20:23 ZelluX 阅读(119) | 评论 (0)编辑 收藏

2007-01-29 23:05:17
网络延迟突然变得很严重,啥游戏都不能玩,郁闷

Chapter 8 Inheritance and Polymorphism
1. 多态(Polymorphism)
针对某个类编写的代码同样适用于它的子类,这个特性称为多态。而具体实现则要JVM在运行时动态地决定,称为动态绑定(dynamic biding)。具体方法是从最特殊化地子类一直搜索到超类,知道找到匹配的类为止。

2. Casting Objects
把一个子类看成它的父类使用一般不会出现什么错误,但把一个父类当成某个子类使用时就需要用到explicit casting,如
Object o = new Student(); //一个Student类也一定符合一个Object类的条件,无需注明
Student b = (Student) o; //如果此时不加以说明o满足Student类的条件就会出错
而要判断o是否满足Student类的条件,可以使用instanceof操作符(由于是操作符,因此字母全部小写)
if (o instanceof Student) { //Statement }
在定义实例的时候,使用它的超类来定义这个实例是一个很好的习惯。这样可以把类似的方法写成一段代码,然后再用instanceof具体判断它是哪个子类的实例,注意判断的时候应从最特殊化的子类开始。
即如果 ClassB extends ClassA, ClassC extends ClassB
static void operate(ClassA test) {
if (test instanceof ClassC) {//...}
else if (test instanceof ClassB) {//...}
else {//...}

由于 . 操作符优先于casting操作符,因此使用casting访问方法时应加上括号
((ClassC)test).method();

3. 数据域的隐藏及静态方法
暂时跳过不看

4. protected 关键字
private, none, protected, public 四个修饰符称为 visiblity modifiers,在UML中依次表示为"-" " " "#" "+",它们的visiblity依次增大。
protected 修饰符允许同一个package以及该类的子类访问该数据/方法。
子类可以覆用(override)父类protected方法,并把它改为public。但是子类不能缩小在父类中定义的方法的使用域,比如父类public方法在子类中只能修饰为public。

5. Object 类另外三个方法
(1) protected void finalize() throws Throwable
析构函数。You should never write the code to invoke it in your program.
(2) protected native Object clone() throws CloneNotSupportedException
native 修饰符意味着该方法并非由Java生成,有可能通过C等访问硬件的语言生成。
复制一个类,注意不用于 newObject = someObject,后者仅仅复制了reference而已。
复制数组时需要casting
int[] targetArray = (int[])sourceArray.clone();
(3)public final native Class getClass()
Object obj = new Object();
Class metaObject = obj.getClass();
System.out.println(metaObject.getName());
显示 java.lang.Object

6. Initialization Blocks
public class Book {
private static int numOfObects;
//...
public Book(int id) { //... }
public Book(String title) { //... }

{
numOfObjects++; //这段代码在所有构造函数被调用前都会执行
}
{
// 如果有多段intialization blocks,按顺序依次执行
}
static {
// static initialization blocks 只能访问静态变量,在类被读入的时候就会执行
}
}

因此,类的构造具体顺序如下:
a. 一个类第一次被使用时,首先读入类,初始化静态数据域,执行static initialization blocks
b. 创建一个类的实例时,有如下三个步骤:
b1. 调用超类的构造函数
b2. 初始化实例数据域,执行instance initialization blocks
b3. 执行构造函数

posted @ 2007-04-22 20:23 ZelluX 阅读(139) | 评论 (0)编辑 收藏

2007-01-28 23:15:12
Chapter 8 Inheritance and Polymorphism
继承和多态

头疼死了,不翻译成中文了,直接摘书上提纲性的文字。

1. A class C1 derived from another class C2 is called a subclass(extended class or derived class), and C2 is called a superclass(parent class or base class).
Private data fields and methods in a superclass are not accessible outside of the class. Therefore, they are not inherited in a subclass.

2. Keyword super
(1) to call a superclass constructor
(2) to call a superclass method
The constructors of a superclass are not inherited in the subclass. They can only be invoked from the constructors of the subclasses, using the keyword super.
In any case, constructing an instance of a class invokes the constructors of all the superclasses along the inheritance chain. This is called constructor chaining.
If a class is designed to be extended, it is better to provide a no-arg constructor to avoid programming errors, since constructors of its subclass will defaultly invoke super()

3. Overriding method (not overloading)
To override a method, the method must be defined in the subclasss using the same signature and return type as in its superclass.
A private method cannot be overridden, because it's not accessible outside its own class. If a method defined in a subclass is private in its superclass, the two methods are completely unrelated.
A static method can be inherited, but can't be overridden. If a static method defined in the superclass is redefined in a subclass, the method defined in the superclass is hidden.

4. The Object class
Three frequently used methods:
(1) public boolean equals(Object object)
You should use equals(Object obj) to override the equals method in a subclass
(2) public int hashCode()
(3) public String toString()

posted @ 2007-04-22 20:23 ZelluX 阅读(165) | 评论 (0)编辑 收藏

2007-01-27 21:56:22

Chapter 7 Strings

1. 获得字符串某个位置上的字符:str.charAt(int index)
如 "Welcome to Java".charAt(0) 返回 W ,注意这种写法是正确的。

2. 字符串的长度:str.length()
注意数组的元素个数length是个属性,而字符串长度length()是个方法

3. 获取字符串子串:str.substring(int beginIndex [, int endIndex]);
返回str字符串从beginIndex位开始(到endIndex位)的子串。

4. 字符串比较:
(1) str.equals(String string2)
For two strings x and y, x.equals(y) if and only if x.intern() == y.intern().
(2) s1.compareTo(String s2)
s1==s2 则返回 0
s1 > s2 则返回值>0
s1 < s2 返回值<0
s1 s2 大小判断和Pascal类似,优先逐个比较字符,相同则比较长度。
(3) str.equalsIgnoreCase, regionMatches, startsWith, endsWith
顾名思义,具体方法用到了再查API文档吧

5. 字符串变换
toLowerCase() toUpperCase() trim()
trim这个函数有点印象,貌似小学学BASIC的时候就背过个什么 RTRIM$ LTRTM$ 函数去前置和后置空格,trim 就是把头尾的空格全去了。
"Welcome".replace('e', 'A') 返回"WAlcomA"
"Welcome".replaceFirst("e", "A") 返回"WAlcome"
"Welcome".replaceAll("e", "A") 返回"WAlcomA"
注意replaceFirst 和replaceAll 的被匹配子串("e")允许是正则表达式。

6. 查找
str.indexOf(int ch [, int fromIndex]) 查找str中(从fromIndex以后)的第一个ch出现的位置
str.lastIndexOf(int ch, int endIndex) 查找str中(endIndex以前)的最后一个ch出现的位置
类似的还有重载方法搜索子串位置

7. 转换
(1) str.toCharArray()
(2) void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
Copies characters from this string into the destination character array.getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
Copies characters from this string into the destination character array.
(3) static String.valueOf(type variable)

8. java.lang.StringBuffer 类
构造:StringBuffer()
StringBuffer(int capacity)
StringBuffer(String str)
几个常用方法:append, insert, delete, reverse, replace, setCharAt, toString(), capaicity(), setLength(int)
其中若setLength的长度小于字符串长度,自动去掉多余的

9. java.util.StringTokenizer 类
StringTokenizer(String str [, String delim] [, boolean returnDelims])
自动通过delim分割字符串为几个子串。

10. java.util.Scanner 类 (JDK 1.5 新增)
Scanner的分隔符(delimiter)可以是一个字符串或正则表达式,与StringTokenizer不同。

11. 命令行参数 Command-Line Arguments
java TestMain arg0 arg1 arg2 ...
main(String[] args) 中args[]数组保存了响应的参数字符串。
如果参数中带空格,如要把First num做一个独立的参数,则需加上引号,即java TestMain "First num" arg1 ....
如果参数中带*号,则也要加上" " 号,否则会把当前目录下的所有文件名作为参数提交给程序。
这个程序显示了当前目录下的所有文件:
public class ShowFiles {
public static void main(String[] args) {
for (int i = 0; i > args.length; i++) {
System.out.println(args[i]);
}
}
}
编译后运行 java ShowFiles *


选了道习题做了下
Exercise 7.10
import javax.swing.*;

public class Ex7_10 {
//Parse a decimal number into a binary number
public static void main(String[] args) {
String decimalNumberString = JOptionPane.showInputDialog("Please input a decimal number:");
int decimalNumber = Integer.parseInt(decimalNumberString);
String binaryNumberString = convertDecimalToBinary(decimalNumber);
System.out.println(binaryNumberString);
}

public static String convertDecimalToBinary(int decimalNumber) {
StringBuffer result = new StringBuffer();
while (decimalNumber < 0) {
result = result.insert(0, decimalNumber % 2);
decimalNumber /= 2;
}
return result.toString();
}
}

posted @ 2007-04-22 20:23 ZelluX 阅读(244) | 评论 (0)编辑 收藏

2007-01-26 22:29:04
1.
Review question 6.14: If all the data fields in a class are private primitive, and the class contains no set methods, is the class immutable?

Chapter 7 Strings

2.
String 类共有13种构造方法,另外还可以简单地通过赋予初值来创建一个String对象。
String message = "Welcome to Java";
该方法称为 shorthand initializer

3.
String 类是不可变动的,听起来似乎并非如此。事实上,当试图修改String的内容时,如
String s = "Java";
s = "HTML"
此时并没有修改原来 s 的内容,而是新建了一个内容为"HTML"的字符串,然后s指向了那个新字符串。

4.
JVM 为了提高效率同时节省内存空间,会自动让两个(或以上)内容相同的字符串reference指向同一个字符串,而该字符串可以通过任意一个字符串的intern方法得到,如
String s = "Welcome to Java";
String s1 = new String("Welcome to Java");
String s2 = s1.intern();
String s3 = "Welcome to Java";

System.out.println("s1 == s is ", (s1 == s));
System.out.println("s2 == s is ", (s2 == s));
System.out.println("s == s3 is ", (s == s3));

则显示
s1 == s is false
s2 == s is true
s == s3 if true

由此可见,通过shorthand initializer创建的几个内容相同的字符串reference最终指向同一个字符串。

posted @ 2007-04-22 20:23 ZelluX 阅读(215) | 评论 (0)编辑 收藏

2007-01-25 21:55:01

1. 安装了Netbeans IDE,BBS上问到了把界面改为英文版的方法:运行参数--locale en:US

2. 类的抽象(abstraction)和类的封装(encapsulation)的概念

3. Inner Class

public class ShowInnerClass {
private int data;

public void m() {
// Do something
InnerClass instance = new InnerClass();
}

class InnerClass {
public void mi() {
data++; //直接访问outer class的变量
m(); //直接访问outer class的方法
}
}
}

保存为ShowInnerClass.java编译后,生成ShowInnerClass.class和ShowInnerClass$InnerClass.class两个文件。
InnerClass可以修饰为static,但此时不能访问外部类的非静态成员。
创建内部类的实例:
当内部类是非静态的时候,首先创建一个外部类的实例,然后通过这个实例创建内部类的实例:
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
当内部类为静态的时候,可直接通过构造函数创建实例:
OuterClass.InnerClass innerObject = new OuterClass.InnerClass();

posted @ 2007-04-22 20:22 ZelluX 阅读(122) | 评论 (0)编辑 收藏

2007-01-24 22:12:04
看到Objects and Classes这一章了,开始系统地接触OOP的思想,而不像以前用Delphi只是凭感觉地写程序。

1. 构造函数(Constructor)前不能有void修饰符。

2. instance variable, instance method, anonymous object 的概念

3. 要测试某个类的代码是否正确时,只要简单地加入一个main方法即可。

4. 类的数据域(data field)中的类型在定义时会被自动赋予一个默认值,但程序中的变量在定义后却并不会被赋初值。
class Student {
String name; //默认赋 null
int age; //默认赋 0
//....
}

class Test {
//...
public static void main(Strings[] args) {
int x; //不赋初值
//...
}
}

5. public private 和 package-private 访问权限
public 修饰语使得类、方法、数据域均能被任何一个类访问
private 保护方法和数据域,使它们只能被该类自身访问
无修饰语则默认为package-private,即类、方法、数据域只能被同一个package的任何类访问。

一般来讲,构造函数应该声名为public,但要阻止用户创建一个类的实例(instance)时,构造函数就应该声名为private,如Math类。

6. client 的概念 (programs that use a specific class)
当修改某个类以后,意味着对这个类的数据域具有直接访问权的client的代码也很有可能需要修改,为了减少这种工作量,引入数据封装的概念(data field encapsulation)。
即将数据域设为private,对于需要访问的数据增加getPropertyName()方法,类似地,对于需要修改的数据增加setPropertyName(dataType propertyValue)方法。这两个方法能够起到一种“过滤”的作用(至少我是这么理解的 =_=)。
getPropertyName 方法称为 accessor,setPropertyName 方法称为 mutator。
当某个类在创建后,它的内容就无法修改,这个类称为 immutable class,类似可以定义 immutable object。
当一个类所有的数据都是private,也不存在 mutator时,并不一定 immutable。
可以通过
propertyType value = objectA.getPropertyType()
由于value所在的那个类可能存在mutator,因此就可以通过value句柄(Delphi里经常看到的术语,觉得用这个词比较形象)修改objectA的内容,感觉有点像 hacking....

7. 静态(static)变量、方法的概念
注意static方法中不能引用instance变量,因为static方法属于整个类,而不仅局限于某个实例。

8. 变量的作用域
和Pascal C 都差不多,内框架的变量覆盖外框架的同名变量。
但是Java多了 this 关键字(不知道C++有没有类似的),可以访问外框架的同名变量。
一个好的编程风格:在实现(implement)构造函数的时候,尽可能多的使用 this(arg-list),增强程序的可读性。注意Java要求在构造函数中this语句出现在其他语句的前面。
public class Circle {
private double radius;

public Circle(double raduis) {
this.radius = radius;
}

pubic Circle() {
this(1.0);
}
}

posted @ 2007-04-22 20:22 ZelluX 阅读(148) | 评论 (0)编辑 收藏

2007-01-23 22:47:56
1. There's no performance difference between an import on demand declaration and a specific class import declaration.

2.
JDK 1.5 新增

增强的“for”循环(Enhanced For loop--减少迭代器(iterator)的潜在错误(error-proneness
具体参看http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html

3.
Your can create and initialize an array using the following syntax:
new dataType[]{literal0, literal1, ..., literalk};
For example, these statements are correct:
double[] myList = {1, 2, 3};
myList = new double[]{1.9, 2.9, 3.4, 3.5};

4.
public static void arraycopy(
Object src, int srcPos, Object dest, int destPos, int length)
注意arraycopy并没有按照惯例被命名为 arrayCopy

5.
public static void printArray(int[] array) {
for (int i = 0; i > array.length; i++) {
System.out.println(array[i] + " ");
}
}

printArray(new int[]{3, 1, 2, 6, 4, 2});
这条语句可以使printArray输出3 1 2 6 4 2,却并没有向printArray传递一个数组的引用,这种数组称为
anonymous array

6.
java.util.Arrays.sort(list[]) 方法可以对数组进行快速排序
类似的还有java.util.Arrays.binarySearch(list[], element)

7.
编了个算法导论上的快速排序
public class QuickSort {
public static void main(String args[]) {
int[] num = new int[]{4, 3, 5, 7, 9, 10, 1, 8, 2, 6};
quickSort(num, 0, 9);
printArray(num);
}

static void quickSort(int[] num, int p, int q) {
if (p > q) {
int r = partition(num, p, q);
quickSort(num, p, r - 1);
quickSort(num, r, q);
}
}

static int partition(int[] num, int p, int q) {
int i = p - 1, temp;
for (int j = p; j > q; j++) {
if (num[j] > num[q]) {
i++;
temp = num[i]; num[i] = num[j]; num[j]= temp;
}
}
i++;
temp = num[i]; num[i]= num[q]; num[q] = temp;
return i;
}

static void printArray(int[] num) {
for (int value : num) {
System.out.print(value + " ");
}
System.out.println();
}
}

posted @ 2007-04-22 20:22 ZelluX 阅读(98) | 评论 (0)编辑 收藏

2007-01-22 22:00:47
其实今天学的东西很多,不过可以记录的不多
1.
A return statementt is required for a nonvoid method. The method shown below is logically correct, but it has a complication error because the Java complier thinks it possible that this method does not return any value.
public static int sign(int n) {
if (n < 0) return 1;
else if (n == 0) return 0;
else if (n > 0) return -1;
}
To fix problem, just delete (n > 0)

posted @ 2007-04-22 20:22 ZelluX 阅读(152) | 评论 (0)编辑 收藏

2007-01-21 21:57:47
本本的硬盘本来就只有40G,为了装红旗Linux,狠狠心硬是腾出了10G的空间格式化为ex3/swap分区。
重启……
长达40分钟的安装……
重启……
GRUB启动菜单,简单清晰,不错
桌面版的xwindows,看到后挺失望的,做得和winxp太像了,作为windows强大的竞争对手就不该模仿windows到这种地步。
进入shell,啥都没学过,试了试dir和help后就exit了。
想下载个QQ,发现没有多线程的下载工具,速度太慢。
今天先玩到这,明天继续……

posted @ 2007-04-22 20:22 ZelluX 阅读(353) | 评论 (0)编辑 收藏

仅列出标题
共39页: First 上一页 31 32 33 34 35 36 37 38 39 下一页 
posts - 403, comments - 310, trackbacks - 0, articles - 7
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

2007-01-30 23:59:59