复制代码
- //:Arrays.java
- //Arraysofprimitives.
- publicclassArrays{
- publicstaticvoidmain(String[]args){
- int[]a1={1,2,3,4,5};
- int[]a2;
- a2=a1;
- for(inti=0;i<a2.length;i++)
- a2[i]++;
- for(inti=0;i<a1.length;i++)
- prt("a1["+i+"]="+a1[i]);
- }
- staticvoidprt(Strings){
- System.out.println(s);
- }
- }///:~
复制代码
- //:ArrayNew.java
- //Creatingarrayswithnew.
- importjava.util.*;
- publicclassArrayNew{
- staticRandomrand=newRandom();
- staticintpRand(intmod){
- returnMath.abs(rand.nextInt())%mod+1;
- }
- publicstaticvoidmain(String[]args){
- int[]a;
- a=newint[pRand(20)];
- prt("lengthofa="+a.length);
- for(inti=0;i<a.length;i++)
- prt("a["+i+"]="+a[i]);
- }
- staticvoidprt(Strings){
- System.out.println(s);
- }
- }///:~
复制代码
- //:ArrayClassObj.java
- //Creatinganarrayofnon-primitiveobjects.
- importjava.util.*;
- publicclassArrayClassObj{
- staticRandomrand=newRandom();
- staticintpRand(intmod){
- returnMath.abs(rand.nextInt())%mod+1;
- }
- publicstaticvoidmain(String[]args){
- Integer[]a=newInteger[pRand(20)];
- prt("lengthofa="+a.length);
- for(inti=0;i<a.length;i++){
- a[i]=newInteger(pRand(500));
- prt("a["+i+"]="+a[i]);
- }
- }
- staticvoidprt(Strings){
- System.out.println(s);
- }
- }///:~
复制代码
- //:ArrayInit.java
- //Arrayinitialization
- publicclassArrayInit{
- publicstaticvoidmain(String[]args){
- Integer[]a={
- newInteger(1),
- newInteger(2),
- newInteger(3),
- };
- //Java1.1only:
- Integer[]b=newInteger[]{
- newInteger(1),
- newInteger(2),
- newInteger(3),
- };
- }
- }///:~
复制代码
- //:VarArgs.java
- //UsingtheJava1.1arraysyntaxtocreate
- //variableargumentlists
- classA{inti;}
- publicclassVarArgs{
- staticvoidf(Object[]x){
- for(inti=0;i<x.length;i++)
- System.out.println(x[i]);
- }
- publicstaticvoidmain(String[]args){
- f(newObject[]{
- newInteger(47),newVarArgs(),
- newFloat(3.14),newDouble(11.11)});
- f(newObject[]{"one","two","three"});
- f(newObject[]{newA(),newA(),newA()});
- }
- }///:~
欢迎光临 仓酷云 (http://ckuyun.com/) | Powered by Discuz! X3.2 |