DFshmily の 博客园|

DFshmily

园龄:3年3个月粉丝:4关注:1

数组串讲

数组

一、内容

  • 数组的概念

  • 数组的创建方式

  • 数组的赋值与取值

  • 数组的遍历

二、数组的概念

数组是内存当中所开辟的一串连续的空间

特点:

  1. 连续空间

  2. 数据类型保持一致

  3. 长度固定

  4. 不能越界操作

三、数组的创建方式

语法:数据类型[]  数组名 = new  数据类型[长度];

申明方式:

  • 先申明再赋值
int[] is;
is = new int[10];
  • 边申明边赋值
int[] is = new int[10];
  • 连续申明赋值
int[] is1 , int[] is2 ;

注意:

数组的长度是固定的,在创建数组的时候就需要确定好。一旦确定好,就无法发生改变。

案例

public class CreateArray {
	public static void main(String[] args) {
		//基本数据类型
		byte[] bs = new byte[10];
		short[] sts = new short[10];
		int[] is = new int[10];
		long[] ls = new long[10];
		
		float[] fs = new float[10];
		double[] ds = new double[10];
		
		char[] cs = new char[10];
		boolean[] bls = new boolean[10];
		
		//引用数据类型
		String[] strs = new String[10];
	}
}

四、数组的赋值和取值

  • 赋值

根据数组的游标进行赋值

注意:

  • 数组的游标从0开始
  • 数组的最大游标为[数组长度-1]
  • 如果游标超出范围,会出现游标越界异常
int[] is = new int[10];
is[0] = 100;
is[is.length - 1] = 900;

游标越界:

public class IndexOutBounds {
	public static void main(String[] args) {
		//游标越界
		int[] is = new int[10];
		is[-1] = 100;
		is[10] = 900;
	}
}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
	at com.qf.demo01.IndexOutBounds.main(IndexOutBounds.java:7)
  • 取值

数组的取值同样是根据游标获取

int[] is = new int[10];
is[0] = 100;
int i = is[0];//取值后放入一个变量备用
  • 数组的静态申明方式

  • 在同一行内申明并赋值

  • 可以先申明再赋值

  • 目的:是为了给初始化的数组一个预设值

public class CreateStaticArrays {
	public static void main(String[] args) {
		//数组的静态申明
		//让数组拥有预设值
		//只能在同一行申明并赋值
		int[] is1 = {10,20,30};
		//可以先申明再赋值
		int[] is2;
		is2 = new int[]{10,20,30};
	}
}
  • 数组的预设值

三大类

  • 数值:整数的预设值为0 , 浮点数的预设值为0.0
  • 字符和布尔类型:字符是空格,布尔类型是false
  • 引用类型:默认值null
public class DefaultArrayItem {
	public static void main(String[] args) {
		//基本数据类型
		byte[] bs = new byte[10];
		short[] sts = new short[10];
		int[] is = new int[10];
		long[] ls = new long[10];
		System.out.println(ls[0]);
		
		float[] fs = new float[10];
		double[] ds = new double[10];
		System.out.println(ds[0]);
		
		char[] cs = new char[10];
		System.out.println(cs[0]);
		boolean[] bls = new boolean[10];
		System.out.println(bls[0]);
		
		//引用数据类型
		String[] strs = new String[10];
		System.out.println(strs[0]);
	}
}

结果

0
0.0
[这是一个空格^_^]
false
null

五、数组的遍历

1.案例

public class ForeachArray {
	public static void main(String[] args) {
		//创建一个数组
		int[] is = {100,200,300,400,500};
		//常规遍历方式
		for(int index = 0 ; index < is.length ; index ++){
			int item = is[index];
			System.out.println(item);
		}
		//foreach
		for(int item : is){
			System.out.println(item);
		}
	}
}

2.差异

  1. 常规的遍历方式可以自由的定义游标起止位置,以及迭代方式

  2. foreach只能从数组的开始遍历到结尾,但是其语法更加的精炼

3.foreach同样可以维护游标

int index = 0;
for(int item : is){
    System.out.println(item);
    index ++;
}

4.可以一边遍历,一边往数组内赋值

Scanner sca = new Scanner(System.in);
System.out.print("请输入学生数量:");
int studentNumber = sca.nextInt();
//通过遍历数组进行赋值
double[] scores = new double[studentNumber];
for(int i = 0 ; i < scores.length ; i ++){
    System.out.print("请输入第"+(i+1)+"位学生的成绩:");
    scores[i] = sca.nextDouble();
}

本文作者:DFshmily

本文链接:https://www.cnblogs.com/DFshmily/p/17196207.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   DFshmily  阅读(10)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
展开