Java中数组的应用

数组

数组可以存放多个同一类型的数据。数组引用数据类型。即:数(数据)组(一组)就是一组数据。

数组的使用

使用方式1-动态初始化

数组的定义:

数据类型 数组名[ ] = new 数据类型[大小]
(也可以 数据类型[ ] 数组名 = new 数据类型[大小])
例如:int a[] = new int[5]; //创建了一个数组,名字为a,存放5个int型
在这里插入图片描述

数组的引用(使用/访问/获取数组元素):

数组名[下标/索引/index],数组的下标从0开始
如:要使用a数组的第3个数-----a[2]

快速入门案例

循环输入5个成绩,保存到double数组,并输出

import java.util.Scanner;
public class test{
	public static void main(String[] args){
	
	double scores[] = new double[5];
	Scanner sc = new Scanner(System.in);
	
	//循环输入
	for(int i = 0; i < scores.length; i++){
		System.out.println("请输入第" + (i+1) + "个成绩:");
		scores[i] = sc.nextDouble();
	}

	//输出,遍历数组
	for (int i = 0; i < scores.length; i++ ) {
		System.out.println("第" + i + "个成绩为:" + scores[i]);
		
	}

	}
}

使用方式2-动态初始化

先声明数组

语法:

数据类型 数组名[];  //数据类型[] 数组名;
int a[]; //int[] a;
再创建数组

语法:

数组名 = new 数据类型[大小];
a = new int[10];
使用方式1和2的比较
	//1.第一种动态分配方式
	double scores[] = new double[5];
	
	//2.第二种动态分配方法
	//先声明数组,再 new 分配空间
	double scores[];  //声明数组,此时scores是null
	scores = new double[5];  //分配内存空间,可以存放数据

使用方式3-静态初始化

初始化数组
数据类型 数据名[] = {元素值、元素值...};

int a[ ] = {2,5,9,12,47,489,12};
如果知道数组有多少元素和具体值可用
上面的用法相当于:

int a[] = new int[9];
a[0] = 2; a[1] = 5; a[2] = 9;  ....
快速入门案例

一个养鸡场有6只鸡,它们的体重分别是3kg、5kg、1kg、3.4kg、2kg、50kg。请问这六只鸡的总体重是多少?平均体重是多少?

public class test{
	public static void main(String[] args){
	
	//遍历数组得到数组的所有元素的和,使用for
	//1.通过hens[下标] 来访问数组的元素
	//下标是从0开始编号的,比如第一个元素就是hen[0]
	//第二个元素就是hen[1],以此类推
	//2.使用一个变量totalWeight将各个元素积累

	double[] hens = {3,5,1,3.4,2,50};
	double totalWeight = 0;
	
	for(int i = 0; i < 6; i++){
		totalWeight += hens[i];
	}

	System.out.println("总体重 = " + totalWeight +
		"\n平均体重 = " + totalWeight / 6);

	}
}

先死后活,若我随机改变数组长度

public class test{
	public static void main(String[] args){
	
	**//可以通过 数组名.length 得到数组长度**
	double[] hens = {3,5,1,3.4,2,50,88.8};
	double totalWeight = 0;
	System.out.println("数组长度为:" + hens.length);
	for(int i = 0; i < hens.length; i++){
		totalWeight += hens[i];
	}

	System.out.println("总体重 = " + totalWeight +
		"\n平均体重 = " + totalWeight / hens.length);

	}
}

注意事项和细节

1.数组是多个相同类型数据的组合,实现对这些数据的统一管理。

int [] arr1 = {1,2,3,60,"hello"}; //String->int
double[] arr2 = {1.1,2.2,100};  //int -> double

2.数组中的元素可以是任何数据类型,包括基本类型和引用类型,但不能混用。

String[] arr3 = {"北京","jack","money"};

3.数组创建后,如果没有赋值,有默认值
int 0, short 0, byte 0, long 0
float 0.0, double 0.0
char \u0000
boolean false
String null
4.使用数组的步骤:1)声明数组并开辟空间;2)给数组各个元素赋值;3)使用数组
5.数组的下标是从0开始的。
6.数组的下标必须在指定范围内使用,否则报:下标越界异常,如:
int [ ] arr = new int[5];则有效下标为0-4
数组的下标最小0,最大 (数组长度-1)
7.数组属于引用类型,数组型数据是对象(object)

练习题

创建一个char类型的26个元素的数组,分别放置’A’-‘Z’,并打印

public class test{
	public static void main(String[] args){
	
	char arr[] = new char[26];
	// Scanner sc = new Scanner(System.in);

	for(char i = 'A'; i <= 'Z'; i++){
		System.out.print(i + " ");
	}

	}
}
public class test{
	public static void main(String[] args){
	
	char arr[] = new char[26];
	// Scanner sc = new Scanner(System.in);

	for(int i = 0; i < arr.length; i++){
		arr[i] = (char)('A' + i); //'A' + 1 是int型,需要强制转换
		// System.out.print(i + " ");
	}

	for(int i = 0; i < arr.length; i++){
		System.out.print(arr[i] + " ");
	}
	}
}

求出一个数组int[ ]的最大值{4,-1,9,10,23},并得到对应的下标。

public class test{
	public static void main(String[] args){
	
	//假定 max = arr[0]是最大值,maxIndex=0
	//从下标 1 开始遍历arr,如果max<当前元素,
	//说明max不是真正的最大值,我们就 max=当前元素,maxIndex=当前下标
	//遍历数组后max就是真正的最大值
	int arr[] = {4,-1,9,10,23};
	int max = arr[0];
	int maxIndex = 0;
	for (int i = 1; i < arr.length; i++ ) {
		if(arr[i] > max){
			max = arr[i];
			maxIndex = i;
		}
	}
	System.out.println("最大值为:" + max + "下标为:" + maxIndex);


	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值