您的位置:首页 > 编程语言 > Java开发

我的Thinking in Java学习笔记(八)

2004-08-30 02:31 941 查看
Array的初始化
array其实就是一组对象或者一组基本数据类型的数据,每一个array中的数据必须是同一种类型,并且以一个标示符封装在一起
例如 int[] i; 或者 int i[];
现在你这样声明一个array,但是array里面储存的其实是一组句柄,而非具体的数据对象,你必须要撰写初始化语句才能 让array变的有意义,初始化的方式同样有2种
int i[] = new int[3];或者是int i[] = { 1,2,3 };
需要注意的是第二种初始化方法,只能出现在array生成的时候。java允许你将某个array指派给另一个array,你所传递的其实只是句柄罢了,我们看个例子
public class arrays
{
public static void main(String args[])
{
int[] i = {1,2,3}; //声明一个数组,并且初始化
int[] ii; //只是声明一个数组
ii = i; //句柄的传递,并未把数组内的值传递,换句话说就是此时ii和i的句柄已经同时指向同一个内存地址
for(int j = 0; j<ii.length; j++)
{
ii[j]++; //我们只改变ii的值
}
for(int j = 0; j<i.length; j++)
{
System.out.println(" i[ "+j+" ]= "+i[j]); //显示i的值
}
}
}
我们回发现,i的值随着ii的改变而改变了,这个现象我们在赋值运算的时候已经讲过了。我们需要知道,任何一个array都有一个特性,通过他你可以知道array内的元素的个数,那就是length,因为java是从0开始检索数组的,也就是说该数组的最大索引值是length-1。如果你的索引值大于/小于实际容纳元素值,那么就会在程序的执行期得到数组越界例外,因为java会对数组进行边界检查,所以array的效率不是很高。如果你并不知道你所需要能能容纳多少元素的array,但是你却需要这么一个array,你可以采用new的方式来生成
public class arrays
{
public static void main(String args[])
{
int i[] = new int[3];
for(int j = 0; j<i.length; j++)
{
System.out.println(" i[ "+j+" ]= "+i[j]); //显示3个0
}
}
}
基本类型的array会自动初始化为0(与基本数据类型的成员数据的初始化值是一样的),但是你要是产生对象数组的话,你就必须指定初始化值
public class arrays
{
public static void go(Object[] o) //可以接受未定类型和个数的数组
{
for(int i=0;i<o.length;i++)
System.out.println(o[i]);
}
public static void show()
{
String[] s = new String[3]; //声明可以容纳3个元素对象数组,但是他们其实是3个句柄组成的句柄数组,并没有指向任何具体的内存块
s[0]="one"; //分别给每个句柄连接实际的对象,这才完成初始化动作,如果要是没有这一步,就会显示的是null
s[1]="two";
s[2]="three";
for(int j = 0; j<s.length; j++)
{
System.out.println(" i[ "+j+" ]= "+s[j]);
}
}
public static void main(String args[])
{
arrays.go(new Object[] {new String("a"),new String("b"),new String("c")});
arrays.show();
}
}
还有一种方法
public class arrays
{
public static void main(String args[])
{
String[] s = {"1","2","3"}; //因为数组的容量在编译期就确定了,所以用途很小
for(int j = 0; j<s.length; j++)
{
System.out.println(" i[ "+j+" ]= "+s[j]);
}
}
}
多维数组
public class arrays
{
public static void main(String args[])
{
System.out.println("This is Mulitdimentsional Arrays Demo One");
int[] [] ii={{1,2,3},{2,3,4}};
for(int i=0; i<ii.length ;i++)
{
for(int j = 0; j< ii[i].length ; j++)
{
System.out.println(" i["+i+"] ["+j+"]="+ii[i] [j]);
}
}

System.out.println("This is Mulitdimentsional Arrays Demo Two");
int[] [] [] iii = new int[2] [3] [4];
for(int i=0;i<iii.length;i++)
for(int j=0;j<iii[i].length;j++)
for(int k=0;k<iii[i] [j].length;k++)
System.out.println(" i["+i+"]["+j+"]["+k+"] = "+iii[i] [j] [k]);

System.out.println("This is Mulitdimentsional Arrays Demo Three");
Integer[] [] II= new Integer[2] [];
for(int i=0;i<II.length;i++)
{
II[i]=new Integer[5];
for(int j=0;j<II[i].length;j++)
{
II[i][j]=new Integer(4);
System.out.println(" i["+i+"] ["+j+"]="+II[i] [j]);
}
}
}
}
注意:多维数组中的每一组中括号代表arrays的层次,中括号从左到右分别代表大括号的从外到内
习题解答:
因为有的题目比较简单,实在是没有讲解的意义,我会把稍微有点难度的题目拿出来解答,但是也许大家的程度不一样,有的题目人家不觉得容易的你却觉得很容易,如果是这样的话,谁有问题可以从qq上或mail上告诉我,我会把答案给你写出来
12、撰写具有finalize()的class,并且保证他一定会被调用
class test
{
protected void finalize()
{
System.out.println(" finalize() running......");
}
public static void main(String args[])
{
new test(); //注意这里为什么写成new test(),而不写成test t = new test();原因就是这样写更容易引起垃圾回收器的注意
System.gc();
}
}
使用System.gc();可能会让你的finalize()调用,记住我们只是说可能,System.gc();只是一个请求,但是至于是否执行,不得而知。大家一定要记住,无论是垃圾回收还是终结动作,都不一定保证会发生,如果jvm没有面临资源紧张的情况,那么,他就不会去执行清理动作,以节省系统开支
Basically, there’s no way to ensure that finalize( ) will be called.

13、撰写一个名叫tank的类,此类的状态是满的或者是空的,他的死亡条件是,对象在清理的时候必须是空的,请撰写finalize()函数,来判断其死亡条件是否成立,并且
在main 中检测几种可能发生的情况
class Tank
{
static int counter = 0;
int id = counter++;
boolean full = false;
public Tank()
{
System.out.println("Tank " + id + " created");
full = true;
}
public void empty()
{
full = false;
}
protected void finalize()
{
if(full)
System.out.println( "Error: tank " + id +" must be empty at cleanup");
else
System.out.println( "Tank " + id + " cleaned up OK");
}
}
class tankTest
{
public static void main(String args[])
{
new Tank();
new Tank().empty();
System.gc();
}
}
16、撰写一个类,其中有2个string数据,一个在构造函数初始化,一个在定义处初始化,这2中方法有什么区别
class test
{
String s1="hello!";
String s2;
public test(String s)
{
s2=s;
}
public static void main(String args[])
{
test t =new test("goodbye");
System.out.println(t.s1); //static的函数只可以调用static的数据和函数,但是对象.数据的方式除外
System.out.println(t.s2);
}
}
s1的初始化动作是在构造函数调用之前就完成的~,而与此同时s2也被初始化,但是他被初始化成null,以后s2可以根据传递给构造函数的值的变化而变化,
更具有柔韧性。
18、撰写一个class,有一个string 数据,使用实体初始化的方式,并且说出与本书不同的用途
class test
{
String s;
{
s="hello!"; //注意不要写成 String s="hello",否则就成局部变量了!
System.out.println("Instance before constructor ");
}
test()
{
System.out.println(s+"from test()");
}
public static void main(String args[])
{
test t=new test();
}
}
书上的说的用途是,这种用法在匿名内部类(现在还没学到)中是必须用到,因为构造函数需要有个函数名,但是匿名内部类是没有名字的 ,于是我们需要用这种方法来初始化数据!我们再来说个用途:在一般的class中,实体初始化方式是在构造函数调用之前调用的,那么既然这样,这个程序会先显示Instance before constructor再显示hello! from test(),足以说明问题。
20、撰写一个函数,能够产生三维数组并且初始化,数组的容量由函数的引数决定,array的初始值必须是在函数因数所指定的范围内,再撰写一个函数,
可以打印出数组。在main函数中需要能够产生不同容量的数组
class test
{
//接受引数的函数,x、y、z分别代表数组中的元素,d1、d2代表数组的范围,并且该函数可以返回一个数组
public static double[][][] createArray(int x , int y , int z ,double d1,double d2)
{
double[][][] darray = new double[x][y][z]; //定义一个数组,未初始化
double incr = (d1-d2)/(x * y * z); //计算每个数组值的增量,因为咱们数组的值要在d1-d2的范围中
double value=d2; //数组中每一个元素的值,并且让他初始值等于传递引数的最小值
for( int i =0 ; i<darray.length ; i++) //初始化数组
for( int j =0 ; j < darray[i].length ; j++)
for( int k =0 ; k < darray[i][j].length ; k++)
{
darray[i][j][k]=value; //给每个元素指派值
value=incr+value; //重新类加值,用于下次运算
}
return darray; //返回一个数组
}
public static void showArray(double[][][] darray) //显示数组的函数
{
for( int i =0; i<darray.length ; i++)
{
for( int j=0; j<darray[i].length; j++)
{
for( int k =0; k<darray[i][j].length ;k++)
System.out.println("darray["+i+"]["+j+"]["+j+"] = "+darray[i][j][k]);
}
System.out.println("*****************"); //打印分割符
}
}
public static void main(String args[])
{
double[][][] test =createArray(2,3,4,11.0,2.0);
showArray(test);
}
}
好了以上就是第四章所有的内容,知识点的东西很少,基本上全是需要理解的东西,看不懂的地方多看几便就好了~,我开始的时候看了3遍才明白呢~呵呵~驽钝
下一篇我们讲解的第五章 隐藏实现细目 希望大家继续捧场

大家有什么问题,可以这样联系我
E-mail molmd@163.net /163.com QQ:31349283 我们可以共同学习进步!
欢迎访问我的blog,http://blog.csdn.com/maoerzuozuo 里面有更多学习内容!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: