Java学习(四)

数组

  • 数组是存储在一个连续的内存块中的元素集合。可以减少内存中变量搜索的时间
  • 数组中的每个元素必须是相同的数据类型,并且通过索引进行区分。数组中的第一个元素的索引为0
  • 单一值类型变量有时候很难满足应用程序的设计要求。数组类型变量可以同时存储多个同类型基础数据类型变量或对象

声明数组

  • 声明一个数组

    int[] age;
    int ages[];  //这种方式不推荐
    age = new int[28];  
    命名了28个int空间,并且每个空间初始化为0;new
    

    new强制初始化数组,数组里面全初始化为0

定义数组的方式

double[] grade = new double[28];
grade2[0] = 90;  //[index]index是索引
String[] str = new String[10];

Sting如果没有初始化,默认是null

数组的复制

    String[] studentcopy = new String[5];
    for(int k =0;k<student.length;k++){
        studentcopy[k] = student[k];}

arraycopy 进行数组的复制

    System.arraycopy(student, 0, studentcopy, 1, student.length-1);  
    System.out.print(studentcopy[2]);
    for(int l=0;l<5;l++){
        System.out.println(studentcopy[l]);}
  • 方法

    System.arraycopy(arg0, arg1, arg2, arg3, length);
    

第一个arg0表示旧的数组,arg1copy的起始位置,arg2新的数组,arg3copy到新数组的位置,arg4数组的长度

posted @ 2016-03-30 00:07  夜如何其  阅读(170)  评论(0编辑  收藏  举报