数组笔记
来源:互联网 发布:linux端口映射 命令 编辑:程序博客网 时间:2024/06/10 12:03
数组:
概念:一组同一类型的数据变量的集合
数组声明:
声明:int[] arr;//不能使用//常用
开辟内存空间:a = new int[5];//给a开辟5个内存空间
声明:int[] a1 = new int[5];//给a1开辟5个内存空间//常用
声明:int[] a2 = new int[]{10,23,45,65,43};//给a2开辟5个内存空间并赋初始值
声明:int[] a3 = {10,23,45,65,43};//上面形式的简写//常用
使用数组注意的两个问题
空指针异常
String[] str = null;
System.out.print(str);//提示NullPointerException
数组越界异常
String[] str = String[3];
System.out.print(str[3]);//提示ArrayIndexOutofBoundException
内存
栈区:
存放的数据大小固定
变量名存放在栈区
栈区存放变量的地址指向堆区的数据
堆区:
存放不固定大小的数据
变量存放在堆区
增强for循环:
//数据类型必须与数组的数据类型一致
for(数据类型 变量名:数组){
System.out.print(变量名+"\t");
}
复制一个数组 4种方法
只复制地址
int[] a = {10,20,30};
int[] b = a;
tips:两个数组变量的内存地址相同,都指向同一个堆内存空间,其中一个更改,两个都会更改
复制值:
int[] c = new int[a.length];
for(int i = 0;i < a.length;i++){
c[i] = a[i];
}
方法复制
a.clone()
System.arraycopy()
数组的几个常用方法:
public class Demo {
//打印数组
public static void printArray(int[] a1){
for(int i = 0; i<a1.length;i++){
System.out.print(a1[i] +"\t");
}
System.out.println();
}
//折半查找(必须是有序列表)
public static int getBinaryQuery(int[] a1 ,int value){
//找到三个索引
//最大索引
int maxIndex = a1.length-1;
//最小索引
int minIndex = 0;
//中间索引
int midIndex = (maxIndex+minIndex)/2;
//循环条件为直到找到这个数为止
while(a1[midIndex] != value){
//如果为最大索引的数组的值比要找的值大
if(a1[midIndex]>value){
maxIndex = midIndex-1;
//如果为最大索引的数组的值比要找的值大
}else{
minIndex = midIndex+1;
}
//更新中间索引
midIndex = (maxIndex+midIndex)/2;
}
return midIndex;
}
//数组的倒置
public static void reverseArray(int[] a1){
//找对称轴 对称轴为array.length/2
//array.length/2会自动向下取整
for(int i = 0;i<a1.length/2;i++){
int temp = a1[a1.length-1-i];
a1[a1.length-1-i] = a1[i];
a1[i] = temp;
}
}
public static void main(String[] args){
int[] a1 = {1,2,3,4,5};
reverseArray(a1);
printArray(a1);
System.out.println(getBinaryQuery(a1,4));
}
}
概念:一组同一类型的数据变量的集合
数组声明:
声明:int[] arr;//不能使用//常用
开辟内存空间:a = new int[5];//给a开辟5个内存空间
声明:int[] a1 = new int[5];//给a1开辟5个内存空间//常用
声明:int[] a2 = new int[]{10,23,45,65,43};//给a2开辟5个内存空间并赋初始值
声明:int[] a3 = {10,23,45,65,43};//上面形式的简写//常用
使用数组注意的两个问题
空指针异常
String[] str = null;
System.out.print(str);//提示NullPointerException
数组越界异常
String[] str = String[3];
System.out.print(str[3]);//提示ArrayIndexOutofBoundException
内存
栈区:
存放的数据大小固定
变量名存放在栈区
栈区存放变量的地址指向堆区的数据
堆区:
存放不固定大小的数据
变量存放在堆区
增强for循环:
//数据类型必须与数组的数据类型一致
for(数据类型 变量名:数组){
System.out.print(变量名+"\t");
}
复制一个数组 4种方法
只复制地址
int[] a = {10,20,30};
int[] b = a;
tips:两个数组变量的内存地址相同,都指向同一个堆内存空间,其中一个更改,两个都会更改
复制值:
int[] c = new int[a.length];
for(int i = 0;i < a.length;i++){
c[i] = a[i];
}
方法复制
a.clone()
System.arraycopy()
数组的几个常用方法:
public class Demo {
//打印数组
public static void printArray(int[] a1){
for(int i = 0; i<a1.length;i++){
System.out.print(a1[i] +"\t");
}
System.out.println();
}
//折半查找(必须是有序列表)
public static int getBinaryQuery(int[] a1 ,int value){
//找到三个索引
//最大索引
int maxIndex = a1.length-1;
//最小索引
int minIndex = 0;
//中间索引
int midIndex = (maxIndex+minIndex)/2;
//循环条件为直到找到这个数为止
while(a1[midIndex] != value){
//如果为最大索引的数组的值比要找的值大
if(a1[midIndex]>value){
maxIndex = midIndex-1;
//如果为最大索引的数组的值比要找的值大
}else{
minIndex = midIndex+1;
}
//更新中间索引
midIndex = (maxIndex+midIndex)/2;
}
return midIndex;
}
//数组的倒置
public static void reverseArray(int[] a1){
//找对称轴 对称轴为array.length/2
//array.length/2会自动向下取整
for(int i = 0;i<a1.length/2;i++){
int temp = a1[a1.length-1-i];
a1[a1.length-1-i] = a1[i];
a1[i] = temp;
}
}
public static void main(String[] args){
int[] a1 = {1,2,3,4,5};
reverseArray(a1);
printArray(a1);
System.out.println(getBinaryQuery(a1,4));
}
}
阅读全文
0 0
- 数组笔记
- 数组笔记
- 数组笔记
- 数组笔记
- 数组笔记
- 【笔记】数组
- 笔记:数组
- 数组笔记
- java语言数组笔记
- C++笔记(数组)
- SqlParameter数组 使用笔记
- PHP数组回忆笔记
- 笔记 :定义二维数组
- 许智磊后缀数组笔记
- C++学习笔记--数组
- php 中的数组(笔记)
- FORTRAN的数组-笔记
- c#框架笔记--数组
- “后起之秀”——Kotlin
- Python:聚合函数(groupby)
- 23-IO流(演示键盘录入) 24-IO流(读取键盘录入) 25-IO流(转换流)123 26-IO流(转换流-需求演示)
- 如何解决python3中numpy报错No module named 'numpy'
- 修改linux中的网卡名
- 数组笔记
- 字符串文件流的实现
- (一)Node.js写入文件中文乱码问题
- Multisim的探针(Probe)及数字电路的一些调试技巧的应用
- 学习笔记6
- Servlet 文件上传
- centos7.2重新安装yum
- PTA 6-1 顺序表基本操作(10 分)
- 高德地图小实现