Java数组以及冒泡排序--------07

Java数组以及冒泡排序--------07,第1张

一、回顾及打印方法版万年历
package com.qf.test01;

import java.util.Scanner;

public class Test01 {
	public static void main(String[] args) {
		
		Scanner scan = new Scanner(System.in);
		
		System.out.println("请输入年:");
		int year = scan.nextInt();
		System.out.println("请输入月:");
		int month = scan.nextInt();
		
		//计算总天数
		int allDay = getAllDayOfYear(1900, year) + getAllDayOfMonth(year, 1, month) + 1;
		
		//计算星期
		int week = getWeek(allDay);
		
		//获取当月的天数
		int day = getDay(year, month);
		
		//打印日历
		printCalendar(year, month, day, week);
		
		//关闭资源
		scan.close();
	}
	
	//打印日历
	public static void printCalendar(int year, int month, int day, int week){
		System.out.println(" ---" + year + "年" + month + "月---");
		
		System.out.println("一\t二\t三\t四\t五\t六\t日");
		
		int num = 0;//控制换行
		
		//打印空格
		for(int i = 1;i
二、冒泡排序
package com.qf.test02;

public class Test01 {

	public static void main(String[] args) {
		/**
		 * 
		 * 冒泡排序口诀:
		 * 		N个数字来排序
		 * 		两两相比小靠前
		 * 		外层循环N-1
		 * 		内层循环N-1-i
		 * 
		 * 有兴趣研究下其他的排序算法
		 * 	https://blog.csdn.net/weixin_45082647/article/details/104149152
		 */
		
		int[] is = {64,12,81,8,45,27};
		
		for(int i = 0;i is[j+1]){
					int temp = is[j];
					is[j] = is[j+1];
					is[j+1] = temp;
				}
			}
		}
		
		for (int e : is) {
			System.out.println(e);
		}
	}
}
2.1数组遍历
package com.qf.test03;

public class Test01 {

	public static void main(String[] args) {
		/**
		 * 知识点:数组的查找 -- 顺序查找
		 * 
		 * 设计思想:从头到尾遍历数组
		 */
		
		int[] is = {64,12,81,8,45,27};
		
		int num = 81;//要查找的元素
		
		for (int i = 0; i < is.length; i++) {
			if(is[i] == num){
				System.out.println("查找到元素了");
			}
		}
		
	}
}
2.2遍历循环2分法查找
package com.qf.test03;
import java.util.Arrays;
public class Test02 {
	public static void main(String[] args) {
		/**
		 * 知识点:数组的查找 -- 二分法查找
		 * 
		 * 设计思想:把数组一分为二,缩小范围的查找方式
		 * 注意:使用二分法查找之前,必须先排序
		 */
		int[] is = {64,12,81,8,45,27};
		int num = 81;//要查找的元素
		//排序:8 12 27 45 64 81 
		Arrays.sort(is);
		int start = 0;
		int end = is.length-1;
		while(start <= end){
			int mid = (start + end)/2;
			
			if(num > is[mid]){
				start = mid+1;
			}else if(num < is[mid]){
				end = mid-1;
			}else{
				System.out.println("找到元素了");
				break;
			}
		}
		
	}
}
 三、数组的复制 3.1缺点
package com.qf.test04;

public class Test01 {

	public static void main(String[] args) {	
		//原数组
		String[] names = {"水菜丽","朝桐光","濑亚美莉","深田咏美"};
		//新数组
		String[] newNames = names;
		//修改原数组中的数据
		names[0] = "高正";
		//遍历新数组
		for (String str : newNames) {
			System.out.println(str);
		}
	}
}
 3.2数组复制2
package com.qf.test04;

public class Test02 {

	public static void main(String[] args) {
		
		//原数组
		String[] names = {"水菜丽","朝桐光","濑亚美莉","深田咏美"};
		
		//新数组
		String[] newNames = new String[names.length];
		
		//将原数组中的数据迁移到新数组中
		for (int i = 0; i < names.length; i++) {
			newNames[i] = names[i];
		}
		
		//修改原数组中的数据
		names[0] = "高正";
		
		//遍历新数组
		for (String str : newNames) {
			System.out.println(str);
		}
	}
}
3.3数组的扩容
package com.qf.test04;

public class Test03 {

	public static void main(String[] args) {
		/**
		 * 知识点:数组的扩容
		 */
		
		//原数组
		String[] names = {"水菜丽","朝桐光","濑亚美莉","深田咏美"};
		
		//创建新的数组(是原数组长度1.5倍)
		int oldCapacity = names.length;
		int newCapacity = oldCapacity + (oldCapacity>>1);
		String[] newNames = new String[newCapacity];
		
		//将原数组所有的数据迁移到新数组中
		for (int i = 0; i < names.length; i++) {
			newNames[i] = names[i];
		}
		
		//将新数组的地址赋值给原数组
		names = newNames;
		
		//遍历原数组
		for (String str : names) {
			System.out.println(str);
		}
	}
}
3.4数组的删除1
package com.qf.test04;

public class Test04 {

	public static void main(String[] args) {
		/**
		 * 缺点:数组本来说是一个存储数据的容器,删除一个元素,就把容器的空间给缩小了(不合理的设计思想)
		 */
		
		//原数组
		String[] names = {"水菜丽","朝桐光","濑亚美莉","深田咏美"};
		
		//创建新数组
		String[] newNames = new String[names.length-1];
		
		//除了要删除的元素("朝桐光"),把 其余的元素迁移到新数组中
		int index = 0;//新数组的下标
		for (String str : names) {
			if(!str.equals("朝桐光")){
				newNames[index++] = str;
			}
		}
		
		//将新数组的地址赋值给原数组
		names = newNames;
		
		//遍历原数组
		for (String str : names) {
			System.out.println(str);
		}
	}
}
3.5数组的删除2
package com.qf.test04;

public class Test05 {

	public static void main(String[] args) {
		
		//原数组
		String[] names = {"水菜丽","朝桐光","濑亚美莉","深田咏美"};
		
		//迁移数据(把要删除的元素后面的数据往前移)
		for (int i = 1; i < names.length-1; i++) {
			names[i] = names[i+1];
		}
		
		//把最后一个元素赋值为null
		names[names.length-1] = null;
		
		//遍历原数组
		for (String str : names) {
			System.out.println(str);
		}
	}
}
3.6需求 

设计一个方法,传入一个int类型的数组,返回最大值和最小值 

package com.qf.test05;

public class Test01 {

	public static void main(String[] args) {
		int[] is = {64,12,81,8,45,27};
		
		int[] method = method(is);
		
		System.out.println("最大值为:" + method[0]);
		System.out.println("最小值为:" + method[1]);
	}
	
	public static int[] method(int[] is){
		//假设下标为0的元素是最大值
		int max = is[0];
		//假设下标为0的元素是最小值
		int min = is[0];
		
		for (int i = 1; i < is.length; i++) {
			if(max < is[i]){
				max = is[i];
			}
			if(min > is[i]){
				min = is[i];
			}
		}
		
		return new int[]{max,min};
	}
}

总结:
1.数组可以作为方法的参数传入到方法内部
2.数组可以作为返回值将方法内部的多个参数返回给调用方

3.7可变数 
package com.qf.test05;

public class Test02 {

	public static void main(String[] args) {
		
		//实参作为元素压入到数组中
		int max = getMax(1,2,3);
		System.out.println(max);
	}
	
	//可变参数底层就是数组
	public static int getMax(int... is){
		
		int max = is[0];
		
		for (int i = 1; i < is.length; i++) {
			if(max < is[i]){
				max = is[i];
			}
		}
		return max;
	}
	
	//可变参数后面不能加其他参数
	public static void method(String str,int...is){
	}
}
 3.8Arrays工具类
package com.qf.test05;

import java.util.Arrays;

public class Test03 {

	public static void main(String[] args) {
		/**
		 *
		 * 含义:Java给我们提供的专门 *** 作数组的工具类
		 * 
		 * 工具类--该类方法都是静态的,直接使用类名调用
		 * API -- Java类的使用说明书
		 */
		
		int[] is = {64,12,81,8,45,27};
		
		//排序:8, 12, 27, 45, 64, 81
		Arrays.sort(is);
		
		//查找(前提必须先排序)
		//返回值:如果查找的数据在数组内,返回下标,否则返回 -插入点-1
		int index = Arrays.binarySearch(is, 12);
		System.out.println("获取查找元素的下标:" + index);
		
		//拷贝数组
		int[] copyOf = Arrays.copyOf(is, is.length*2);
		
		//拷贝区间(开始下标-包含,结束下标-不包含)
		int[] copyOfRange = Arrays.copyOfRange(copyOf, 2, 8);
		
		//替换
		Arrays.fill(copyOfRange, 888);
		Arrays.fill(copyOfRange, 1, 4, 666);//(开始下标-包含,结束下标-不包含)
		
		//将数组转换为字符串
		System.out.println(Arrays.toString(copyOfRange));
		
	}
}
四、内存图

 

 

 

 

 

 

 

 

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/756755.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-04-30
下一篇2022-04-30

发表评论

登录后才能评论

评论列表(0条)