Java基础语法:数组
发布日期:2021-05-15 03:50:11 浏览次数:12 分类:博客文章

本文共 6395 字,大约阅读时间需要 21 分钟。


������������

���������

  • ���������������������������������������������
  • ������,������������������������������������������������������������������������������������������������������

���������������

  1. ���������������������������������������������������������������������������������������������
  2. ������������������������������������������������������������������
  3. ������������������������������������������������������������������������������������
  4. ������������������������������������������������������������������������������������������������������������������������������������������������������Java������������������������������������������������������������������������������������������������������������������������������������


������������������

���������

/* ��������������������������������������� */elementType[] arrayVarName/* ������������������������������������ */elementType arrayVarName[]

���������

  • ������������������������������������������������������������������������������������������
  • Java������������������������������������������������������������'������������'���������������������������'���������'������

���������

  • ���������������������������������'������������'������������������������������������������������������������
  • ������������������������������������������'���������'���������������������C/C++���������������������������������C/C++������������������Java���������������������������������������
  • ������������������������������������������������������������������������������������
  • ������������������������������������������������������������������������������������������������������JVM���������������������������������������������

���������

/* ������������ */int[] intArr1;/* ��������������� */int intArr2[];


������������������

���������

  1. ������������������

    new elementType[arrayLength]
  2. ������������������

    {firstElementValue, secondElementValue, thirdElementValue...}

���������

  • ������������������������������������������������������������������
  • ���������������������������������������������������������������������������������������������������������������������������������������������������������������
  • ���������������������������������������������������������������������������������������������������������������
  • ������������������������������������������������������������������������������������������������������������������������������������������

���������

  • ���������������������������������������������������������������������������������������������������������������������������������������

    ���������������������������������������������

���������

/* ��������������� */new int[5];/* ��������������� */{1, 2, 3, 4, 5};


������������������

���������������������

���������

/* ������������������������������������ */elementType[] arrayVarName = new elementType[arrayLength];/* ������������������������������������ */elementType[] arrayVarName = {firstElementValue, secondElementValue, thirdElementValue...};

���������

  • ������������������������������������������������������������������������������������������������������������������������������

���������

/* ������������������������������������ */int[] intArr1 = new int[5];/* ������������������������������������ */int[] intArr2 = {1, 2, 3, 4, 5};


������������������

���������

arrayObject.length

���������

  • ������������������������������������������������������������������������
  • ������������������������������������������������������������������������������������������������������������

���������

int[] intArr = {1, 2, 3, 4, 5};System.out.println(intArr.length);//5


������������������

���������

arrayObject[elementIndex]

���������

  • ������������������������������������������������������
  • ���������������'0'������������������������������������������������������������������������������������������������0 ~ arrayObject.length-1

���������

  • ���������������������������������������������������������������������������������������������������������������������������������ArrayIndexOutOfBoundsException���������������������������������

���������

int[] intArr = {1, 2, 3, 4, 5};System.out.println(intArr[1]);//2


������������������������������

���������

  1. 'for'������

    for (int i = 0; i < arrayObject.length; i++) {    /* ������'arrayObject[i]'������������������������ */}
  2. 'foreach'������

    for (elementType elementVarName : arrayObject) {    /* ������'elementVarName'������������������������ */}

���������

  • ���������������������������������������������������������������������������������������������������������������'for'������������'foreach'������������������'for'������������

  • 'foreach'������������������������������������������������������

���������

/* ������������������������������������������1 2 3 4 5 1 2 3 4 5 */public static void main(String[] args) {    int[] intArr = {1, 2, 3, 4, 5};    /* for������������������ */    for (int i = 0; i < intArr.length; i++) {        System.out.println(intArr[i]);    }    /* foreach������������������ */    for (int element : intArr) {        System.out.println(element);    }}


������������������

���������

  1. ������

    innerElementType[][] arrayVarName
  2. ������

    /* ��������������������������� */new elementType[outerArrayLength][innerArrayLength]/* ��������������������������� */{{innerFirstElementValue, innerSecondElementValue, innerThirdElementValue...},{innerFirstElementValue, innerSecondElementValue, innerThirdElementValue...},{innerFirstElementValue, innerSecondElementValue, innerThirdElementValue...}...}
  3. ������������

    /* ������������������������ */arrayObject.length/* ��������������������������������������������������� */arrayObject[outerIndex].length
  4. ������������

    /* ��������������������������������������������������������������� */arrayObject[outerIndex]/* ��������������������������������������������������������������������������������� */arrayObject[outerIndex][innerIndex]

���������

  • ���������������������������������������������������������������������
  • ������������������������������������������������������������������������������������������������������������������������������������������������

���������

/* ������������������������������������������1 2 2 3 3 4 4 5 5 6 */public static void main(String[] args) {    int[][] arrayObject = {{1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}};    /* ������foreach������������������������ */    for (int[] innerArr : arrayObject) {        for (int innerElement : innerArr) {            System.out.println(innerElement);        }    }}


上一篇:Java常用类:Arrays类
下一篇:Java基本概念:方法

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2025年05月04日 15时04分24秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章