• Java 数组


    数组说明:

      1. 数组可以看成是多个相同类型的数据组合,并对这些数据的统一管理;

      2. 数组变量属于引用类型,数组也可以看成是对象,数组中每一个元素相当于该对象的成员变量;

      3. 数组中的元素可以是任何的数据类型,包括基本类型和引用类型;

      4. C 和 C++ 的数组都可以分配在栈上面,而 Java 的数组是引用类型,分配在堆上面.

    一维数组的声明:

      1. 一维数组的声明方式:type[] var  或者 type var[];

          2. 例如: int a1[]; int[] a2;

                       double b[];

                       person p1[]; ← person 对象的引用

                       String s1[]; ← String 对象的引用

          3. Java 语言中声明数组时吗,不能指定其长度(数组中元素的个数),例如:int a[5]; ✘  // 非法

    数组对象的创建:

      1. Java 中使用关键字 new 创建数组对象,如:

    public class Test1{
      public static void main(String[] args){
      	int[] s;
    	s = new int[5];
    	for(int i=0;i<5;i++){
    	  s[i] = 2*i+1;
    	  System.out.println(s[i]);
    	}
      }
    }
    

              

    元素为引用数据类型:

      1. 元素为引用类型的数组中每一个元素都要实例化,如:

    class Date {
      private int year, month, day;
    	public Date(int year, int month, int day) {
    	  this.year = year;
    	  this.month = month;
    	  this.day = day;
    	}
    	
     }
    public class Test1{
      public static void main(String[] args) {
    	 Date[] days;
    	 days = new Date[3];
    	 for(int i=0;i<3;i++){
    	   days[i] = new Date(2017,4,i+23);
    	 }
      }
    }
    

     数组初始化:

      1. 动态初始化:数组定与为数组元素分配空间和赋值分开进行,如:

             Demo_1

    class Date {
        private int year, month,day;
        public Date(int year, int month, int day) {
    	this.year = year;
    	this.month = month;
    	this.day = day;
         }
    }
    public class Test1{
        public static void main(String[] args) {
    	int a[];
    	a = new int[3];
    	a[0] = 3;
    	a[1] = 9;
    	a[2] = 5;
    	Date days[];
    	days = new Date[3];
            days[0] = new Date(2017, 4, 23);
            days[1] = new Date(2017, 4, 24);
            days[2] = new Date(2017, 4, 25);
    	}
    }
    

       Demo_2

    class Date {
      private int year, month, day;
        public Date(int year, int month, int day) {
    	  this.year = year;
    	  this.month = month;
    	  this.day = day;
    	}
    	public String toString() {
    	  return "Year:Month:Day "+year+"-"+ month+ "-" +day;
    	}
    }
    public class Test1 {
      public static void main(String[] args) {
    	Date date[] = new Date[3];
    	date[0] = new Date(2017,4,23);
    	date[1] = new Date(2017,4,24);
    	date[2] = new Date(2017,4,25);
    	for(int i=0;i<date.length;i++){
    	  System.out.println(date[i]);
    	}
      }
    }
    // 运行结果:
    // Year:Month:Day 2017-4-23
    // Year:Month:Day 2017-4-24
    // Year:Month:Day 2017-4-25

      2. 静态初始化:在定义数组的同时九尾数组分配空间并赋值,如:

    class Date {
        private int year, month,day;
        public Date(int year, int month, int day) {
            this.year = year;
    	this.month = month;
    	this.day = day;
        }
    }
    public class Test1{
        public static void main(String[] args) {
             int a[] = {3,8,6};
    	 Date days[] = {new Date(2017, 4, 23), new Date(2017, 4, 24), new Date(2017, 4, 25)};
    	}
    }    
    

       3. 数组元素的默认初始化:数组是引用类型,它的元素相当于类的成员变量,因此数组分配空间后,每个元素也被按照成员变量的规则被隐式初始化.

    class Date {
        private int year, month,day;
    	public Date(int year, int month, int day) {
    	    this.year = year;
    	    this.month = month;
    	    this.day = day;
    	}
    }
    public class Test1{
    	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]);
    	}
    }
    // results:  0
    //           null
    

    数组元素的引用:

      1. 定义并引用运算符 new 为之分配空间后, 才可以引用数组中的每个元素;

          2. 每个数组都有一个属性 length,指明它的长度.

    【注】:在 Java 中输出双引号,要用 "

    【注】:System.exit(0); // 虚拟机正常退出

               System.exit(-1); // 虚拟机非正常退出

  • 相关阅读:
    python3读取chrome浏览器cookies
    python3 reqeuests给OSC全站用户刷积分
    批处理与python代码混合编程的实现方法
    python3 使用pyperclip读写剪贴板(windows)
    python3使用requests登录人人影视网站
    Flask form
    Flask session
    Flask 目录
    flask 源码剖析
    单例模式
  • 原文地址:https://www.cnblogs.com/bosongokay/p/6752339.html
Copyright © 2020-2023  润新知