马士兵 J2SE第五章 数组 个人笔记_第1页
马士兵 J2SE第五章 数组 个人笔记_第2页
马士兵 J2SE第五章 数组 个人笔记_第3页
马士兵 J2SE第五章 数组 个人笔记_第4页
马士兵 J2SE第五章 数组 个人笔记_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、尚学堂科技_马士兵_java视频教程_j2se_5.0_第05章_数组 听课笔记 09.12.3- by lovexforce第5章 数 组完成时间:2009年12月3日-2009年12月4日数组的内存布局与常见算法一、 概念1. 数组可以看成是多个相同类型数据组合,对这些数据的统一管理2. 数组变量属于引用类型,数组可以看成是对象,数组中的每个元素相当于该对象的成员变量3. 数组中的元素可以是任何数据类型,包括基本型和引用类型二、 一维数组声明:type var ; 或者 type var 例如:int a1 ; int a2;double b ;person p1; /person对象的引

2、用 string s1 ; /string对象的引用注意:java语言中声明数组时不能指定其长度(数组中元素个数),例如 int a5; /非法三、 数组对象创建(内存分析)java中使用关键字new创建数组对象:数组名 = new 数组元素的类型 数组元素的个数栈内存s null堆内存例如:public class testpublic static void main(string args )int s; s = new int5; for (int i = 0 ; i 5 ; i+)java分配5个小格,自动做初始化,int类型堆内存0 00 10 20 30 4栈内存s *si =

3、2*i+1; 12元素为引用数据类型的数组注意:元素为引用数据类型的数组中的每一个元素都需要实例化例如:栈内存days堆内存*public class test public static void main(string args )date days; days = new date3;for ( int i =0 ;i 3 ; i+ )days i = new date(2004,12,i+1);*栈内存days堆内存nullnullnullclass dateint year; int month ; int day;date( int y, int m, int d)year = y

4、 ; month = m; day = d;栈内存days堆内存 *四、 数组初始化动态初始化:数组定义与数组元素分配空间和赋值的操作开始进行静态初始化:在定义数组的同时就为数组元素分配空间并赋值public class test public static void main(string args )int a ;a = new int 3 ;a0 = 3 ; a1 = 9 ; a2 = 8;date days ; days = new date3;days0 = new date(1,4,2004);days0 = new date(1,4,2004);days0 = new date(

5、1,4,2004);class dateint year; int month ; int day;date( int y, int m, int d)year = y ; month = m; day = d;public class test public static void main(string args )int a = 3 ,9 ,8;date days = new date(1,4,2004),new date(1,4,2004),new date(1,4,2004);class dateint year; int month ; int day;date( int y, i

6、nt m, int d)year = y ; month = m; day = d;数组元素的默认初始化:数组是引用类型,它的元素相当于类的成员变量,因此数组分配空间后,每个元素也被安装成员变量的规则被隐式初始化public class test public static void main(string args )int a = new int 5 ;date days = new date3;system.out.println( a3 );system.out.println( days2 );class dateint year; int month ; int day;date(

7、 int y, int m, int d)year = y ; month = m; day = d;结果: 0null五、 数组元素的引用定义并用运算符new为之分配空间后,才可以引用数组中的每个元素,1. 数组元素的引用方式:arrayname index index为数组元素下标,可以是整形常量或整形表达式。a 3 ,b i ,c 6*i。数组元素下标从0开始;长度为n的数组的合法下标取值范围为 0 n-1。2. 每个数组都有一个属性length指明它的长度如a.length的值为数组a的长度(元素个数)。六、 小练习1数组小练习public class testarray public

8、 static void main(string args) int a = 2, 4, 6, 7, 3, 5, 1, 9, 8;/* for(int i=0; ia.length; i+) system.out.print(ai + ); */for(int i=0; iargs.length; i+) system.out.println(argsi); java testarray 23 567 shit 23 567shit注意:args 数组将命令行后的参数碍着打印出来 2算数程序public class testargs public static void main(string

9、 args) /* for(int i=0; iargs.length; i+) system.out.println( args i );system.out.println(usage: java test n1 op n2); */ if(args.length3) system.out.println(usage: java test n1 op n2); system.exit(-1);/非正常退出 double d1 = double.parsedouble(args0); /把args0强制转换为double型 double d2 = double.parsedouble(arg

10、s2); double d = 0; if(args1.equals(+) d = d1+d2; else if(args1.equals(-) d = d1-d2; else if(args1.equals(x) d = d1*d2; else if(args1.equals(/) d = d1/d2; else system.out.println(error operator!); system.exit(-1); system.out.println(d);/健壮:在输入数值的地方用try catch语句捕获异常3排序算法:数字(基本类型)排序numsort.javapublic cl

11、ass numsort public static void main(string args) int a = new intargs.length;for (int i=0; iargs.length; i+) ai = integer.parseint(argsi); /将args数组里的数据转换成int型放到a里面print(a);/调用打印a 数组值的方法selectionsort(a);/调用选择排序方法print(a);/调用打印a 数组值的方法private static void selectionsort(int a) for(int i = 0 ;ia.length ;

12、i+)for(int j=i+1 ;ja.length;j+)if(aj ai )int temp = ai;ai = aj;aj = temp;/* int k, temp;for(int i=0; ia.length; i+) k = i;for(int j=k+1; ja.length; j+) if(aj ak) k = j;if(k != i) temp = ai;ai = ak;ak = temp;*/ /优化算法,每次循环只调换一次 private static void print(int a) for(int i=0; ia.length; i+) system.out.pr

13、int(ai + );/打印a数组里面的值system.out.println();4排序算法:对引用类型(某个类的对象)进行排 testdatesort.java 分析过程:date a返回值public static date bubblesort(date a) /返回值可也是数组类型 就是返回指向一块堆内存的空间数组作为返回值:public class testdatesort public static void main(string args) date days = new date5;days0 = new date(2006, 5, 4);days1 = new date(

14、2006, 7, 4);days2 = new date(2008, 5, 4);days3 = new date(2004, 5, 9);days4 = new date(2004, 5, 4);date d = new date(2006, 7, 4);string str = string.valueof(d);/str = d.tostring();bubblesort(days);for(int i=0; i=1;i-) for(int j = 0;j 0) date temp = aj; aj=aj+1; aj+1=temp; return a; public static int

15、 binarysearch(date days, date d) if (days.length=0) return -1; int startpos = 0; int endpos = days.length-1; int m = (startpos + endpos) / 2; while(startpos 0) startpos = m + 1; if(pare(daysm) date.year ? 1 : year date.month ? 1 : month date.day ? 1 : day date.day ? -1 : 0; public string tostri

16、ng() return year:month:day - + year + - + month + - + day; 内存分析012342008 5 42006 7 4 2004 5 92004 5 4 2006 5 4len = 5j=1i=3a 5排序算法: 数三退一 count3quit.java 分析过程见:10_练习_8.avi 500人手拉手围成一圈从头数到尾,从1数到3,数到3就退出,接着又从1数到3这样循环,只剩一个人,那个人是原来500人中的第几个人? 2009-12-4 13:41:00算法一:public class count3quit public static vo

17、id main(string args) boolean arr = new boolean500;/布尔类型的数组模拟500个人for(int i=0; i 1) /循环的条件if(arrindex = true) /这个人还在圈里面countnum +;/数值加1if(countnum = 3) /数值到时3,则数组重新回零,这个人退出,剩下的人减1countnum = 0;/ 数组重新回零arrindex = false;/ 这个人退出leftcount -;/剩下的人减1index +;/位置往上递增if(index = arr.length) index = 0;/判断是否循环到数组

18、的范围,到界限后index回零for(int i=0; i 1)/判断,countnum +;if( countnum = 3)countnum = 0;/计数器回零kc.delete( k );/调用delete()方法,删除当前小孩k = k.right;/挪到下一个小孩,system. out.println(kc.first.id); class kidint id;kid left;/kid的左面的小孩kid right;/kid的右面的小孩class kidcircle/小孩围成的圈int count = 0;kid first, last;/kid的左面的小孩和右边的小孩kidc

19、ircle( int n)/构建有n个人的圈for ( int i = 0 ; i n ; i+)add( );/调用add()方法/添加和退出的功能void add( )/添加小孩kid k = new kid( );/new小孩k.id = count;/设定小孩的id为countif (count = 0)/先判断有小孩没有,只有一个小孩时first = k;last = k;k.left = k;k.right = k;/往里面添加小孩,整个圈只有自己,else/有两个或者两个以上的小孩,即在几个小孩围成的圈里再添加一个小孩last.right = k;k.left = last;k.

20、right = first;first.left = k;last = k;count+;/循环void delete( kid k)if( count = 0)/没有小孩,不能删return;else if( count = 1)/只有一个小孩,第一个和最后一个都为空first = last =null;else/其他情况,k左边的小孩的右手拉着k右边的小孩,k右边的左手拉着k左边的小孩k.left.right = k.right;k.right.left = k.left;if( k = first)/k如果为第一个小孩,则他为第一个小孩的右边的小孩first = k.right;else

21、 if(k =last )/k如果为最后个小孩,则他为最后个小孩的左边的小孩last = k.left;count -;结果:435算法三: 数组模拟链表1230位置: 0 1 2 3 让下标为1的小格退出则:将下标为0的小格的值变为第2个下标值22230位置: 0 1 2 3 6 搜索算法: 建立在排好序的前提下,(二分搜索法)testsearch.java 分析过程:14_练习_12.avi基本思想是,将n个元素分成个数大致相同的两半,取an/2与欲查找的x作比较,如果x=an/2则找到x,算法终止。如果xan/2,则我们只要在数组a的左半部继续搜索x(这里假设数组元素呈升序排列)。pub

22、lic class testsearch public static void main(string args) int a = 1, 3, 6, 8, 9, 10, 12, 18, 20, 34 ;int i = 12;/找值为12的位置/system.out.println(search(a, i);system.out.println(binarysearch(a, i);public static int search(int a, int num) for(int i=0; ia.length; i+) if(ai = num) return i;return -1;public

23、static int binarysearch(inta, int num) /二分法 if (a.length=0) return -1;/没找到 int startpos = 0; /起始位置 int endpos = a.length-1;/结束位置 int m = (startpos + endpos) / 2;/找中间的位置 while(startpos am) startpos = m + 1;/起始位置变为m+1 if(num am) endpos = m -1;/结束位置变为m-1 m = (startpos + endpos) / 2; return -1; 七、 二维数组定

24、义:二维数组相当于数组元素为元素的数组,例如 int a = 1,2,3,4,5,6,7,8,9 ;java中多维数组的声明和初始化应按从高维到低维的顺序进行,即从左到右a* * *int a = new int 3 ;a 0 = new int 2 ;a 1 = new int 4 ;a 2 = new int 3 ;int t1 = new int 4 ;/非法二维数组初始化:静态初始化 int inta = 1,2 , 2,3 , 3,4,5 ;int intb32 = 1,2,2,3,4,5;/非法,不能写32动态初始化:int a = new int 3 5 ;int b = new

25、 int 3 ;b 0 = new int 2 ;b 1 = new int 3 ;b 2 = new int 5 ;例子1public class testpublic static void main(string args )int a = 1,2,3,4,5,6,7,8,9 ;for(int i = 0 ; i a.length ; i+ )for ( int j = 0 ; j =a i .length ; j+)system.out.println(a+i+j+ = +a i j + );system.out.println();例子2public class testpublic

26、 static void main(string args )string s;/string引用类型的二维数组s = new string 3 ;s 0 = new string 2;s 1 = new string 3;s 2 = new string 2;for (int i = 0; is.length;i+)for (int j = 0 ; js i .length ; j+)sij = new string (我的位置是:+i+,+j);for(int i = 0 ;is.length ; i+)for(int j = 0 ; jsi.length ; j+ )system.out.println(sij+ )

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论