您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

java中数组初始化的三种方式是什么

2024/6/21 23:39:46发布17次查看
java中数组初始化的三种方式是:1、静态初始化,如【int a[] = {2, 0, 1, 9, 2020}】;2、动态初始化,如【int[] c = new int[4]】;3、默认初始化,如【int[]d=new int[5]】。
初始化方式:
1、静态初始化
2、动态初始化
3、默认初始化
(学习视频分享:java课程)
代码示例:
import org.junit.test;//import org.testng.annotations.parameters;public class demo { @test public void test() { //数组的静态初始化 int a[] = {2, 0, 1, 9, 2020}; system.out.println("----------静态初始化----------"); getarray(a); //数组的动态初始化方法一 int[] b = new int[10]; for (int i = 0; i < b.length; i++) { b[i] = i; } system.out.println("----------动态初始化1----------"); getarray(b); //数组的动态初始化方法二 int[] c = new int[4]; c[1] = 2; c[2] = 0; c[3] = 9; system.out.println("----------动态初始化2----------"); getarray(c); //数组的默认初始化,默认初始化只指定数组的长度,数组里面每个元素的值都是默认值 int[]d=new int[5]; system.out.println("----------默认初始化----------"); getarray(d); } public void getarray(int[]arr){ for (int j:arr){ system.out.println(j); } }}
相关推荐:java入门
以上就是java中数组初始化的三种方式是什么的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product