数组的总结

 

//数组的声明和赋值的各种方式

public class ArrayTest

{

       public static void main(String[] args)

       {

              /*   第一种方式,先声明后赋值,这是java的语言规范标准

              int[] a = new int[4];

              a[0] = 1;

              a[1] = 2;

              a[2] = 3;

              a[3] = 4;

              System.out.println(a[3]);

              */

              /*   第二种方式,先声明后赋值,这种方法是c/c++语言的规范

              int a[] = new int[2];

              a[0] = 1;

              a[1] = 2;

              System.out.println(a[1]);

              */

              /*  第三种方式:直接赋值

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

              System.out.println(a[2]);

              int[] b = new int[]{1, 2, 3, 4};

              System.out.println(b[3]);

              */   

              /*  第四种方式赋值:for循环赋值

              int[] a = new int[100];  

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

              {

                     a[i] = i + 1;

              }

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

              {

                     System.out.println(a[i]);

              }

              a.length = 200;//这是错误的,因为数组int的修饰符是public final

              */

              /*

              int[] a = new int[4];

              System.out.println(a[0]);//由于整型的成员变量的默认初始值0,数组是一种对象,它的成员变量默认初始值也是0;

              boolean[] b = new boolean[3];//同上,这是个boolean数组的默认初始值为false;

              System.out.println(b[2]);

              */

       //即使数组a和数组b的内容长度和数值都一样,但是它们是不同的对象,所以引用地址是不一样的,因此a.equals(b)的结果是false;

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

              int[] b = {1, 2, 3};

              System.out.println(a.equals(b));

       }

}

 

//类和数组对象

public class ArrayTest2

{

       public static void main(String[] args)

       {

              Person[] p = new Person[3];

              p[0] = new Person(10);

              p[1] = new Person(20);

              p[2] = new Person(30);

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

              {

                     System.out.println(p[i].age);

              }

              Person[] p2 = new Person[5];

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

              {

                     System.out.println(p2[i]);

              }

       }

}

class Person

{

       int age;

       public Person(int age)

       {

              this.age = age;

       }

}

 

输出结果:

10

20

30

Null

Null

Null

Null

Null

//数组对象中的属性赋值

public class ArrayTest3

{

       public static void main(String[] args)

       {

              Student[] s = new Student[100];

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

              {

                     s[i] = new Student();

                     s[i].name = i % 2 == 0 ? "zhangsan" : "lisi";

              }

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

              {

                     System.out.println(s[i].name);

              }

       }

}

 

class Student

{

       String name;

}

//介绍二维数组

public class ArrayTest4

{

       public static void main(String[] args)

       {

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

              //System.out.println(i[0] instanceof int[]);//这表示i[0]的数组类型是int[]

              int m = 0;

              for(int i = 0; i < 2; i++)

              {

                     for(int j = 0; j < 3; j++)

                     {

                            m++;

                            a[i][j] = 2 * m;            

                     }

              }

             

       }

}

//已知行数组明确,不明确列数组的前提下

public class ArrayTest5

{

       public static void main(String[] args)

       {

              /*

              int[][] a = new int[3][];

              a[0] = new int[2];

              a[1] = new int[3];

              a[2] = new int[1];

              */

              //int[][] a = new int[][3];//这是错误的表示方式,因为行数组不明确,即使知道列数组也没用

              //int[] a = new int[]{1, 2 ,3};

              int[][] a = new int[][]{ {1 ,2 ,3}, {4}, {5, 6, 7, 8} };

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

              {

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

                     {

                            System.out.print(a[i][j] + " ");

                     }

                     System.out.println();

              }

       }

}

//接口与数组

public class ArrayTest6

{

       public static void main(String[] args)

       {

              I[] i = new I[2];

              i[0] = new C();

              i[1] = new C();

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

              I[] b = new I[]{new C(), new C()};

             

       }

}

interface I

{

}

class C implements I

{

}

//介绍三维数组

public class ThreeDimensionArrayTest

{

       public static void main(String[] args)

       {

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

              System.out.println(a instanceof int[][][]);

              System.out.println(a[0] instanceof int[][]);

              System.out.println(a[0][0] instanceof int[]);

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

              {

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

                     {

                            for(int k = 0; k < a[i][j].length; k++)

                            {

                                   a[i][j][k] = 100;

                            }

                     }

              }

       }

}

//值交换的三种方式

public class Swap

{

       public static void swap(int a, int b)

       {

              int temp = a;

              a = b;

              b = temp;

       }

       public static void main(String[] args)

       {

              int x = 3;

              int y = 4;

              Swap.swap(x, y);

              System.out.println(x);

              System.out.println(y);

              System.out.println("-----------------");

              int temp = x;

              x = y;

              y = temp;

              System.out.println(x);

              System.out.println(y);

              System.out.println("-----------------");

              int a = 3;

              int b = 4;

              a = a + b;

              b = a - b;

              a = a - b;

              System.out.println(a);

              System.out.println(b);

       }

}

//数组传递起作用,值传递不起作用

public class Swap2

{

       public static void swap(char[] ch, char c)

       {

              ch[0] = 'B';

              c = 'D';

       }

       public static void main(String[] args)

       {

              char[] ch = {'A', 'C'};

              swap(ch, ch[1]);

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

              {

                     System.out.println(ch[i]);

              }

       }

}

public class Swap3

{

       public static void swap(int[] i)

       {

              int temp = i[0];

              i[0] = i[1];

              i[1] = temp;

       }

       public static void main(String[] args)

       {

              int[] i = {1, 2};

              swap(i);

              System.out.println(i[0]);

              System.out.println(i[1]);

       }

}

//数组的复制,调用的是lang包下的System类下的arraycopy方法

public class ArrayCopy

{

       public static void main(String[] args)

       {

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

              int[] b = new int[4];

              System.arraycopy(a, 0, b, 0, 4);

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

              {

                     System.out.print(b[i] + " ");

              }

       }

}

//数组的比较,有自定义和系统自带的

import java.util.Arrays;

public class ArrayEqualsTest

{

       // compare the content of two arrays,这是自定义数组比较

       public static boolean isEquals(int[] a, int[] b)

       {

              if(a == null || b == null)

              {

                     return false;

              }

              if(a.length != b.length)

              {

                     return false;

              }

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

              {

                     if(a[i] != b[i])

                     {

                            return false;

                     }

              }

              return true;

       }

       public static void main(String[] args)

       {

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

              int[] b = {1, 2, 3};

              System.out.println(isEquals(a, b));

              System.out.println("----------");

        //这是调用util包下的Arrays的方法,里面定义了很多类型的数组比较,比较数组的内容是否一致,无须看引用地址是否一样

              System.out.println(Arrays.equals(a, b));

       }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值