马士兵J2SE-第五章-数组
来源:互联网 发布:一次性洗脸巾危害 知乎 编辑:程序博客网 时间:2024/06/05 06:22
//数组对象的创建public class test { public static void main(String[] args) { int[] s; s=new int[5]; for(int i=0;i<5;i++) { s[i]=i*2+1; } System.out.println(s[4]); }}
输出:
9
//元素为引用数据类型的数组的创建public class test {public static void main(String[] args) {Date[] days;days=new Date[3];for(int i=0;i<3;i++) {days[i] =new Date(2004,4,i+1);}}}class Date {int 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(2,4,2004),new Date(3,4,2004),};}}class Date {int year,month,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[] =new int[5];Date[] days=new Date[3];System.out.println(a[3]);System.out.println(days[2]);}}class Date {int year,month,day;Date(int y,int m,int d) {year=y;month=m;day=d;}}
输出:
0
null
数组小练习
public class test {public static void main(String[] args) {int a[] ={3,4,5,6,7,8,9,2,3,41};for(int i=0;i<a.length;i++) {System.out.print(a[i]+" ");//打印完不换行}}}
输出:
3 4 5 6 7 8 9 2 3 41
public class test {public static void main(String[] args) {//int a[] ={3,4,5,6,7,8,9,2,3,41};//for(int i=0;i<a.length;i++) {//System.out.print(a[i]+" ");//打印完不换行//}for(int i=0;i<args.length;i++){System.out.println(args[i]);}}}
输出:
java test 33 44 aa bb
结果:
33
44
aa
bb
public class test {public static void main(String[] args) {/*for(int i=0; i<args.length; i++) {System.out.println(args[i]);}System.out.println( "Usage: java Test \"n1\" \"op\" \"n2\""); */ if(args.length<3){ System.out.println( "Usage: java Test \"n1\" \"op\" \"n2\""); System.exit(-1); } double d1 = Double.parseDouble(args[0]); double d2 = Double.parseDouble(args[2]); double d = 0; if(args[1].equals("+")) d = d1+d2; else if(args[1].equals("-")) d = d1-d2; else if(args[1].equals("x")) d = d1*d2; else if(args[1].equals("/")) d = d1/d2; else{ System.out.println("Error operator!"); System.exit(-1); } System.out.println(d);}}
java test 1 + 2
输出:
3.0
//二维数组初始化public class test {public static void main(String[] args) {String[][] s;s=new String[3][];s[0]=new String[2];s[1]=new String[3];s[2]=new String[2];for(int i=0;i<s.length;i++) {for(int j=0;j<s[i].length;j++) {s[i][j]=new String("我的位置是: "+i+","+j);}}for(int i=0;i<s.length;i++) {for(int j=0;j<s[i].length;j++) {System.out.print(s[i][j]+" ");}System.out.println();}}}
输出:
我的位置是: 0,0 我的位置是: 0,1
我的位置是: 1,0 我的位置是: 1,1 我的位置是: 1,2
我的位置是: 2,0 我的位置是: 2,1
拷贝
public class test {public static void main(String[] args) {String[] s={"Microsoft","IBM","Sun","Oracle","Apple"}; String[] sBak=new String[6]; System.arraycopy(s, 0, sBak, 0, s.length); for(int i=0;i<sBak.length;i++) { System.out.print(sBak[i]+" "); } System.out.println(); int[][] intArray={{1,2},{1,2,3},{3,4}}; int[][] intArrayBak=new int[3][]; System.arraycopy(intArray, 0, intArrayBak, 0, intArray.length); intArrayBak[2][1]=100; for(int i=0;i<intArray.length;i++) { for(int j=0;j<intArray[i].length;j++) { System.out.print(intArray[i][j]+" "); } System.out.println(); }}}
输出:
Microsoft IBM Sun Oracle Apple null
1 2
1 2 3
3 100
- 马士兵J2SE-第五章-数组
- 马士兵J2SE-第二章-J2SE基础语法-运算符
- 马士兵J2SE-第四章-异常处理
- 马士兵J2SE-第七章-容器
- 马士兵J2SE-第八章-流
- 马士兵J2SE-第三章-面向对象-基础及重载
- [马士兵J2SE学习笔记]第七章 容器
- [马士兵J2SE学习笔记]第八章 流
- 马士兵J2SE-第二章-J2SE基础语法-标识符、关键字、数据类型转换
- 马士兵J2SE-第二章-J2SE基础语法(周六补充)-递归、非递归
- 马士兵 讲 J2SE 设计模式
- J2SE 坦克大战 马士兵(1)
- J2SE 坦克大战 马士兵(2)
- J2SE 坦克大战 马士兵(3)
- J2SE 坦克大战 马士兵(4)
- J2SE 坦克大战 马士兵(5)
- J2SE 坦克大战 马士兵(5)
- 马士兵老师经典J2SE中的经典语录
- 程序1
- Linux终端下解决中文乱码问题
- 资料收藏
- 关于Anycast的介绍
- 程序的加载和链接机制
- 马士兵J2SE-第五章-数组
- 假设字符串类似这样的aba和aab就相等,现在随便给你二组字符串,请编程比较他们看是否相等
- 生产者消费者模型-python-多进程
- 百度笔试题系列之一-2011年9月24日百度笔试RD-1
- ExtJS之Ext.Ajax.request用法详解
- Android开发 下载显示进度
- (14)使用查询表达式的LINQ
- u突然放毒药
- Ubuntu下的deb包制作详解