
正文
Prototype,创建型模式
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
读书笔记_探索式测试_混合探索式测试 一、测试场景
1.讲述用户故事
2.描述需求
3.演示产品功能
4.演示集成场景
5.描述设置和安装
6.描述警告和出错情况
二、使用基于场景的探索式测试
1.通过场景操作引入变化:操作后得到的新场景称为衍生场景。
- 插入步骤:增加更多数据、使用附加输入、访问新的界面
- 删除步骤
- 替换步骤
- 重复步骤
- 替换数据
- 替换环境
- 替换硬件
- 替换容器:浏览器兼容性
- 替换版本
修改本地设置:cookie、注册表、浏览器设置等
三、通过漫游测试引入变化
1.卖点测试法:模拟用户的工作习惯
2.地标测试法:从场景中选择特定功能的地标,然后随机乱序
3.极限测试法:修改场景使软件达到极限条件
4.深巷测试法:使用最不可能用到或最没用的功能
5.强迫症测试法:重复场景中的每个步骤至少两次
6.通宵测试法:自动化或录制回放等
7.破坏测试法:抢占场景需要的资源
8.收藏家测试法:记录执行场景和衍生场景的每个输出
9.超模测试法:关注UI,确保所有元素各就其位,设计合理可用,强迫数据刷新等
10.配角测试法:临近选项
11.取消测试法:取消按钮、停止功能,针对费时的任务
12.混票测试法:混合场景,找出通用数据或功能
四、实践中的探索式测试
1.许多缺陷不是通过用例找到的
2.GUI测试引入的场景和交互不宜使用自动化测试
3.不管自动还是手动,都需要回归测试
4.出租车测试法:所要到达的功能、对话框或一些其他功能组件,用户通常有大量的路线可选,测试人员必须熟悉各个路径并验证,重点是执行不同的测试路径,相反的,禁区测试法,测试不能使用功能的路径,如无权限,状态非法等
5.多元文化测试法:本地化的基本条件——无硬编码文本,检查修改语言和窗体大小后的重绘
6.收集缺陷: 多路径导致的缺陷
7.取消测试法:取消被测对象前应改变其状态,取消后再次尝试同一场景也很重要
8.缺陷修复率:所有发现的缺陷中,被修复的缺陷所占的百分比
9.开发早期目标:发现设计缺陷、被误用的控件、界面和可用性上的错误
10.开发后期目标:产品功能、数据安全、符合要求、功能特性适用范围、改过的缺陷不再复现
分类: 测试理论标签: 探索式测试, 软件测试设计模式学习之原型模式(Prototype,创建型模式)(5) 在开发中如果我们希望快速的创建一个对象,并且该对象和原有的对象拥有相同的数据,并且和原有的对象不相干,那么我们就可以这样做
第一步:
假如我需要快速创建一个Person对象,然后和某个Person对象相同,如果我们我们只需要Person对象中的属性不引用其他数据信息,可以使用浅拷贝;
如果我们需要Person对象中所有的信息,包括引用的数据信息,可以使用深拷贝
第二步:
在Person类中实现Cloneable接口,重写clone()方法,如果是浅拷贝,只需要直接调用父类的clone()方法就行,如果是深拷贝,需要在clone()方法中重新开辟引用字段所需的内存
代码如下:
Person.java

package com.designpattern.prototype;import java.util.ArrayList;
import java.util.List;public class Person implements Cloneable {
private String name;
private int age; //朋友
private List<String> friends; public List<String> getFriends() {
return friends;
}
public void setFriends(List<String> friends) {
this.friends = friends;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
} /**
* 实现浅拷贝
*/
// protected Person clone(){
//
// try {
// return (Person)super.clone();
// } catch (CloneNotSupportedException e) {
// e.printStackTrace();
// return null;
// }
//
// } /**
* 实现深拷贝
*/
protected Person clone(){ try {
List<String> newfriends = new ArrayList<String>();
for (String friend : this.getFriends()) {
newfriends.add(friend);
}
Person person = (Person)super.clone();
person.setFriends(newfriends);
return person;
} catch (CloneNotSupportedException e) {
e.printStackTrace();
return null;
} }
}

MainClass.java

package com.designpattern.prototype;import java.util.ArrayList;
import java.util.List;public class MainClass { public static void main(String[] args) {
//假如我需要快速创建一个Person对象,然后和某个Person对象相同,如果我们我们只需要Person对象中的属性不引用其他数据信息,可以使用浅拷贝
//如果我们需要Person对象中所有的信息,包括引用的数据信息,可以使用深拷贝
/*
* 浅拷贝
*/
// Person person = new Person();
// person.setName("jack");
// person.setAge(18);
//
// Person person2 = person.clone();//相当于重新开辟一块内存空间,和person互不干扰
// person2.setName("jack1");
//
// System.out.println(person.getName());
// System.out.println(person2.getName()); Person person = new Person();
person.setName("jack");
person.setAge(18);
List<String> friends = new ArrayList<String>();
friends.add("p1");
friends.add("p2");
person.setFriends(friends); Person person2 = person.clone();
person2.setName("jack1");
friends.add("p3");//person 引用friends,friends改变,person的friends就会改变,如果用浅拷贝,person2也会跟着改变
//如果用深拷贝,person2不会跟着改变,因为person2的friends和person的friends没有指向同一块内存 System.out.println(person.getName());
System.out.println(person2.getName());
System.out.println(person.getFriends());
System.out.println(person2.getFriends());
}}

一、什么是原型模式
Prototype模式是一种对象创建型模式,它采取复制原型对象的方法来创建对象的实例。使用Prototype模式创建的实例,具有与原型一样的数据。
二、原型模式的特点
1. 由原型对象自身创建目标对象。也就是说,对象创建这一动作发自原型对象本身。
2.目标对象是原型对象的一个克隆。也就是说,通过Prototype模式创建的对象,不仅仅与原型对象具有相同的结构,还与原型对象具有相同的值。
3.根据对象克隆深度层次的不同,有浅度克隆与深度克隆。
三、原型模式应用场景
- 在创建对象的时候,我们不只是希望被创建的对象继承其基类的基本结构,还希望继承原型对象的数据。
- 希望对目标对象的修改不影响既有的原型对象(深度克隆的时候可以完全互不影响)。
- 隐藏克隆操作的细节。很多时候,对对象本身的克隆需要涉及到类本身的数据细节。
分类: 设计模式[C#]想说一说嵌套数组 今天早上,随感而发,随便写了点东西。结果下午的时候看了看评论,吓我一跳。估计是不是写代码的人看散文看得太少了,还是因为现在的人读的书太少了,似乎有有些大惊小怪。
关于Y美女,我声明一下,尽管她很脱俗,不过情缘自有分定,我心里所爱的并不是Y美女,而是另外一位女孩子,境界也跟Y差不多。这样的女孩其实我遇过好几个个,要说喜欢,这几位林黛玉式的女孩我都比较喜欢,但是,爱和喜欢是两回事。
看来,以后这些高级文章还是写到新浪博客好了,这个博客专用来写编程相关的。
------------------------------------------
闲言少叙,说正经话。本文就说一说嵌套数组,估计又有人不满了,你也许会说:“这玩意儿平时都不曾用,说来干吗?” 如果只说平时用的,平时不用的就不说了,那就不是我了。我这个人很有趣,专挑别人不以为然的事情来说。
先看看下面的代码,看了之后不要紧张。

1 int[][][] arrs = new int[3][][];
2 arrs[0] = new int[2][]
3 {
4 new int[3] { 1, 2, 3 },
5 new int[1] { 4 }
6 };
7 arrs[1] = new int[][]
8 {
9 new int[] { 5, 6 },
10 new int[] { 7, 8, 9, 10 },
11 new int[] { 11, 12, 13 }
12 };
13 arrs[2] = new int[][]
14 {
15 new int[] { 14, 15, 16, 17, 18 }
16 };

这就是所谓的嵌套数组,其实也没什么的,要理解的话也不那么复杂。无非就是数组里面套了数组。也就是说,一个数组里面(第一层),每个元素又是一个数组(第二层)……
就拿上面的int数组来说,里面套了三层,一般来说,最里面一层数组的元素就不是数组了,就应该是单个int数值了。所以,嵌套数组的最里层都是单个值作为元素的,不然这数组可就要无限地套下去了。
要知道以上代码中的数组是啥结构也不用画图,我们只要调试运行,在给数组赋值后停下来,从“局部变量”窗口中我们可以查看到数组的结构。如下图:

这样看,应该可以理解的。
现在,就回到代码中,我们看要如何声明,其实说起来也不难,和声明一般的数组一样,比如int[][]就是两层数组。但要注意的问题在于实例化的时候。
在实例化的时候,最外面一层数组可以指定大小,但里面套的数组则不可;里面的数组在下一层数组实例化时才可以指定大小。一句话总结:new哪一层数组就可以明确指定哪一层数组的大小。
例如,下面的new法是会报错的。
byte[][][] arr= new byte[3][5][2];
要这样new才不会报错。
byte[][][] arr= new byte[3][][];
因为这样声明new了最外层的byte数组,所以只能给这一层指定维数。我们继续,把整个数组new完,并赋值。

byte[][][] arr = new byte[3][][] //3个元素
{
/* [0] */new byte[2][] //2个元素
{
/* [0] */new byte[] { 0x20, 0x01, 0xFE },
/* [1] */new byte[] { 0xD2, 0xC6, 0x01, 0x22 }
},
/* [1] */new byte[1][] //1个元素
{
/* [0] */new byte[] { 0x33, 0x4A }
},
/* [2] */new byte[3][] //3个元素
{
/* [0] */new byte[] { 0x2e, 0x40 },
/* [1] */new byte[] { 0x52, 0xb2, 0x06 },
/* [2] */new byte[2] { 0x11, 0x21 }
}
};

怎么样?有何感想?带了注释应该容易看一些。由于越往里面数组的层数就递减,所以每进一层,就少一对中括号,第一层三对中括号,第二层两对中括号,第三层一对中括号,然后里面就是真正的单个byte值。
在写代码的时候,我们应该使用上面例子的排版方式,这样层次就分明,写的时候不容易写错,就和代码中大括号的层次一样,恰好,用来包含数组元素的也是一对大括号。有层次地缩进,就不容易写错。
下面我们来个更猛的。

string[][][][][][][] strArr = new string[][][][][][][]
{
new string[][][][][][]
{
new string[][][][][]
{
new string[][][][]
{
new string[][][]
{
new string[][]
{
new string[] { "ai", "gooo", "put" },
new string[] { "san", "de" }
},
new string[][]
{
new string[] { "ki", "chd" }
}
},
new string[][][]
{
new string[][]
{
new string[] { "ga" },
new string[] { "x", "y", "w", "h" },
new string[] { "cc", "li" }
},
new string[][]
{
new string[] { "su" },
new string[] { "dou", "xx", "f" }
},
new string[][]
{
new string[] { "z", "xoo", "ui" },
new string[] { "gn", "sun", "tttt", "fan" },
new string[] { "yin", "ci", "zh" },
new string[] { "oo", "yi" }
}
}
},
new string[][][][]
{
new string[][][]
{
new string[][]
{
new string[] { "da" }
},
new string[][]
{
new string[] { "00" }
}
},
new string[][][]
{
new string[][]
{
new string[] { "xi", "ix" },
new string[] { "ch" }
}
}
}
},
new string[][][][][]
{
new string[][][][]
{
new string[][][]
{
new string[][]
{
new string[] { "zz", "ee" },
new string[] { "teng" }
}
},
new string[][][]
{
new string[][]
{
new string[] { "shi", "me", "ea" }
},
new string[][]
{
new string[] { "ut" }
}
}
}
}
},
new string[][][][][][]
{
new string[][][][][]
{
new string[][][][]
{
new string[][][]
{
new string[][]
{
new string[] { "dd", "eaood" }
},
new string[][]
{
new string[] { "ss", "48" },
new string[] { "ha", "tie" }
}
}
},
new string[][][][]
{
new string[][][]
{
new string[][]
{
new string[] { "tian" }
}
}
},
new string[][][][]
{
new string[][][]
{
new string[][]
{
new string[] { "lan" }
},
new string[][]
{
new string[] { "y", "zu" }
}
}
}
},
new string[][][][][]
{
new string[][][][]
{
new string[][][]
{
new string[][]
{
new string[] { "hao", "zen", "oo", "du" },
new string[] { "xi", "iow", "zzzzz" },
new string[] { "hie", "zz", "8e" }
},
new string[][]
{
new string[] { "fo" }
}
}
}
}
}
};

怎么样?敢动手试试吗?不要看着害怕,其实很简单,我一口气就写下来了。还是那些规律。先看有多少对中括号,每进一层就减一对,一直减到只有一对中括号时,就是独立的数组元素了。另一点就是把大括号做好配对。
我们也可以像盖房子一样,先把大致的框架弄好,然后再弄里面的各个房间,最后再从细节上装潢一下。
比如,先这样。
int[][][][] intArr = new int[][][][]
{ };
然后这样。

int[][][][] intArr = new int[][][][]
{
new int[][][]
{ },
new int[][][]
{ },
new int[][][]
{ }
}

接着这样。

int[][][][] intArr = new int[][][][]
{
new int[][][]
{
new int[][]
{ }
},
new int[][][]
{
new int[][]
{ },
new int[][]
{ },
new int[][]
{ }
},
new int[][][]
{
new int[][]
{ }
}
}

最后填满元素。

int[][][][] intArr = new int[][][][]
{
new int[][][]
{
new int[][]
{
new int[] { 2 }
}
},
new int[][][]
{
new int[][]
{
new int[] { 28, 31 }
},
new int[][]
{
new int[] { 98 }
},
new int[][]
{
new int[] { 54 }
}
},
new int[][][]
{
new int[][]
{
new int[] { 8, 17, 36 },
new int[] { 16, 73 }
}
}
};

这个过程是很考验人的意志的,同时也可以检测一下你的心是否能够平静下来。只要你的心能静下来,哪怕是一连写上100层的嵌套数组也没问题的。
当然,这仅仅是一种练习,真正做开发的话,极少这样做。平时练习编程的时候不妨试试,这不仅在锻炼你写代码的能力,也是锻炼你的心理素质。
编程的时候,我很喜欢一边听无损音乐一边写代码。当然不是那种听着就心烦意乱的音乐,是那种很恬静很优雅,并且还夹带着大自然的声音的音乐,如泉水声,风声,鸟鸣声等。
分类: 个人文章
一、测试场景
1.讲述用户故事
2.描述需求
3.演示产品功能
4.演示集成场景
5.描述设置和安装
6.描述警告和出错情况
二、使用基于场景的探索式测试
1.通过场景操作引入变化:操作后得到的新场景称为衍生场景。
- 插入步骤:增加更多数据、使用附加输入、访问新的界面
- 删除步骤
- 替换步骤
- 重复步骤
- 替换数据
- 替换环境
- 替换硬件
- 替换容器:浏览器兼容性
- 替换版本
修改本地设置:cookie、注册表、浏览器设置等
三、通过漫游测试引入变化
1.卖点测试法:模拟用户的工作习惯
2.地标测试法:从场景中选择特定功能的地标,然后随机乱序
3.极限测试法:修改场景使软件达到极限条件
4.深巷测试法:使用最不可能用到或最没用的功能
5.强迫症测试法:重复场景中的每个步骤至少两次
6.通宵测试法:自动化或录制回放等
7.破坏测试法:抢占场景需要的资源
8.收藏家测试法:记录执行场景和衍生场景的每个输出
9.超模测试法:关注UI,确保所有元素各就其位,设计合理可用,强迫数据刷新等
10.配角测试法:临近选项
11.取消测试法:取消按钮、停止功能,针对费时的任务
12.混票测试法:混合场景,找出通用数据或功能
四、实践中的探索式测试
1.许多缺陷不是通过用例找到的
2.GUI测试引入的场景和交互不宜使用自动化测试
3.不管自动还是手动,都需要回归测试
4.出租车测试法:所要到达的功能、对话框或一些其他功能组件,用户通常有大量的路线可选,测试人员必须熟悉各个路径并验证,重点是执行不同的测试路径,相反的,禁区测试法,测试不能使用功能的路径,如无权限,状态非法等
5.多元文化测试法:本地化的基本条件——无硬编码文本,检查修改语言和窗体大小后的重绘
6.收集缺陷: 多路径导致的缺陷
7.取消测试法:取消被测对象前应改变其状态,取消后再次尝试同一场景也很重要
8.缺陷修复率:所有发现的缺陷中,被修复的缺陷所占的百分比
9.开发早期目标:发现设计缺陷、被误用的控件、界面和可用性上的错误
10.开发后期目标:产品功能、数据安全、符合要求、功能特性适用范围、改过的缺陷不再复现
分类: 测试理论
标签: 探索式测试, 软件测试
在开发中如果我们希望快速的创建一个对象,并且该对象和原有的对象拥有相同的数据,并且和原有的对象不相干,那么我们就可以这样做
第一步:
假如我需要快速创建一个Person对象,然后和某个Person对象相同,如果我们我们只需要Person对象中的属性不引用其他数据信息,可以使用浅拷贝;
如果我们需要Person对象中所有的信息,包括引用的数据信息,可以使用深拷贝
第二步:
在Person类中实现Cloneable接口,重写clone()方法,如果是浅拷贝,只需要直接调用父类的clone()方法就行,如果是深拷贝,需要在clone()方法中重新开辟引用字段所需的内存
代码如下:
Person.java

package com.designpattern.prototype;import java.util.ArrayList;
import java.util.List;public class Person implements Cloneable {
private String name;
private int age; //朋友
private List<String> friends; public List<String> getFriends() {
return friends;
}
public void setFriends(List<String> friends) {
this.friends = friends;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
} /**
* 实现浅拷贝
*/
// protected Person clone(){
//
// try {
// return (Person)super.clone();
// } catch (CloneNotSupportedException e) {
// e.printStackTrace();
// return null;
// }
//
// } /**
* 实现深拷贝
*/
protected Person clone(){ try {
List<String> newfriends = new ArrayList<String>();
for (String friend : this.getFriends()) {
newfriends.add(friend);
}
Person person = (Person)super.clone();
person.setFriends(newfriends);
return person;
} catch (CloneNotSupportedException e) {
e.printStackTrace();
return null;
} }
}

MainClass.java

package com.designpattern.prototype;import java.util.ArrayList;
import java.util.List;public class MainClass { public static void main(String[] args) {
//假如我需要快速创建一个Person对象,然后和某个Person对象相同,如果我们我们只需要Person对象中的属性不引用其他数据信息,可以使用浅拷贝
//如果我们需要Person对象中所有的信息,包括引用的数据信息,可以使用深拷贝
/*
* 浅拷贝
*/
// Person person = new Person();
// person.setName("jack");
// person.setAge(18);
//
// Person person2 = person.clone();//相当于重新开辟一块内存空间,和person互不干扰
// person2.setName("jack1");
//
// System.out.println(person.getName());
// System.out.println(person2.getName()); Person person = new Person();
person.setName("jack");
person.setAge(18);
List<String> friends = new ArrayList<String>();
friends.add("p1");
friends.add("p2");
person.setFriends(friends); Person person2 = person.clone();
person2.setName("jack1");
friends.add("p3");//person 引用friends,friends改变,person的friends就会改变,如果用浅拷贝,person2也会跟着改变
//如果用深拷贝,person2不会跟着改变,因为person2的friends和person的friends没有指向同一块内存 System.out.println(person.getName());
System.out.println(person2.getName());
System.out.println(person.getFriends());
System.out.println(person2.getFriends());
}}

一、什么是原型模式
Prototype模式是一种对象创建型模式,它采取复制原型对象的方法来创建对象的实例。使用Prototype模式创建的实例,具有与原型一样的数据。
二、原型模式的特点
1. 由原型对象自身创建目标对象。也就是说,对象创建这一动作发自原型对象本身。
2.目标对象是原型对象的一个克隆。也就是说,通过Prototype模式创建的对象,不仅仅与原型对象具有相同的结构,还与原型对象具有相同的值。
3.根据对象克隆深度层次的不同,有浅度克隆与深度克隆。
三、原型模式应用场景
- 在创建对象的时候,我们不只是希望被创建的对象继承其基类的基本结构,还希望继承原型对象的数据。
- 希望对目标对象的修改不影响既有的原型对象(深度克隆的时候可以完全互不影响)。
- 隐藏克隆操作的细节。很多时候,对对象本身的克隆需要涉及到类本身的数据细节。
分类: 设计模式
[C#]想说一说嵌套数组 今天早上,随感而发,随便写了点东西。结果下午的时候看了看评论,吓我一跳。估计是不是写代码的人看散文看得太少了,还是因为现在的人读的书太少了,似乎有有些大惊小怪。
关于Y美女,我声明一下,尽管她很脱俗,不过情缘自有分定,我心里所爱的并不是Y美女,而是另外一位女孩子,境界也跟Y差不多。这样的女孩其实我遇过好几个个,要说喜欢,这几位林黛玉式的女孩我都比较喜欢,但是,爱和喜欢是两回事。
看来,以后这些高级文章还是写到新浪博客好了,这个博客专用来写编程相关的。
------------------------------------------
闲言少叙,说正经话。本文就说一说嵌套数组,估计又有人不满了,你也许会说:“这玩意儿平时都不曾用,说来干吗?” 如果只说平时用的,平时不用的就不说了,那就不是我了。我这个人很有趣,专挑别人不以为然的事情来说。
先看看下面的代码,看了之后不要紧张。

1 int[][][] arrs = new int[3][][];
2 arrs[0] = new int[2][]
3 {
4 new int[3] { 1, 2, 3 },
5 new int[1] { 4 }
6 };
7 arrs[1] = new int[][]
8 {
9 new int[] { 5, 6 },
10 new int[] { 7, 8, 9, 10 },
11 new int[] { 11, 12, 13 }
12 };
13 arrs[2] = new int[][]
14 {
15 new int[] { 14, 15, 16, 17, 18 }
16 };

这就是所谓的嵌套数组,其实也没什么的,要理解的话也不那么复杂。无非就是数组里面套了数组。也就是说,一个数组里面(第一层),每个元素又是一个数组(第二层)……
就拿上面的int数组来说,里面套了三层,一般来说,最里面一层数组的元素就不是数组了,就应该是单个int数值了。所以,嵌套数组的最里层都是单个值作为元素的,不然这数组可就要无限地套下去了。
要知道以上代码中的数组是啥结构也不用画图,我们只要调试运行,在给数组赋值后停下来,从“局部变量”窗口中我们可以查看到数组的结构。如下图:

这样看,应该可以理解的。
现在,就回到代码中,我们看要如何声明,其实说起来也不难,和声明一般的数组一样,比如int[][]就是两层数组。但要注意的问题在于实例化的时候。
在实例化的时候,最外面一层数组可以指定大小,但里面套的数组则不可;里面的数组在下一层数组实例化时才可以指定大小。一句话总结:new哪一层数组就可以明确指定哪一层数组的大小。
例如,下面的new法是会报错的。
byte[][][] arr= new byte[3][5][2];
要这样new才不会报错。
byte[][][] arr= new byte[3][][];
因为这样声明new了最外层的byte数组,所以只能给这一层指定维数。我们继续,把整个数组new完,并赋值。

byte[][][] arr = new byte[3][][] //3个元素
{
/* [0] */new byte[2][] //2个元素
{
/* [0] */new byte[] { 0x20, 0x01, 0xFE },
/* [1] */new byte[] { 0xD2, 0xC6, 0x01, 0x22 }
},
/* [1] */new byte[1][] //1个元素
{
/* [0] */new byte[] { 0x33, 0x4A }
},
/* [2] */new byte[3][] //3个元素
{
/* [0] */new byte[] { 0x2e, 0x40 },
/* [1] */new byte[] { 0x52, 0xb2, 0x06 },
/* [2] */new byte[2] { 0x11, 0x21 }
}
};

怎么样?有何感想?带了注释应该容易看一些。由于越往里面数组的层数就递减,所以每进一层,就少一对中括号,第一层三对中括号,第二层两对中括号,第三层一对中括号,然后里面就是真正的单个byte值。
在写代码的时候,我们应该使用上面例子的排版方式,这样层次就分明,写的时候不容易写错,就和代码中大括号的层次一样,恰好,用来包含数组元素的也是一对大括号。有层次地缩进,就不容易写错。
下面我们来个更猛的。

string[][][][][][][] strArr = new string[][][][][][][]
{
new string[][][][][][]
{
new string[][][][][]
{
new string[][][][]
{
new string[][][]
{
new string[][]
{
new string[] { "ai", "gooo", "put" },
new string[] { "san", "de" }
},
new string[][]
{
new string[] { "ki", "chd" }
}
},
new string[][][]
{
new string[][]
{
new string[] { "ga" },
new string[] { "x", "y", "w", "h" },
new string[] { "cc", "li" }
},
new string[][]
{
new string[] { "su" },
new string[] { "dou", "xx", "f" }
},
new string[][]
{
new string[] { "z", "xoo", "ui" },
new string[] { "gn", "sun", "tttt", "fan" },
new string[] { "yin", "ci", "zh" },
new string[] { "oo", "yi" }
}
}
},
new string[][][][]
{
new string[][][]
{
new string[][]
{
new string[] { "da" }
},
new string[][]
{
new string[] { "00" }
}
},
new string[][][]
{
new string[][]
{
new string[] { "xi", "ix" },
new string[] { "ch" }
}
}
}
},
new string[][][][][]
{
new string[][][][]
{
new string[][][]
{
new string[][]
{
new string[] { "zz", "ee" },
new string[] { "teng" }
}
},
new string[][][]
{
new string[][]
{
new string[] { "shi", "me", "ea" }
},
new string[][]
{
new string[] { "ut" }
}
}
}
}
},
new string[][][][][][]
{
new string[][][][][]
{
new string[][][][]
{
new string[][][]
{
new string[][]
{
new string[] { "dd", "eaood" }
},
new string[][]
{
new string[] { "ss", "48" },
new string[] { "ha", "tie" }
}
}
},
new string[][][][]
{
new string[][][]
{
new string[][]
{
new string[] { "tian" }
}
}
},
new string[][][][]
{
new string[][][]
{
new string[][]
{
new string[] { "lan" }
},
new string[][]
{
new string[] { "y", "zu" }
}
}
}
},
new string[][][][][]
{
new string[][][][]
{
new string[][][]
{
new string[][]
{
new string[] { "hao", "zen", "oo", "du" },
new string[] { "xi", "iow", "zzzzz" },
new string[] { "hie", "zz", "8e" }
},
new string[][]
{
new string[] { "fo" }
}
}
}
}
}
};

怎么样?敢动手试试吗?不要看着害怕,其实很简单,我一口气就写下来了。还是那些规律。先看有多少对中括号,每进一层就减一对,一直减到只有一对中括号时,就是独立的数组元素了。另一点就是把大括号做好配对。
我们也可以像盖房子一样,先把大致的框架弄好,然后再弄里面的各个房间,最后再从细节上装潢一下。
比如,先这样。
int[][][][] intArr = new int[][][][]
{ };
然后这样。

int[][][][] intArr = new int[][][][]
{
new int[][][]
{ },
new int[][][]
{ },
new int[][][]
{ }
}

接着这样。

int[][][][] intArr = new int[][][][]
{
new int[][][]
{
new int[][]
{ }
},
new int[][][]
{
new int[][]
{ },
new int[][]
{ },
new int[][]
{ }
},
new int[][][]
{
new int[][]
{ }
}
}

最后填满元素。

int[][][][] intArr = new int[][][][]
{
new int[][][]
{
new int[][]
{
new int[] { 2 }
}
},
new int[][][]
{
new int[][]
{
new int[] { 28, 31 }
},
new int[][]
{
new int[] { 98 }
},
new int[][]
{
new int[] { 54 }
}
},
new int[][][]
{
new int[][]
{
new int[] { 8, 17, 36 },
new int[] { 16, 73 }
}
}
};

这个过程是很考验人的意志的,同时也可以检测一下你的心是否能够平静下来。只要你的心能静下来,哪怕是一连写上100层的嵌套数组也没问题的。
当然,这仅仅是一种练习,真正做开发的话,极少这样做。平时练习编程的时候不妨试试,这不仅在锻炼你写代码的能力,也是锻炼你的心理素质。
编程的时候,我很喜欢一边听无损音乐一边写代码。当然不是那种听着就心烦意乱的音乐,是那种很恬静很优雅,并且还夹带着大自然的声音的音乐,如泉水声,风声,鸟鸣声等。
分类: 个人文章
今天早上,随感而发,随便写了点东西。结果下午的时候看了看评论,吓我一跳。估计是不是写代码的人看散文看得太少了,还是因为现在的人读的书太少了,似乎有有些大惊小怪。
关于Y美女,我声明一下,尽管她很脱俗,不过情缘自有分定,我心里所爱的并不是Y美女,而是另外一位女孩子,境界也跟Y差不多。这样的女孩其实我遇过好几个个,要说喜欢,这几位林黛玉式的女孩我都比较喜欢,但是,爱和喜欢是两回事。
看来,以后这些高级文章还是写到新浪博客好了,这个博客专用来写编程相关的。
------------------------------------------
闲言少叙,说正经话。本文就说一说嵌套数组,估计又有人不满了,你也许会说:“这玩意儿平时都不曾用,说来干吗?” 如果只说平时用的,平时不用的就不说了,那就不是我了。我这个人很有趣,专挑别人不以为然的事情来说。
先看看下面的代码,看了之后不要紧张。

1 int[][][] arrs = new int[3][][];
2 arrs[0] = new int[2][]
3 {
4 new int[3] { 1, 2, 3 },
5 new int[1] { 4 }
6 };
7 arrs[1] = new int[][]
8 {
9 new int[] { 5, 6 },
10 new int[] { 7, 8, 9, 10 },
11 new int[] { 11, 12, 13 }
12 };
13 arrs[2] = new int[][]
14 {
15 new int[] { 14, 15, 16, 17, 18 }
16 };

这就是所谓的嵌套数组,其实也没什么的,要理解的话也不那么复杂。无非就是数组里面套了数组。也就是说,一个数组里面(第一层),每个元素又是一个数组(第二层)……
就拿上面的int数组来说,里面套了三层,一般来说,最里面一层数组的元素就不是数组了,就应该是单个int数值了。所以,嵌套数组的最里层都是单个值作为元素的,不然这数组可就要无限地套下去了。
要知道以上代码中的数组是啥结构也不用画图,我们只要调试运行,在给数组赋值后停下来,从“局部变量”窗口中我们可以查看到数组的结构。如下图:

这样看,应该可以理解的。
现在,就回到代码中,我们看要如何声明,其实说起来也不难,和声明一般的数组一样,比如int[][]就是两层数组。但要注意的问题在于实例化的时候。
在实例化的时候,最外面一层数组可以指定大小,但里面套的数组则不可;里面的数组在下一层数组实例化时才可以指定大小。一句话总结:new哪一层数组就可以明确指定哪一层数组的大小。
例如,下面的new法是会报错的。
byte[][][] arr= new byte[3][5][2];
要这样new才不会报错。
byte[][][] arr= new byte[3][][];
因为这样声明new了最外层的byte数组,所以只能给这一层指定维数。我们继续,把整个数组new完,并赋值。

byte[][][] arr = new byte[3][][] //3个元素
{
/* [0] */new byte[2][] //2个元素
{
/* [0] */new byte[] { 0x20, 0x01, 0xFE },
/* [1] */new byte[] { 0xD2, 0xC6, 0x01, 0x22 }
},
/* [1] */new byte[1][] //1个元素
{
/* [0] */new byte[] { 0x33, 0x4A }
},
/* [2] */new byte[3][] //3个元素
{
/* [0] */new byte[] { 0x2e, 0x40 },
/* [1] */new byte[] { 0x52, 0xb2, 0x06 },
/* [2] */new byte[2] { 0x11, 0x21 }
}
};

怎么样?有何感想?带了注释应该容易看一些。由于越往里面数组的层数就递减,所以每进一层,就少一对中括号,第一层三对中括号,第二层两对中括号,第三层一对中括号,然后里面就是真正的单个byte值。
在写代码的时候,我们应该使用上面例子的排版方式,这样层次就分明,写的时候不容易写错,就和代码中大括号的层次一样,恰好,用来包含数组元素的也是一对大括号。有层次地缩进,就不容易写错。
下面我们来个更猛的。

string[][][][][][][] strArr = new string[][][][][][][]
{
new string[][][][][][]
{
new string[][][][][]
{
new string[][][][]
{
new string[][][]
{
new string[][]
{
new string[] { "ai", "gooo", "put" },
new string[] { "san", "de" }
},
new string[][]
{
new string[] { "ki", "chd" }
}
},
new string[][][]
{
new string[][]
{
new string[] { "ga" },
new string[] { "x", "y", "w", "h" },
new string[] { "cc", "li" }
},
new string[][]
{
new string[] { "su" },
new string[] { "dou", "xx", "f" }
},
new string[][]
{
new string[] { "z", "xoo", "ui" },
new string[] { "gn", "sun", "tttt", "fan" },
new string[] { "yin", "ci", "zh" },
new string[] { "oo", "yi" }
}
}
},
new string[][][][]
{
new string[][][]
{
new string[][]
{
new string[] { "da" }
},
new string[][]
{
new string[] { "00" }
}
},
new string[][][]
{
new string[][]
{
new string[] { "xi", "ix" },
new string[] { "ch" }
}
}
}
},
new string[][][][][]
{
new string[][][][]
{
new string[][][]
{
new string[][]
{
new string[] { "zz", "ee" },
new string[] { "teng" }
}
},
new string[][][]
{
new string[][]
{
new string[] { "shi", "me", "ea" }
},
new string[][]
{
new string[] { "ut" }
}
}
}
}
},
new string[][][][][][]
{
new string[][][][][]
{
new string[][][][]
{
new string[][][]
{
new string[][]
{
new string[] { "dd", "eaood" }
},
new string[][]
{
new string[] { "ss", "48" },
new string[] { "ha", "tie" }
}
}
},
new string[][][][]
{
new string[][][]
{
new string[][]
{
new string[] { "tian" }
}
}
},
new string[][][][]
{
new string[][][]
{
new string[][]
{
new string[] { "lan" }
},
new string[][]
{
new string[] { "y", "zu" }
}
}
}
},
new string[][][][][]
{
new string[][][][]
{
new string[][][]
{
new string[][]
{
new string[] { "hao", "zen", "oo", "du" },
new string[] { "xi", "iow", "zzzzz" },
new string[] { "hie", "zz", "8e" }
},
new string[][]
{
new string[] { "fo" }
}
}
}
}
}
};

怎么样?敢动手试试吗?不要看着害怕,其实很简单,我一口气就写下来了。还是那些规律。先看有多少对中括号,每进一层就减一对,一直减到只有一对中括号时,就是独立的数组元素了。另一点就是把大括号做好配对。
我们也可以像盖房子一样,先把大致的框架弄好,然后再弄里面的各个房间,最后再从细节上装潢一下。
比如,先这样。
int[][][][] intArr = new int[][][][]
{ };
然后这样。

int[][][][] intArr = new int[][][][]
{
new int[][][]
{ },
new int[][][]
{ },
new int[][][]
{ }
}

接着这样。

int[][][][] intArr = new int[][][][]
{
new int[][][]
{
new int[][]
{ }
},
new int[][][]
{
new int[][]
{ },
new int[][]
{ },
new int[][]
{ }
},
new int[][][]
{
new int[][]
{ }
}
}

最后填满元素。

int[][][][] intArr = new int[][][][]
{
new int[][][]
{
new int[][]
{
new int[] { 2 }
}
},
new int[][][]
{
new int[][]
{
new int[] { 28, 31 }
},
new int[][]
{
new int[] { 98 }
},
new int[][]
{
new int[] { 54 }
}
},
new int[][][]
{
new int[][]
{
new int[] { 8, 17, 36 },
new int[] { 16, 73 }
}
}
};

这个过程是很考验人的意志的,同时也可以检测一下你的心是否能够平静下来。只要你的心能静下来,哪怕是一连写上100层的嵌套数组也没问题的。
当然,这仅仅是一种练习,真正做开发的话,极少这样做。平时练习编程的时候不妨试试,这不仅在锻炼你写代码的能力,也是锻炼你的心理素质。
编程的时候,我很喜欢一边听无损音乐一边写代码。当然不是那种听着就心烦意乱的音乐,是那种很恬静很优雅,并且还夹带着大自然的声音的音乐,如泉水声,风声,鸟鸣声等。
分类: 个人文章







