1.3.1-数组

本文是1.3.1-数组学习笔记

数组的概念

数组是由同一类型的对象或者基本数据组成,并封装在同一个标识符(数组名称)下。
数组是对象

  • 动态初始化
  • 可以赋值给Object类型的变量
  • 在数组中可以调用类Object的所有方法
  • 每个数组都由一个public final 修饰的成员变量:length,即数组含元素的个数(length可以是正数或零)

数组元素
- 数组中变量被称作数组元素
- 元素没有名字,通过数组名字和非负数访问数组元素
数组的创建和使用
数组的引用声明:声明数组时无需指明数组元素的个数,也不为数组元素分配内存空间;不能直接使用,必须经过初始化内存后才能使用。
数组声明举例:

Type[] arrayName
int[] intArray;
String[]stringArray;
Type ArrayName[]; 
Type arrayName[];

数组的创建

  • 用关键字 new 构成数组的创建表达式,可以指定数组的类型和数组元素个数。元素可以是 常量也可以是变量。
  • 基本类型数组的每个元素都是一个基本类型的变量;引用类型数组的每个元素都是对象的引用。
    数组的创建举例:
arryName = new Type[componets number];
例如:
int ai;ai = new int[10];
String []s = new String[3];
或者可以将数组中的声明和创建一并执行
int ai[]= new int[10];
可以在一条声明语句中创建多个数组
String []s1 = new String[],s2= new String[8];

数组元素的初始化

  • 声明数组时,给出了数组的初始值,程序便利用数组初始化创建数组并对它的各个元素进行初始化
int a[]={1,23,3,33};
  • 创建数组时,如果没有指定初始值,数组便被赋予默认初始值
  • 基本类型数值数据,默认的初始值为0
  • boolean类型数据,默认值为false
  • 引用类型元素的默认值为null
  • 程序也可以在数组被构造之后改变数组元素的值

使用数组

  • 引用数组的一个元素
arrayName[index]
  • 数组下标必须为int,short,byte或者char.
  • 下标从零开始计数
  • 元素的个数即为数组的长度,可以通过arryName.length得到
  • 元素下标最大值为length-1,如果超过最大值,将会产生数组越界异常(ArrayIndexOutOfBoundsException)
  • 数组名是一个引用:
public class Arrays {
    public static void main(String[] args) {
        int []a1={1,2,3,4,5};
        int[]a2;
        a2 =a1;
        for (int i = 0; i < a2.length; i++) {
            a2[i]++;
        }
        for (int i = 0; i < a1.length; i++) {
            System.out.println("a1["+i+"] =" +a1[i]);
        }
    }
}
/**
 * 输出结果
 * a1[0] =2 a1[1] =3 a1[2] =4 a1[3] =5 a1[4] =6
 */
  • 字符串引用构成的数组–每个元素都是引用
public class ArrayOfStringsDemo {
    public static void main(String[] args) {
        String[] anArray = { "String One", "String Two", "String three" };
        for (int i = 0; i < anArray.length; i++) {
            System.out.println(anArray[i].toLowerCase());
        }
    }
}
/**
 * 运行结果
 * string one string two string three
 */

复制元素的部分或者全部元素
arraycopy(Object src, int srcPos, Object dest, int destPos, int length);

/**
 * 复制数组或者数组中的部分元素
 * 
 */
public class ArrayCopyDemo {
    public static void main(String[] args) {
        char[] copyFrom = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' };
        char[] copyTo = new char[7];
        System.arraycopy(copyFrom, 2, copyTo, 0, 4);
        System.out.println(new String(copyTo));
    }
}
// 输出结果 cdef

多维数组
二维数组的声明和构造

  • int[][]myArray;
    myArray是指向2维数组的引用。其初始化值为null
  • int[][]myArray= new int[3][5];
    定义引用时,同时构造数组并初始化引用。这个数组所有元素的初始值为0.
  • int[][]myArray={{1,2,3},{4,5,6}}
    定义引用、构造数组,初始化数组元素。
    -二维数组的长度及每行的长度 行数

public class UnevenExample3 {
    public static void main(String[] args) {
        // 声明并构造一个2维数组
        int[][] uneven = { { 1, 2, 2 }, { 0, 3 }, { 1, 2, 3, 4 } ,{0}};
        // 数组的长度(行)
        System.out.println("Length of array is :" + uneven.length);
        // 数组每一行的长度(列数)
        System.out.println("Length of row[0] is :" + uneven[0].length);
        System.out.println("Length of row[1] is :" + uneven[1].length);
        System.out.println("Length of row[2] is :" + uneven[2].length);
    }
    /**
     * 输出数据 
     *  Length of array is :3
     *  Length of row[0] is :3
     *  Length of row[1] is :2
     *  Length of row[2] is :4
     */
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值