1.return关键字的用法return 在有返回值的方法中使用return 返回值;return 关键字书写以后后面的代码就不能写了2.声明方法的四种形式无参无返回值的语法格式有参无返回值的无参有返回值的有参有返回值的3.什么是形参,什么是实参在方法的声明处,写的变量叫形参在方法的调用处,书写的是实参在方法的调用的时候,将实参赋值给形参
4.数组的三种声明方法数据类型[] 数组名字 = {值1, 值2,...};数据类型[] 数组名字 = new 数据类型[容量];数据类型[] 数组名字 = new 数据类型[]{值1, 值2, ...};
5.对第二种数组进行声明的时候如何进行赋值arr[0] = 12;
6.for循环的语法格式和执行流程for () {}
回顾
1.数组的声明三种的声明
2.对数组如何进行赋值通过下标来进行赋值
3.对数组进行取值通过下标进行取值
4.数组可以作为方法的参数
5.数组可以作为方法的返回值[1,2,3,4]=>[4,3,2,1]
1.需求:定义一个方法,找出int数组中,最大值的索引下标
[1,2,23,5,6,7,8] 打印出来2
2.需求:定义一个方法,找出int数组中,最小值的索引下标
3.需求:定义一个方法,在指定的int数组中找出指定的数据第一个的下标8这个数的下标
[1,2,8,4,5,78,7,8,9]4. 在一个数组中,找出所有的 指定数据的下标位置 【难】找出所有的下标存倒数组中8这个数的下标
[1,2,8,4,5,7,8,7,8,9]
5. 完成一个方法,替换掉数组中所有元素为0的元素,替换为指定元素 [1,2,3,0,0,0]
6. 完成一个方法,删除指定下标的元素,要求从删除位置开始,之后的元素整体前移。【难】[1,2,3,4,5,6,7]=>[1,2,4,5,6,7,0]
7. 完成一个方法,添加指定元素到指定下标位置,要求从指定下标位置之后的元素,整体向后移动。【难】[1,2,3,4,5,0]=>[1,2,250,3,4,5]8. 找出数组中最大的元素,放到下标为0的位置9. 在上一道题的基础上,不考虑下标为0的元素。找出数组中最大的元素,放到下标为1的位置
1.方法循环数组的练习题
1.需求:定义一个方法,找出int数组中,最小值的索引标
[1,2,3,4,5]=>0
package com.qfedu.a_test;public class Demo1 {public static void main(String[] args) {//需求:定义一个方法,找出int数组中,最小值的索引下标int[] arr = {3,2,1,4};//2int minIndex = 0;//minIndex目的是用来存储最小值的下标的for (int i = 0; i < arr.length; i++) {/*** i=0 0<4true arr[0]>arr[0] false minIndex=0 i++* i=1 1<4 true arr[0]>arr[1] true minIndex=1 i++* i=2 2<4 true arr[1]>arr[2] true minIndex=2 i++* i=3 3<4 true arr[2]>arr[3] false i++* i=4 4<4 false 循环结束*/if (arr[minIndex] > arr[i]) {minIndex = i;}}System.out.println(minIndex);}}
package com.qfedu.a_test;public class Demo2 {public static void main(String[] args) {int[] arr = {1,2,3,4,5};System.out.println(minIndexInArray(arr));}public static int minIndexInArray (int[] arr) {int minIndex = 0;for (int i = 0; i < arr.length; i++) {if (arr[minIndex] > arr[i]) {minIndex = i;}}return minIndex;}
}
3.需求:定义一个方法,在指定的int数组中找出指定的数据第一个的下标
8这个数的下标
[1,2,8,4,5,7,8,7,8,9]
package com.qfedu.a_test;public class Demo3 {public static void main(String[] args) {int[] arr = {1,2,8,3,4,8,9,8};System.out.println(indexOf(arr, 100));}/*** * @param arr 是数组,原始数组 你要在整个数组中找出那个值* @param find 要找的值* @return 是返回的找到值的索引下标*/public static int indexOf (int[] arr, int find) {int index = -1;for (int i = 0; i < arr.length; i++) {if (find == arr[i]) {index = i;break;//终止}}return index;}
}
3.在一个数组中,找出所有的 指定数据的下标位置 【难】
找出所有的下标存倒数组中
8这个数的下标
[1,2,8,4,5,7,8,7,8,9]===>[2,6,8]
package com.qfedu.a_test;import java.util.Arrays;public class Demo4 {public static void main(String[] args) {//在一个数组中,找出所有的 指定数据的下标位置 【难】
// 找出所有的下标存倒数组中
// 8这个数的下标
//[1,2,8,4,5,7,8,7,8,9]//1.先声明一个数组int[] arr = {8, 2, 8, 4, 8, 9,8,78,67,8};int count = 0;//计数的//就找8 8的下标 2,6,8 将他们存到一个新数组中//2.新建一个空的数组,空的数组容量是上一个数组的大小int[] indexes = new int[arr.length];//容器//[0,0,0,0]for (int i = 0; i < indexes.length; i++) {indexes[i] = -1;}//代码走到这一步 indexes = {-1,-1,-1,-1};/*** {8, 2, 8, 4,};* i=0 0<4 true 8 == arr[0] true indexes[0] = 0; count++ i++* i=1 1<4 true 8==arr[1] false i++* i=2 2<4 true 8==arr[2] true indexes[1]=2;count++ i++* i=3 3<4 true 8==arr[3] false i++* i=4 4<4 false 循环结束*/for (int i = 0; i < arr.length; i++) {if (8 == arr[i]) {//将i存到新数组中[0,2,-1,-1] //根据索引下标进行存储的indexes[count++] = i;}//此时新数组中indexes = {0,2,-1,-1};//{0,2}}System.out.println(Arrays.toString(indexes));//现在要求有几个索引就是几个不要-1//借助于count这个变量//重新弄一个数组int[] newIndexes = new int[count];for (int i = 0; i < newIndexes.length; i++) {newIndexes[i] = indexes[i];}System.out.println(Arrays.toString(newIndexes));}}
package com.qfedu.a_test;import java.util.Arrays;public class Demo5 {public static void main(String[] args) {int[] arr = {2,3,8,7,8,2,8};int[] arr1 = findAllIndexes(arr, 8);System.out.println(Arrays.toString(arr1));}public static int[] findAllIndexes (int[] arr, int find) {int count = 0;//计数器//这个数组使用存放值的下标的int[] indexes = new int[arr.length];//[0,0,0,0,0]for (int i = 0; i < indexes.length; i++) {indexes[i] = -1;}//[-1,-1,-1,-1,-1...]for (int i = 0; i < arr.length; i++) {if (find == arr[i]) {indexes[count++] = i;}}//[2,4,6,-1,-1,-1...]//再弄一个新的数组 只要2 4 6int[] newIndexes = new int[count];for (int i = 0; i < newIndexes.length; i++) {newIndexes[i] = indexes[i];}return newIndexes;}
}
4. 完成一个方法,替换掉数组中所有元素为0的元素,替换为指定元素 [1,2,3,0,0,0]
package com.qfedu.a_test;import java.util.Arrays;public class Demo6 {public static void main(String[] args) {int[] arr = {1,0,2,0,3};System.out.println(Arrays.toString(replace(arr, 250)));}public static int[] replace (int[] arr, int find) {for (int i = 0; i < arr.length; i++) {if (arr[i] == 0) {arr[i] = find;}}return arr;}}
5.完成一个方法,删除指定下标的元素,要求从删除位置开始,之后的元素整体前移。【难】
[1,2,3,4,5,6,7]=>[1,2,4,5,6,7,0]
package com.qfedu.a_test;import java.util.Arrays;public class Demo7 {public static void main(String[] args) {int[] arr = {1,2,3,4,5};//想删除下标为2的/*** i=2 2<4 arr[2]=arr[3] =>{1,2,4,4,5} i++* i=3 3<4 arr[3]=arr[4]=>{1,2,4,5,5} i++* i=4 4<4 false循环结束* arr[4] = 0;*/for (int i = 2; i < arr.length - 1; i++) {arr[i] = arr[i + 1];}arr[arr.length - 1] = 0;System.out.println(Arrays.toString(arr));}}
package com.qfedu.a_test;import java.util.Arrays;public class Demo8 {public static void main(String[] args) {int[] arr = {1,2,3,4,5};System.out.println(Arrays.toString(remove(arr, 1)));}public static int[] remove (int[] arr, int find) {if (find > arr.length - 1) {System.out.println("删除的索引超出了数组的范围");System.exit(0);}for (int i = find; i < arr.length - 1; i++) {arr[i] = arr[i + 1];}arr[arr.length - 1] = 0;return arr;}
}
package com.qfedu.a_test;import java.util.Arrays;public class Demo10 {public static void main(String[] args) {int[] arr = {1,2,3,4,5,6,7,8,10,0};System.out.println(Arrays.toString(add(arr, 3, 799)));}/*** * @param arr 被添加的数组* @param index 添加到数组中下标值* @param number 添加到数组中的值* @return*/public static int[] add (int[] arr, int index, int number) {for (int i = arr.length - 1; i > index; i--) {arr[i] = arr[i - 1];}arr[index] = number;return arr;}}
上午的东西是数组 循环 分支 方法 综合一起的案例
6. 找出数组中最大的元素,放到下标为0的位置
[3,2,4,5,1]=>[5,2,4,3,1]
把最大值放到下标为0的位置,原来下标为0的数据,放到之前最大值的下标位置
package com.qfedu.a_test;import java.util.Arrays;public class Demo11 {public static void main(String[] args) {int[] arr = {3,2,4,5,1};int maxIndex = 0;//接收最大值下标for (int i = 0; i < arr.length; i++) {if (arr[maxIndex] < arr[i]) {maxIndex = i;}}//代码走到这一步 其实就是之前讲过的最大值的下标//最大值的下标是3 要把下标为3的数据放到下标为0的位置//arr[0] = arr[maxIndex];//{5,2,4,5,1}这种方式不行啊//咋办? 借助于中间变量int temp = arr[maxIndex];//先将最大值赋值给temparr[maxIndex] = arr[0];//将第一个数据3 赋值给最大值{3,2,4,3,1}arr[0] = temp;//{5,2,4,3,1}System.out.println(Arrays.toString(arr));}}
package com.qfedu.a_test;import java.util.Arrays;public class Demo12 {public static void main(String[] args) {int[] arr = {89,67,34,90,98,98,34};change(arr);}public static void change (int[] arr) {int maxIndex = 0;for (int i = 0; i < arr.length; i++) {if (arr[maxIndex] < arr[i]) {maxIndex = i;}}System.out.println(maxIndex);//交换int temp = arr[maxIndex];arr[maxIndex] = arr[0];arr[0] = temp;System.out.println(Arrays.toString(arr));}}
7.在上一道题的基础上,不考虑下标为0的元素。找出数组中最大的元素,放到下标为1的位置
package com.qfedu.a_test;import java.util.Arrays;public class Demo13 {public static void main(String[] args) {int[] arr = {3, 2, 4, 5, 1 };int maxIndex = 0;// 接收最大值下标for (int i = 0; i < arr.length; i++) {if (arr[maxIndex] < arr[i]) {maxIndex = i;}}// 代码走到这一步 其实就是之前讲过的最大值的下标// 最大值的下标是3 要把下标为3的数据放到下标为0的位置// arr[0] = arr[maxIndex];//{5,2,4,5,1}这种方式不行啊// 咋办? 借助于中间变量int temp = arr[maxIndex];// 先将最大值赋值给temparr[maxIndex] = arr[0];// 将第一个数据3 赋值给最大值{3,2,4,3,1}arr[0] = temp;// {5,2,4,3,1}System.out.println(Arrays.toString(arr));//找2 4 3 1 中的最大值,然后把最大值放到下标为1的位置//{5,4,2,3,1}int maxIndex1 = 1;for (int i = 1; i < arr.length; i++) {if (arr[maxIndex1] < arr[i]) {maxIndex1 = i;}}//交换位置int temp1 = arr[maxIndex1];arr[maxIndex1] = arr[1];arr[1] = temp1;System.out.println(Arrays.toString(arr));}
}
package com.qfedu.a_test;import java.util.Arrays;public class Demo14 {public static void main(String[] args) {int[] arr = {3, 2, 4, 5, 1 };change1(arr);}public static void change1 (int[] arr) {int maxIndex = 0;// 接收最大值下标for (int i = 0; i < arr.length; i++) {if (arr[maxIndex] < arr[i]) {maxIndex = i;}}// 代码走到这一步 其实就是之前讲过的最大值的下标// 最大值的下标是3 要把下标为3的数据放到下标为0的位置// arr[0] = arr[maxIndex];//{5,2,4,5,1}这种方式不行啊// 咋办? 借助于中间变量int temp = arr[maxIndex];// 先将最大值赋值给temparr[maxIndex] = arr[0];// 将第一个数据3 赋值给最大值{3,2,4,3,1}arr[0] = temp;// {5,2,4,3,1}System.out.println(Arrays.toString(arr));//找2 4 3 1 中的最大值,然后把最大值放到下标为1的位置//{5,4,2,3,1}int maxIndex1 = 1;for (int i = 1; i < arr.length; i++) {if (arr[maxIndex1] < arr[i]) {maxIndex1 = i;}}//交换位置int temp1 = arr[maxIndex1];arr[maxIndex1] = arr[1];arr[1] = temp1;System.out.println(Arrays.toString(arr));}}
2.排序
[3,2,1,4,5]=>从小到大排序
2.1选择排序
原理:找到最小值的索引,然后和第1个数据进行交换。再找除了第一个数据以外的最小值的索引。然后和第二个数据交换
package com.qfedu.b_sort;import java.util.Arrays;public class Demo1 {public static void main(String[] args) {//选择排序int[] arr = {3,4,1,5,2};/*** i=0 0<4 true minIndex=0 * 进入内层的for循环* j=0 0<5 true arr[0] >arr[0]false j++* j=1 1<5 true arr[0] >arr[1] false j++* j=2 2<5 true arr[0]>arr[2]true minIndex=2 j++* j=3 3<5 true arr[2]>arr[3]fasle j++* j=4 4<5 true arr[2]>arr[4] fasle j++* j=5 5<5fasle循环结束* 执行交换代码* temp = arr[2]=1* arr[2] = arr[0] {3,4,3,5,2}* arr[0] = 1 {1,4,3,5,2} i++* i=1 1<4 true minIndex =1* ......* */for (int i = 0; i < arr.length - 1; i++) {//控制的是轮数int minIndex = i;for (int j = i; j < arr.length; j++) {//遍历咱们的数据找到最小值的索引的if (arr[minIndex] > arr[j]) {minIndex = j;}}//交换位置int temp = arr[minIndex];arr[minIndex] = arr[i];arr[i] = temp;}System.out.println(Arrays.toString(arr));}}
2.2冒泡排序
从小到大排序
原理:比较两个相邻的数据,如果左边比右边元素大就交换位置。如果左边比右边小,就不变
package com.qfedu.b_sort;import java.util.Arrays;public class Demo2 {public static void main(String[] args) {//冒泡 排序和索引没有关系int[] arr = {1,5,2,3};for (int i = 0; i < arr.length - 1; i++) {//最内层的循环两两比较交换位置//4-1-i=>i=0 第1轮 3次//4-1-i=>i=1 第2轮 2次//4-1-i=>i=2 第3轮 1次for (int j = 0; j < arr.length - 1 - i; j++) {if (arr[j] > arr[j + 1]) {//交换位置int temp = arr[j];arr[j] = arr[j + 1];arr[j + 1] = temp;}}}System.out.println(Arrays.toString(arr));}
}
作业:
1.把今天的东西理解为主,该写注释的写注释
2.自学 Java快速排序杨辉三角二分法查找算法
3.一套卷子自己名字交给组长