java代码如何声明和定义数组呢?
下文笔者讲述使用java代码声明和定义数组的方法及示例分享,如下所示
数组声明和定义方法
声明和定义数组的方法
定义方法: type [] arraryName = {*,*,*,*}; 赋值方法: type[0] = "***";例:
数组声明和定义方法
//原始类型数组 package com.java265; import java.util.Arrays; public class ArrayExample1 { public static void main(String[] args) { //定义数组 int[] num1 = new int[5]; int[] num2 = {1, 2, 3, 4, 5}; int[] num3 = new int[]{1, 2, 3, 4, 5}; //数组索引从0开始 num1[0] = 1; num1[1] = 2; num1[2] = 3; num1[3] = 4; num1[4] = 5; //打印数组 System.out.println(Arrays.toString(num1)); System.out.println(Arrays.toString(num2)); System.out.println(Arrays.toString(num3)); } } //对象类型数组 package com.java265; import java.util.Arrays; public class ArrayExample1 { public static void main(String[] args) { String[] str1 = new String[5]; String[] str2 = {"a", "b", "c", "d", "e"}; String[] str3 = new String[]{"a", "b", "c", "d", "e"}; str1[0] = "a"; str1[1] = "b"; str1[2] = "c"; str1[3] = "d"; str1[4] = "e"; System.out.println(Arrays.toString(str1)); System.out.println(Arrays.toString(str2)); System.out.println(Arrays.toString(str3)); } }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。