java实训第六次课后实训——数组

来源:互联网 发布:网络硬盘录像机怎么用 编辑:程序博客网 时间:2024/06/16 09:03

数组

1.

          100

 

一维数组             a[0]=100                                    1001

  int[] a;               a+0*4=1001                                 1002

  int[] a={1,2,3,4};                                                 1003

  int[] a=new int[5];                                                 1004

  int[] a=new int[]{5,6,7};                                           1005

                       a[1] a+1*4=1005

 

 


                       a[2] a+2*4=1009

 

 

2.为什么数组下标从0开始:数组变量本身拿着内存地址的一第片地址。

3.i.length  数组长度

4.  for(h:i1){         //打印数组

   System.out.print(h); 

   }

= for(int u=0;u<i1.length;u++){

Int h=i1[u];

System.out.println(h);

}

5.冒泡排序

int[] i={10,50,66,7,12,78};

for(int a=0;a<i.length-1;a++){

 for(inty=i.length-1;y>a;y--){

   If(i[y]<i[y-1]) {

    int h=i[y];

    i[y]=i[y-1];

    i[y-1]=h;

  }

输出:

7 10 12 50 66 78

 

 }

}

for(int j:i){

 System.out.println(j+” ”);

}

6.二维数组:int[][] a= new int[3][3];

//int [][]a=new int [][]{{10,20},{30,40}};

int [][]a=new int[2][];

a[0]=new int[]{10,50,60};

a[1]=new int[]{1,45,2,52,22,21};

/for(int[] list:a){     //

 for(int  u:list){     //

    System.out.print(u+” ”);

}

System.out.println(“  ”);

   }/

for(int i=0;i<a.length;i++){

 for(int u=0;u<a[i].length;u++){

  System.out.print(a[i][u]+”  ”);

}

 System.out.println(“  ”);

 

 

 

100

 

}

7.eg.int[][].i=newint[3][];

                                                     i[0].length   i[0][2]=100

              i[0]=new int[3];

i.length     i[1]=new int[2];

 

 

           i[2]=new int[1];                       

                                                    i[1].length

 

 

 

 

 

 

8.①对象数组

class Dog{

  String name;

}

     Dog[] ds=new Dog[]{new Dog(),new Dog(),new Dog()}

 


                                    旺财          笨笨

     Dog[] ds=new Dog[3];

ds[o]=newDog();

name

 

name

 

name

 

ds[1]=new Dog();

ds[2]=new Dog();

ds[0].name=”旺财”;

对象

 

对象

 

对象

 

ds[1].name=”笨笨”;

     Dog[] ds=new Dog[3];

ds[0]=a1;

0     1    2

 

ds[1]=a2;

ds[2]=a3;

ds[1].name=”旺财”;

ds

 

System.out.println(a2.name);

 

9. ②数组引用

   Dog d1=new Dog();

   d1.name=旺财”;

   Dog d2=new Dog();

name

 

d2.name=旺财它哥”;              

对象

 

 Dog[] ds=new Dog[]{d1,d2};

 ds[0].nane=”旺财二代”;

d1 d2

 

System.out.println(d1.name);

结果为旺财二代        

Dos.path

 

10.d:/>java

 

   则继续运行

 

没有

 

报错

 

main()

 

String[] 数组

 

准备外界参数

“A” “A     A” ‘/’  “*”

 

初始化static(静态成员)

 

加载Test.class

 

 

没有

Class 不存在

 

结果

 

classpath

 

没有           

 

D:/

 

classpath

 

Test.exe文件

 

没有       java.exe

 

报错。没有此命令

 

               java.exe

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

11.接受外界参数(String[] args

12.DDS”*”是目录的意思。

13.封装:面向用户更简单,面向属性更安全。

14.java权限修饰符  public protectedprivate 置于类的成员定义前,用来限定其他对象对该类对象的访问权限。

Public  任何地方

Protected 在不同包的子类中可用

Default 只可以被同一个包内部的类访问

Private 只能访问类的内部

15. 封装的两个方法 set和get

16.private String name;

   Public void setName(String n){

     name=n;

}

Public voidgetName(){

       return name;

}

 

 

 

private String name1;

private String name2;

public void setName(String n1,String n2){

name1=n1;

 name2=n2;

}

Public void getName(){

 return (name1+name2);

}

 

 

安全验证

 publicvoid setXy(int x1,int y1){

              if(x1<=-1|| y1<=-1){

                 System.out.println("wrong```");

              }else{           

                 x=x1;

                 y=y1;

}

数组的封装:

   private String[] colors=new String[10];

       publicvoid setColors(String[] cs){

              colors=cs;

              }

       publicString[] getColors(){

           return colors;

       }

   public void addcolor(int index,String color){

          colors[index]=color;

       }

       publicint getColorsSize(){

           return colors.length;

       }

Boolean类型的封装:

   public boolean state;

       publicvoid setState(boolean s){

           state=s;

       }

       publicboolean isState(){

           return state;  

       }

 

 

 

原创粉丝点击