how2j.cn


工具版本兼容问题
数组是一个固定长度的,包含了相同类型数据的 容器


本视频是解读性视频,所以希望您已经看过了本知识点的内容,并且编写了相应的代码之后,带着疑问来观看,这样收获才多。 不建议一开始就观看视频



8分35秒
本视频采用html5方式播放,如无法正常播放,请将浏览器升级至最新版本,推荐火狐,chrome,360浏览器 如果装有迅雷,播放视频呈现直接下载状态,请调整 迅雷系统设置-基本设置-启动-监视全部浏览器 (去掉这个选项)



步骤 1 : 声明数组   
步骤 2 : 创建数组   
步骤 3 : 访问数组   
步骤 4 : 数组长度   
步骤 5 : 练习-数组最小值   
步骤 6 : 答案-数组最小值   

int[] a; 声明了一个数组变量。
[]表示该变量是一个数组
int 表示数组里的每一个元素都是一个整数
a 是变量名
但是,仅仅是这一句声明,不会创建数组

有时候也会写成int a[]; 没有任何区别,就是你看哪种顺眼的问题
public class HelloWorld { public static void main(String[] args) { // 声明一个数组 int[] a; } }
public class HelloWorld {
	public static void main(String[] args) {
		// 声明一个数组
		int[] a;
	}
}
创建数组的时候,要指明数组的长度。
new int[5]
引用概念:
如果变量代表一个数组,比如a,我们把a叫做引用
与基本类型不同
int c = 5; 这叫给c赋值为5
声明一个引用 int[] a;
a = new int[5];
让a这个引用,指向数组
创建数组
public class HelloWorld { public static void main(String[] args) { //声明一个引用 int[] a; //创建一个长度是5的数组,并且使用引用a指向该数组 a = new int[5]; int[] b = new int[5]; //声明的同时,指向一个数组 } }
public class HelloWorld {
	public static void main(String[] args) {
		//声明一个引用
		int[] a; 
		//创建一个长度是5的数组,并且使用引用a指向该数组
		a = new int[5];
		
		int[] b = new int[5]; //声明的同时,指向一个数组
		
	}
}
数组下标基0
下标0,代表数组里的第一个数
访问数组
public class HelloWorld { public static void main(String[] args) { int[] a; a = new int[5]; a[0]= 1; //下标0,代表数组里的第一个数 a[1]= 2; a[2]= 3; a[3]= 4; a[4]= 5; } }
public class HelloWorld {
	public static void main(String[] args) {
		int[] a; 
		a = new int[5];
		
		a[0]= 1;  //下标0,代表数组里的第一个数
		a[1]= 2;
		a[2]= 3;
		a[3]= 4;
		a[4]= 5;
	}
}
.length属性用于访问一个数组的长度
数组访问下标范围是0到长度-1
一旦超过这个范围,就会产生数组下标越界异常
数组长度
public class HelloWorld { public static void main(String[] args) { int[] a; a = new int[5]; System.out.println(a.length); //打印数组的长度 a[4]=100; //下标4,实质上是“第5个”,即最后一个 a[5]=101; //下标5,实质上是“第6个”,超出范围 ,产生数组下标越界异常 } }
public class HelloWorld {
	public static void main(String[] args) {
		int[] a; 
		a = new int[5];
		
		System.out.println(a.length); //打印数组的长度
		
		a[4]=100; //下标4,实质上是“第5个”,即最后一个 
		a[5]=101; //下标5,实质上是“第6个”,超出范围 ,产生数组下标越界异常
		
	}
}
步骤 5 :

练习-数组最小值

练习难度 edit edit Or  姿势不对,事倍功半! 点击查看做练习的正确姿势
首先创建一个长度是5的数组
然后给数组的每一位赋予随机整数
通过for循环,遍历数组,找出最小的一个值出来

0-100的 随机整数的获取办法有多种,下面是参考办法之一:

(int) (Math.random() * 100)

Math.random() 会得到一个0-1之间的随机浮点数,然后乘以100,并强转为整型即可。
public class HelloWorld { public static void main(String[] args) { int[] a = new int[5]; a[0] = (int) (Math.random() * 100); a[1] = (int) (Math.random() * 100); a[2] = (int) (Math.random() * 100); a[3] = (int) (Math.random() * 100); a[4] = (int) (Math.random() * 100); System.out.println("数组中的各个随机数是:"); for (int i = 0; i < a.length; i++) System.out.println(a[i]); System.out.println("本练习的目的是,找出最小的一个值: "); } }
public class HelloWorld {
	public static void main(String[] args) {
		int[] a = new int[5];
		a[0] = (int) (Math.random() * 100);
		a[1] = (int) (Math.random() * 100);
		a[2] = (int) (Math.random() * 100);
		a[3] = (int) (Math.random() * 100);
		a[4] = (int) (Math.random() * 100);
		
		System.out.println("数组中的各个随机数是:");
		for (int i = 0; i < a.length; i++) 
			System.out.println(a[i]);
		
		System.out.println("本练习的目的是,找出最小的一个值: ");
	}
}
步骤 6 :

答案-数组最小值

edit edit
在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多
在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多
在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多
查看本答案会花费3个积分,您目前总共有点积分。查看相同答案不会花费额外积分。 积分增加办法 或者一次性购买JAVA 基础总计0个答案 (总共需要0积分)
查看本答案会花费3个积分,您目前总共有点积分。查看相同答案不会花费额外积分。 积分增加办法 或者一次性购买JAVA 基础总计0个答案 (总共需要0积分)
账号未激活 账号未激活,功能受限。 请点击激活


HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。


提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
关于 JAVA 基础-数组-创建数组 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 982790551
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: https://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图