1.1 数组

本文详细介绍了Java中数组的概念、定义方式及内存分配,并讲解了数组初始化、遍历等常见操作,帮助初学者掌握数组的基本用法。

数组用于存储多个相同类型数据的存储模型

一、数组的定义格式

1、 int[] arr    定义了一个int类型的数组,数组名是arr

2、int  arr[]    定义了一个int类型的变量,变量是arr数组

二、动态初始化

int[] arr = new int[3];

三、数组元素访问

四、内存分配

Java中内存分配

int[] arr = new int[3];

int[] arr   //在 栈内存 ,栈内存存储局部变量 , 栈内存变量使用完毕,立即消失。

new int[3]  //在 堆内存 ,堆内存存储 new出来的内容(实体,对象) , 堆内存对象使用完毕,会在垃圾回收器空闲时被回收。

五、数据初始化之静态初始化

初始化指定每个数组元素的初始化值,由系统决定数组长度

int[] arr = new int[] {1, 2, 3}; --> 简化格式:   int[] arr = {1, 2, 3};

 

六、两个小问题:1、数组越界。2、空指针异常。

七、数据常见操作

遍历

int[] arr = {11, 22, 33, 44, 55};

for(int x=0; x < 5; x++) {

    System.out.println(arr[x]);

}

 

0/1 1.1-采用数组实现图书浏览/1.1-采用数组实现图书浏览.cpp: In function ‘void Print(SqList&)’: 1.1-采用数组实现图书浏览/1.1-采用数组实现图书浏览.cpp:8:2: error: ‘cout’ was not declared in this scope cout << "北京林业大学图书馆计算机类图书采购列表" << endl; ^~~~ 1.1-采用数组实现图书浏览/1.1-采用数组实现图书浏览.cpp:8:73: error: ‘endl’ was not declared in this scope cout << "北京林业大学图书馆计算机类图书采购列表" << endl; ^~~~ 1.1-采用数组实现图书浏览/1.1-采用数组实现图书浏览.cpp:8:73: note: suggested alternative: ‘enum’ cout << "北京林业大学图书馆计算机类图书采购列表" << endl; ^~~~ enum 1.1-采用数组实现图书浏览/1.1-采用数组实现图书浏览.cpp: In function ‘int main()’: 1.1-采用数组实现图书浏览/1.1-采用数组实现图书浏览.cpp:18:5: error: redefinition of ‘int main()’ int main(){ ^~~~ In file included from 1.1-采用数组实现图书浏览/1.1-采用数组实现图书浏览.cpp:1:0: 1.1-采用数组实现图书浏览/1.1.h:69:5: note: ‘int main()’ previously defined here int main() { ^~~~ 1.1-采用数组实现图书浏览/1.1-采用数组实现图书浏览.cpp:20:2: error: ‘string’ was not declared in this scope string filepath = "/data/workspace/myshixun/book.txt"; ^~~~~~ 1.1-采用数组实现图书浏览/1.1-采用数组实现图书浏览.cpp:20:2: note: suggested alternative: ‘stdin’ string filepath = "/data/workspace/myshixun/book.txt"; ^~~~~~ stdin 1.1-采用数组实现图书浏览/1.1-采用数组实现图书浏览.cpp:21:12: error: cannot convert ‘SqList’ to ‘SqList*’ for argument ‘1’ to ‘void InitList(SqList*)’ InitList(L); ^ 1.1-采用数组实现图书浏览/1.1-采用数组实现图书浏览.cpp:22:14: error: ‘filepath’ was not declared in this scope ReadFile(L, filepath); //读取文件 ^~~~~~~~ 1.1-采用数组实现图书浏览/1.1-采用数组实现图书浏览.cpp:22:14: note: suggested alternative: ‘realpath’ ReadFile(L, filepath); //读取文件 ^~~~~~~~ realpath 1.1-采用数组实现图书浏览/1.1-采用数组实现图书浏览.cpp:24:12: error: cannot convert ‘SqList’ to ‘SqList*’ for argument ‘1’ to ‘void FreeList(SqList*)’ FreeList(L); ^
09-22
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值