13_数组

数组

概念:一组连续的存储空间,存储多个相同数据类型的值

特点

  1. 类型相同

  2. 长度固定

  • 数组中的每个数据格被称为“数组元素”
  • 对每个元素进行赋值或取值的操作被称为“元素的访问”
  • 访问元素时,需要使用“下标”(从0开始,依次+1,自动生成)

访问的语法

赋值:数组名[下标] = 值;//a[0] = 10;

取值:数组名[下标];//a[0];

数组的遍历:

从头至尾,逐一对数组的每个元素进行访问

题目一:创建长度为5的数组并遍历

public class TestCreateArray{
	
	public static void main(String[] args){
		
		int[] a = new int[5];//在内存中创建长度为5的整数数组
		
		a[0] = 1;
		a[1] = 2;
		a[2] = 3;
		a[3] = 4;
		a[4] = 5;
		
		System.out.println(a[0]);
		System.out.println(a[1]);
		System.out.println(a[2]);
		System.out.println(a[3]);
		System.out.println(a[4]);
		
		for(int i = 0; i < a.length; i++){
			System.out.println(a[i]);
		}
		
	}
}

数组默认值:

  • 整数:0
  • 小数:0.0
  • 字符:\u0000
  • 布尔:false
  • 其他:null

题目二:数组默认值

public class TestDefaultValue{
	
	public static void main(String[] args){
		
		int[] a = new int[5];
		
		for(int i = 0; i < a.length; i++){
			System.out.println(a[i]);
		}
		
		double[] b = new double[5];
		
		for(int i = 0; i < b.length; i++){
			System.out.println(b[i]);
		}
		
		String[] strs = new String[4];
		
		for(int i = 0; i < strs.length; i++){
			System.out.println(strs[i]);
		}
	}
}

数组创建语法:

  1. 先声明、再分配空间:
  • 数据类型[] 数组名;
  • 数组名 = new 数组类型[长度];
  1. 声明并分配空间:
  • 数据类型[] 数组名 = new 数组类型[长度];
  1. 声明并赋值(繁):
  • 数据类型[] 数组名 = new 数据类型[]
  1. 声明并赋值(简):
  • 数据类型[] 数组名 = {value1, value2, value3, ...}//显示初始化,不可换行

题目三:创建数组的方式

public class TestCreates{
	
	public static void main(String[] args){
		
		//1.先声明、再分配空间
		int[] array1;
		
		array1 = new int[4];
		
		System.out.println(array1[0]);
		
		//2.声明并分配空间
		int[] array2 = new int[4];
		
		System.out.println(array2[0]);
		
		//3.声明并赋值(繁)
		int[] array3 = new int[]{1, 2, 3};
		
		for(int i = 0; i < array3.length; i++){
			System.out.println(array3[i]);
		}
		
		//4.声明并赋值(简)
		
		int[] array4 = {1, 2, 3, 4};//不支持换行书写
		
		for(int i = 0; i < array4.length; i++){
			System.out.println(array4[i]);
		}
	}
}

题目四:求数组中元素的平均数

public class TestGetAvg{
	
	public static void main(String[] args){
		
		int[] numbers = new int[]{55,66,77,88,99};
		
		int sum = 0;
		
		for(int i = 0; i < numbers.length; i++){
			sum += numbers[i];
		}
		double avg = sum / numbers.length;
		
		System.out.println("平均数为:" + avg);
	}
}

题目五:查找数组元素

import java.util.Scanner;

public class TestSearch{
	
	public static void main(String[] args){
		
		Scanner input = new Scanner(System.in);
		
		System.out.println("请输入一个整数:");
		
		int n = input.nextInt();
		
		int[] numbers = new int[]{1,2,3,4,5,6,7};
		
		int index = -1;
		
		for(int i = 0; i < numbers.length; i++){
			if(numbers[i] == n){
				index = i;
				break;
			}
		}
		
		System.out.println(index);
	}
}

题目六:数组的排序

JDK排序:java.util.Arrays.sort(数组名);//JDK提供(升序)

import java.util.Arrays;

public class TestSort{
	
	public static void main(String[] args){
		
		int[] nums = new int[]{4,1,3,2,5};
		
		//借助JDK提供的数组工具,进行排序(升序)
		Arrays.sort(nums);
		
		//第一次遍历(升序)
		for(int i = 0; i < nums.length; i++){
			System.out.println(nums[i]);
		}
		
		//降序:需要手工的方式完成元素的倒置 1 2 3 4 5
		
		for(int i = 0; i < nums.length / 2; i++){
			int temp = nums[i]; 
			
			nums[i] = nums[nums.length - 1 - i];
			
			nums[nums.length - 1 -i] = temp;
			
		}
		//第二次遍历(降序)
		for(int i = 0; i < nums.length; i++){
			System.out.println(nums[i]);
		}
	}
}
posted @ 2021-02-02 16:14  MARSdoubleZ  阅读(53)  评论(0编辑  收藏  举报