Java零基础(六)之数组

1.数组基础(重点)

1.1. 引入数组

//引言:为什么使用数组?
//需求: 请存储全班的成绩
//根据变量存: int score1=66;  int score2=68;  int score3=77; ...
//弊端: 60个学生成绩,则需要60个变量,冗余太多
//假设每个同学的成绩都+1分?  如何处理?
//弊端:每个变量,都需要做+1操作,不方便管理
//引入数组:
//概念:一组连续的内存空间,用于存储相同类型的元素
//数组特点:类型相同,长度固定

案例1

public class ArrayTest1 {
    
    
	public static void main(String[] args) {
    
    
		//需求: 请存储全班的成绩
		//数组是引用类型:往往需要先开辟空间,然后再存值
		int[] arr = new int[3]; //定义3个int类型的数组空间  
		//通过下标给数组元素赋值(存值)与取值   下标范围:0~长度-1
		arr[0] = 66;   //赋值
		arr[1] = 68;
		arr[2] = 77;
		//arr[3] = 99;  //ArrayIndexOutOfBoundsException: 数组越界异常
		//打印输出的优化---循环打印(遍历)
		System.out.println("数组长度:"+arr.length);  //3
		for(int i=0;i<arr.length;i++) {
    
     //i=0
			System.out.println(arr[i]);  //arr[0],arr[1]..
		}
		//System.out.println(arr[3]);  //ArrayIndexOutOfBoundsException
		
	}
}

案例2

//案例2:给每个学生成绩都+1
public class ArrayTest2 {
    
    
	public static void main(String[] args) {
    
    
		int[] arr = new int[3]; //定义3个int类型的数组空间  
		//通过下标给数组元素赋值(存值)与取值   下标范围:0~长度-1
		arr[0] = 66;   //赋值
		arr[1] = 68;
		arr[2] = 77;
		for(int i=0;i<arr.length;i++) {
    
    
			arr[i] += 1; //arr[i]=arr[i]+1;
			
			System.out.println(arr[i]);
		}
		
	}
}


1.2 数组的初始值与创建

数组的初始值

//数组的初始值问题:
public class Test1 {
    
    
	public static void main(String[] args) {
    
    
		int[] a = new int[3];  //创建3个存int值的空间
		System.out.println(a[0]);  //0   int类型初始为0
		
		double[] b = new double[3];
		System.out.println(b[0]);  //0.0 double类型初始值	
		
		boolean[] c = new boolean[3];
		System.out.println(c[0]);  //false
		
		char[] d = new char[3];
		System.out.println(d[0]);  //码值为0对应的字符
		
		String[] e = new String[3];
		System.out.println(e[0]);  //String类型初始为 null
	}
}

数组的创建

//数组的创建:
public class Test2 {
    
    
	public static void main(String[] args) {
    
    
		//----------动态赋值(有了空间,再一个个赋值)-----------
		//1.先声明,后创建空间
		int[] a;
		a = new int[3];
		
		//2.声明的同时,创建空间-(常用)
		int[] c = new int[3];
		
		//------------静态赋值(有了空间的同时赋值)-------------
		//3.创建空间的同时进行赋值
		int[] d = new int[]{
    
    1,3,5}; //数组的长度,由值的个数决定
		
		//4.创建空间的同时进行赋值(简化版-常用)
		int[] e = {
    
    1,3,5};
	}
}

1.3 数组练习

案例1

//案例1:给定一个整数数组,统计数组元素的平均分
//分析:先遍历数组的所有元素; 再求和   在除以长度
public class Test1 {
    
    
	public static void main(String[] args) {
    
    
		int[] a = {
    
    68,75,83,63};
		double sum = 0;  //求和,初始为0
		int len = a.length;
		for(int i=0;i<len;i++) {
    
    
			sum += a[i];
		}
		System.out.println("数组元素的平均分为:"+(sum/len));
	}
}

案例2

//案例2:给定一个整数数组,输入一个整数n,如果在数组中存在,则输出下标;否则打印-1
//例如:      7   3  8  2  6
//录入1   -1     录入8   2
public class Test2 {
    
    
	public static void main(String[] args) {
    
    
		int[] a = {
    
    7,3,8,2,6};
		Scanner sc = new Scanner(System.in);
		System.out.print("请输入一个值:");
		int n = sc.nextInt();
		int index = -1;  //记录下标
		for(int i=0;i<a.length;i++) {
    
     //0
			if(n==a[i]) {
    
    
				index = i;  //如果匹配上,进来了,得到了下标
				break;
			}
		}
		System.out.println("下标:"+index);
	}
}

2. 数组扩容

//数组扩容:
//从前面的数组特点中可知,数组的长度是固定的
//如果需要给数组扩容的话,需要重新创建空间
//扩容思路是: 创建一个比原数组更大的空间,再将原数组元素拷贝进来

2.1 自定义数组扩容

public class Test1 {
    
    
	public static void main(String[] args) {
    
    
		int[] src = {
    
    1,3,5};
		int[] dest = new int[src.length+3];  //定义目标数组,比原数组空间要大
		for(int i=0;i<src.length;i++) {
    
    
			dest[i]=src[i];  //循环将原数组元素赋值给目标数组
		}
		for(int i=0;i<dest.length;i++) {
    
    
			System.out.print(dest[i]+"\t"); 
		}
		
		//假设数组扩容的实现,需要我们自己写,那么有哪些参数?
		//copyOf1();  源数组,目标数组,源位置,目标位置,拷长度
		//copyOf2();  源数组,扩容的长度-->返回扩容数组
	}
}

2.2 系统定义的数组扩容方法

//系统提供的扩容方法调用:
public class Test2 {
    
    
	public static void main(String[] args) {
    
    
		int[] src = {
    
    1,3,5};  //原数组
		
		//-------扩容方式1-------
		//int[] dest = new int[src.length+3];  //目标数组
		//参数1:原数组  2.原的起始位置-一般为0   
		//3.目标数组   4.目标起始位置-一般为0    5.拷贝长度--一般是原数组长度
		//System.arraycopy(src, 0, dest, 0, src.length);
		
		//------扩容方式2------
		//参数1:原数组    参数2:要扩容的长度    返回值:扩容的目标数组
		int[] dest = Arrays.copyOf(src, src.length+3);
		
		for(int i=0;i<dest.length;i++) {
    
    
			System.out.print(dest[i]+"\t");
		}
		
	}
}

3.数组中的方法封装(重点)

3.1 关于方法的传参

传基本类型(值传递)

//案例:通过方法调用,将变量以参数传入,在方法实现中改变参数值;
//在main方法打印该变量,看该变量的值是否进行改变?
//值传递: 形参的改变,不会影响实参
public class Test1 {
    
    
	public static void main(String[] args) {
    
    
		int a = 3;
		change(a);
		System.out.println("最终结果:"+a);  //3
	}

	private static void change(int a) {
    
    
		a = 5;
	}
}

传引用类型(地址传递)

//案例2:将数组以参数传递,在方法实现中改变数组的元素
//在main方法中打印数组,查看是否改变?
//地址传递:形参的改变会影响实参
public class Test2 {
    
    
	public static void main(String[] args) {
    
    
		int[] a = {
    
    1,3,5};
		change(a);
		System.out.println(a[0]);  //9
	}

	private static void change(int[] a) {
    
    
		a[0] = 9;
	}
}

3.2 可变参数

//可变参数:
//1,先从普通数组参数进行演变
//案例:通过方法封装,来打印数组
//可变参数: 可以接收可变化的实参   格式: int... a
public class Test3 {
    
    
	public static void main(String[] args) {
    
    
		//int[] a = {1,3,5};
		//printArray(new int[]{1,3,5});  //打印数组
		
		printArray(1,3,5);  //本质传入new数组
	}

	//可变参数---本质:数组   使用反编译工具,查看
	private static void printArray(int...a) {
    
      
		for(int i=0;i<a.length;i++) {
    
    
			System.out.print(a[i]+"\t");
		}
	}
}

3.3 方法的返回值返回数组

//案例:在main方法中,传入一个原数组;在方法实现中对原数组进行扩容;
//且将扩容的数组返回
public class Test1 {
    
    
	public static void main(String[] args) {
    
    
		int[] a = {
    
    1,3,5};
		int[] b = copyOf(a,a.length+3);
		
		//在Arrays类中已经写好了数组的打印: 
		//toString: 将数组转字符串返回
		System.out.println(Arrays.toString(a));  //1,3,5
		
		System.out.println(Arrays.toString(b));  //1,3,5,0,0,0
	}


	private static int[] copyOf(int[] a,int len) {
    
     //输入原数组,及扩容长度
		//创建一个扩容数组
		int[] b = new int[len];
		//将原数组循环拷贝的扩容数组中
		for(int i=0;i<a.length;i++) {
    
    
			b[i]=a[i];
		}
		return b;
	}
}




猜你喜欢

转载自blog.csdn.net/weixin_45682261/article/details/125120972
今日推荐