Java数组-初识数组

news/2025/3/14 1:13:49/

签名:但行好事,莫问前程。

文章目录

  • 前言
  • 一、数组的概述
    • 1、数组中的概念
    • 2、数组的特点
  • 二、数组的声明与初始化
    • 1、先声明后初始化
    • 2、声明的同时直接初始化
    • 3、静态初始化数组
    • 4、动态初始化数组
  • 三、数组的赋值与取值
    • 1、给数组的元素赋值
    • 2、遍历数组的元素
  • 四、各种数据类型的数组的默认初始化值
  • 总结


前言

记录一下数组的学习。


一、数组的概述

数组(Array):是多个相同类型数据按照一定顺序排列的集合,并使用一个名字命名,并且通过编号的方式对这些数据进行统一管理。

1、数组中的概念

  • 数组名
  • 索引
  • 元素
  • 长度
    在这里插入图片描述

2、数组的特点

  • 数组本身是“引用数据类型”,数组中的元素可以是:基本数据类型和引用类型。
  • 创建数组对象会在内存中开辟一整块连续的内存空间,空间的大小取决于数组的长度和元素的类型。
  • 数组中的元素是依次紧密排列的,有序的。
  • 数组一旦初始化完成,其长度就是确定的。
  • 可以通过索引的方式调用指定位置的元素。

二、数组的声明与初始化

1、先声明后初始化

public class ArrayTest {public static void main(String[] args) {// 声明数组int[] intArray ;// 初始化数组intArray= new int[]{1, 2, 3, 4, 5};}
}

2、声明的同时直接初始化

public class ArrayTest {public static void main(String[] args) {// 声明数组并初始化int[] intArray = new int[]{1, 2, 3, 4, 5};}
}

3、静态初始化数组

静态初始化:在创建数组时,直接在中括号里面放入数组元素

public class ArrayTest {public static void main(String[] args) {// 声明数组并初始化(静态初始化)int[] intArray = new int[]{1, 2, 3, 4, 5};}
}

4、动态初始化数组

动态初始化:创建数组数组,仅指定数组长度,数组元素不确定

public class ArrayTest {public static void main(String[] args) {// 声明数组并初始化(动态初始化)int[] intArray = new int[5];}
}

三、数组的赋值与取值

1、给数组的元素赋值

用索引给每个指定的元素赋值:索引的范围(0 ~ 数组长度-1)

public class ArrayTest {public static void main(String[] args) {// 声明数组并初始化(动态初始化)int[] intArray = new int[5];// 给数组赋值intArray[0] = 1;intArray[1] = 2;intArray[2] = 3;intArray[3] = 4;intArray[4] = 5;}
}

2、遍历数组的元素

通过索引取出每个元素:

public class ArrayTest {public static void main(String[] args) {// 声明数组并初始化(动态初始化)int[] intArray = new int[5];// 给数组赋值intArray[0] = 1;intArray[1] = 2;intArray[2] = 3;intArray[3] = 4;intArray[4] = 5;// 遍历数组元素for (int i = 0; i < intArray.length; i++) {System.out.println(intArray[i]);}}
}

运行结果:
在这里插入图片描述

四、各种数据类型的数组的默认初始化值

public class ArrayTest {public static void main(String[] args) {byte[] bytes = new byte[1];System.out.println("byte数组默认初始化值:" + bytes[0]);short[] shorts = new short[1];System.out.println("short数组默认初始化值:" + shorts[0]);int[] ints = new int[1];System.out.println("int数组默认初始化值:" + ints[0]);long[] longs = new long[1];System.out.println("long数组默认初始化值:" + longs[0]);float[] floats = new float[1];System.out.println("float数组默认初始化值:" + floats[0]);double[] doubles = new double[1];System.out.println("double数组默认初始化值:" + doubles[0]);char[] chars = new char[1];System.out.println("char数组默认初始化值:" + chars[0]);boolean[] booleans = new boolean[1];System.out.println("boolean数组默认初始化值:" + booleans[0]);String[] strings = new String[1];System.out.println("引用类型数组默认初始化值:" + strings[0]);}
}

运行结果:
在这里插入图片描述


总结

博客主要记录了数组的学习,有啥错误或不足地方请指正,如果对你有所帮助,请一键三连。


http://www.ppmy.cn/news/1339180.html

相关文章

Camera2+OpenGL ES+MediaCodec+AudioRecord实现录制音视频写入H264 SEI数据

记录一下学习过程&#xff0c;得到一个需求是基于Camera2OpenGL ESMediaCodecAudioRecord实现录制音视频。 需求&#xff1a; 在每一帧视频数据中&#xff0c;写入SEI额外数据&#xff0c;方便后期解码时获得每一帧中的自定义数据。点击录制功能后&#xff0c;录制的是前N秒至…

MySQL基础(三)-学习笔记

一.innodb引擎&#xff1a; 1). 表空间&#xff1a;表空间是InnoDB存储引擎逻辑结构的最高层&#xff0c;启用了参数 innodb_file_per_table(在 8.0版本中默认开启) &#xff0c;则每张表都会有一个表空间&#xff08;xxx.ibd&#xff09;&#xff0c;一个mysql实例可以对应多个…

mac电脑风扇控制软件:Macs Fan Control Pro for mac 激活版

Macs Fan Control 是一款专门为 Mac 用户设计的软件&#xff0c;它可以帮助用户控制和监控 Mac 设备的风扇速度和温度。这款软件允许用户手动调整风扇速度&#xff0c;以提高设备的散热效果&#xff0c;减少过热造成的风险。 Macs Fan Control 可以在菜单栏上显示当前系统温度和…

记一次某竞赛中的渗透测试(Windows Server 2003靶机漏洞)

靶机简介 Windows Server 2003是微软公司于2003年3月28日发布的服务器操作系统&#xff0c;它基于Windows XP/Windows NT 5.1进行开发&#xff0c;并在同年4月底上市。以下是关于Windows Server 2003的详细介绍&#xff1a; 系统名称与发布历程&#xff1a; 该产品最初被命名为…

【操作系统和计网从入门到深入】(八)线程

前言 这个专栏其实是博主在复习操作系统和计算机网络时候的笔记&#xff0c;所以如果是博主比较熟悉的知识点&#xff0c;博主可能就直接跳过了&#xff0c;但是所有重要的知识点&#xff0c;在这个专栏里面都会提到&#xff01;而且我也一定会保证这个专栏知识点的完整性&…

洗地机哪个牌子好性价比高又实惠?全能型洗地机推荐

近年来&#xff0c;在家居清洁工具中&#xff0c;洗地机出镜率非常高&#xff0c;是我们生活中常备的清洁工具之一&#xff0c;尤其在繁忙的工作之余&#xff0c;还要给家里做大扫除清洁的人儿。然而&#xff0c;市面上的洗地机品牌繁多&#xff0c;产品功能各异&#xff0c;如…

非内积级联学习

1.首页推荐非内积召回现状 非内积召回源是目前首页推荐最重要的召回源之一。同时非内积相比于向量化召回最终仅将user和item匹配程度表征为embeding内积&#xff0c;非内积召回仅保留item embedding&#xff0c;不构造user显式表征&#xff0c;而是通过一个打分网络计算用户-商…

一、冯诺依曼计算机

图灵与冯诺依曼两位计算机发展重要人物。冯诺依曼简介&#xff1a;被誉为现代计算机之父。 世界上第一台通用计算机Eniac&#xff0c;就有冯诺依曼的参与。他提出的思想&#xff0c;将数据和程序分离开了&#xff0c;程序是程序&#xff0c;数据是数据&#xff0c;数据可以由程…