1.方法、递归、数组
1.1方法
1.1.1方法的概念
- 方法是封装了具有特定功能的代码段
1.1.2方法的定义
-
如果没有返回值,return可以省略
package com.sxt; public class Test90 { // 求两个整数的和 /* * [修饰符] 返回值类型 方法名 (参数列表){ 方法体; return 返回值;} */ public static int add(int a, int b) { int c = a + b; return c; } public static void main(String[] args) { int m = 10; int n = 20; System.out.println("m + n = " + add(m, n)); } }
1.1.3方法的调用过程
- 方法定义时设置的参数为形式参数,具备占位作用
- 调用方法时传入的参数为实际参数
- 实参到形参之间的形式是值传递(Java没有址传递之说,引用类型也是值传递,传递的是地址值)
1.2方法重载
1.2.1重载的概念
-
同一个类中,多个方法名称相同,参数列表不同
-
参数列表不同:参数个数不同、参数类型不同、参数顺序不同
-
方法的重载与返回值的类型、形参的名称无关
package com.sxt; public class Test85 { // 方法的重载 public static int add(int a, int b) { return a + b; } public static int add(int a, int b, int c) { return a + b + c; } public static float add(float a, int b) { return a + b; } public static float add(int a, float b) { return a + b; } public static void main(String[] args) { int a = 1; int b = 2; int c = 3; float f = 1.0f; System.out.println("a + b =" + add(a, b)); System.out.println("a + f =" + add(a, f)); System.out.println("a + b + c =" + add(a, b, c)); } }
1.3方法递归
-
一个问题可被分解为若干层简单的子问题 ,子问题和其上层问题的解决方案一致,外层问题的解决依赖于子问题的解决。
package com.sxt; public class Test49 { public static long fb(int n) { if (n == 1 || n == 2) { return 1; } return fb(n - 1) + fb(n - 2); } public static void main(String[] args) { // 用循环求费布拉切数列的第n项 // TODO Auto-generated method stub for (int i = 1; i <= 44; i++) { System.out.print(fb(i) + " "); } } }
1.4数组
1.4.1数组的概念
- 相同数据类型的多个元素的有序集合
- 数组是引用数据类型
- 数组用下标标记数据元素,下标从0开始
1.4.2内存结构
- 栈区变量必须声明赋值后才可以使用
1.4.3数组的声明
-
数组是引用类型,自动分配默认值。
-
数组在栈中存的是堆区的地址
package com.sxt;
public class Test52 {
public static void main(String[] args) {
// 数组3种声明方式
int[] arr1 = new int[] { 1, 2, 3, 4, 5 };
// 声明数组(arr2是存在栈中的)
int[] arr2;
// 向堆区申请空间
arr2 = new int[] { 1, 2, 3, 4, 5, 6 };
// 字面量声明,声明和赋值要在同一行
int[] arr3 = { 1, 2, 3, 4, 5 };
// 引用数据类型声明之后有默认值
int[] arr4 = new int[4];
}
}
1.4.4数组的遍历
-
避免在一次循环(i==1)过程中,频繁使用arr[1]对同一个空间访问多次
package com.sxt; public class Test50 { public static void main(String[] args) { // 依次访问数组中的每个元素称为数组的遍历 // TODO Auto-generated method stub int[] arr = new int[4]; arr[0] = 1; arr[1] = 2; arr[2] = 3; arr[3] = 4; for (int i = 0; i < arr.length; i++) { System.out.print(arr[i]); } System.out.println(); // for each 快速遍历 // int 表示数组元素的数据类型 // item 表示迭代变量 // arr 表示迭代(需要遍历的)数组 for (int item : arr) { System.out.print(item); } } }
1.4.5数组的其他声明方式
1.5数组的常见算法
1.5.1数组查找算法
package com.sxt;
import java.util.Scanner;
public class Test53 {
public static void main(String[] args) {
/**
* 思想:依次访问数组的每个元素,问是否和输入的元素相等 如果相等结束循环,并返回位置 否则继续查找,直到查找到数组的最后
*/
int[] arr = new int[] { 1, 2, 3, 4, 5 };
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要查找的元素:");
int num = scanner.nextInt();
int i = 0;
for (; i < arr.length; i++) {
if (arr[i] == num) {
break;
}
}
if (i < arr.length) {
System.err.println("index:" + i);
} else {
System.out.println("没找到");
}
}
}
1.5.2插入算法
package com.sxt;
import java.util.Scanner;
public class Test55 {
public static void main(String[] args) {
// 需求:向有序数组中添加一个元素,元素添加后依然保持数组有序
/**
* 思想: [1] 找位置。在数组中查找目标元素合适的位置,并记录位置到index中 [2] 移动元素。从后向前依次后移
* [3]添加元素。把t添加到index的位置上
*/
int[] arr = { 1, 2, 5, 7, 9, 10 };
int t = 4;
int index = -1;
int num = arr.length;
for (int j = 0; j < num; j++) {
if (arr[j] > t) {
index = j;
// System.out.println("Index:" + j);
break;
}
}
if (index < 0) {
arr[num - 1] = t;
} else {
for (int i = num - 1; i <= index; i--) {
arr[i + 1] = arr[i];
arr[index] = i;
}
arr[index] = t;
num++;
}
for (int item : arr) {
System.out.print(item + "\t");
}
}
}
1.5.3删除算法
package com.sxt;
public class Test56 {
public static void main(String[] args) {
// 删除一个有序数组中的元素,依然保持数组有序
/**
* [1] 找位置。依次从数组中查询每个元素,如果存在,记录位置 [2] 移动元素。从前到后依次前移。 [3] 最后元素置空。
*/
int[] arr = new int[] { 1, 2, 3, 4, 5 };
int t = 2;
int num = arr.length;
int index = -1;
for (int i = 0; i < num; i++) {
if (arr[i] == t) {
index = i;
System.out.println("Index:" + i);
break;
}
}
if (index < 0) {
System.out.println("该数组中没有这个元素");
} else {
for (int i = index; i < num - 1; i++) {
arr[i] = arr[i + 1];
}
arr[--num] = 0;
}
for (int item : arr) {
System.out.print(item + "\t");
}
}
}
1.6排序算法
1.6.1冒泡排序
package com.sxt;
public class Test63 {
public static void main(String[] args) {
// 冒泡排序
/**
* 思想: 【1】数组中的数据两两向比较,得到一个大数,这个过程称为一趟。每趟必定会出现一个大数(大数沉底)。
* 【2】对剩余的数再两两比较,直到所有的数都有序。
*/
int[] arr = { 5, 1, 2, 3, 7, 8, 9 };
int temp = 0;
// 躺数
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
// 两两交换
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (int item : arr) {
System.out.print(item + "\t");
}
}
}
1.6.2直接插入排序
1.7命令行参数
1.8二维数组
-
二维数组的元素是一维数组的地址
1.8.1二维数组的声明
1.8.2二维数组的遍历
package com.sxt;
public class Test68 {
public static void main(String[] args) {
// 二维数组的声明
int[][] arr = new int[3][];
arr[0] = new int[] { 1, 2, 3 };
arr[1] = new int[] { 2, 3, 4 };
arr[2] = new int[] { 4, 5, 6 };
int[][] arr2 = { { 1, 2, 4 }, { 1, 2, 3, 4 }, { 1, 2, 3, 4, 5 } };
for (int i = 0; i < arr2.length; i++) {
for (int j = 0; j < arr2[i].length; j++) {
System.out.print(arr2[i][j] + "\t");
}
System.out.println();
}
}
}
1.9Arrays
- Arrays是jdk提供给开发者的一个工具类,工具类中提供了对数组的便利操作(查找、遍历、排序等)。
1.9.1Arrays常用方法
-
toString(int [] arr)
-
sort(int [] arr)
-
binarySearch(int[] arr,int key)
package com.sxt; import java.util.Arrays; import java.util.Scanner; import jdk.internal.org.objectweb.asm.tree.IntInsnNode; public class Test69 { public static void main(String[] args) { // [1] toString int [] arr = {8,1,2,4,5,6}; System.out.println(Arrays.toString(arr)); // [2] sort Arrays.sort(arr); System.out.println(Arrays.toString(arr)); // [3] binarySearch(int[] arr,int key); System.out.println(Arrays.binarySearch(arr, 2)); } }
1.9.2二分法查找
1.9.3数组的复制
package com.sxt;
import java.util.Arrays;
import javafx.scene.control.IndexRange;
public class Test73 {
public static void main(String[] args) {
// 使用copyof方法复制数组
int[] arr = { 1, 2, 3 };
int[] arr2 = Arrays.copyOf(arr, 3);
System.out.println(Arrays.toString(arr2));
System.out.println(Arrays.equals(arr, arr2));
int[] newArr = new int[arr.length];
System.arraycopy(arr, 0, newArr, 0, arr.length);
System.out.println(Arrays.toString(newArr));
}
}
1.10基本数据类型与引用类型的区别
1.10.1赋值
package com.sxt;
public class Test77 {
public static void main(String[] args) {
// 基本数据类型赋值
int a = 10;
int b = a;
a = 20;
// a = 20;b = 10;
// 引用类型赋值
int[] arr = { 1, 2, 3 };
int[] arr2 = arr;
arr2[0] = 100;
// arr:[100,2,3];arr2[100,2,3]
arr2 = new int[] { 10, 20, 30 };
// arr2:[10,20,30]
arr2[0] = 100;
// arr2:[100,20,30];arr[100,2,3]
}
}
1.10.2传值
package com.sxt;
public class Test78 {
public static void test(int m) {
m = 10;
}
public static void test2(int[] arr) {
arr[0] = 100;
}
public static void main(String[] args) {
// 基本数据类型传值
// 引用数据类型传值
int m = 20;
test(m);
// m = 20;
System.out.println("m = " + m);
int[] arr = { 1, 2, 3 };
test2(arr);
// [100,2,3]
System.out.println(arr[0]);
}
}
2.作业
-
声明一个char类型的数组, 从键盘录入6个字符 1.遍历输出 2.排序 3.把char数组转化成一个逆序的数组
package com.sxt; import java.util.Arrays; import java.util.Scanner; import javafx.scene.control.IndexRange; public class Test57 { public static void main(String[] args) { /* * 声明一个char类型的数组, 从键盘录入6个字符 遍历输出 排序 把char数组转化成一个逆序的数组 */ Scanner scanner = new Scanner(System.in); char[] arr = new char[6]; char temp; for (int i = 0; i < arr.length; i++) { System.out.print("请输入第" + (i + 1) + "个字符:"); temp = scanner.next().charAt(0); arr[i] = temp; } System.out.print("数组遍历输出:"); /* * for (char item : arr) { System.out.print(item + "\t"); } */ System.out.println(Arrays.toString(arr)); for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - 1 - i; j++) { if (arr[j] > arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } System.out.print("数组排序输出:"); System.out.println(Arrays.toString(arr)); for (int i = 0; i < arr.length / 2; i++) { temp = arr[i]; arr[i] = arr[arr.length - 1 - i]; arr[arr.length - 1 - i] = temp; } System.out.print("数组倒序输出:"); System.out.println(Arrays.toString(arr)); } }
-
有一组数分别为18,25,7,36,13,2,89,63求出最小的值,并将最小的数与最小的数所在的下标输出
package com.sxt; public class Test58 { public static void main(String[] args) { // 有一组数分别为18,25,7,36,13,2,89,63求出最小的值,并将最小的数与最小的数所在的下标输出 int[] arr = { 1, 25, 7, 36, 13, 2, 89, 63 }; int min = arr[0]; int index = 0; for (int i = 1; i < arr.length; i++) { if (arr[i] < min) { min = arr[i]; index = i; } } System.out.println("最小的数是:" + arr[index]); System.out.println("最小的数的下标是:" + index); } }
-
从键盘输入5句话,将这五句话逆序输出(不需要排序) 运行效果
大家早上好 明天会更好 北京欢迎你 长城长 太阳出来了
逆序输出
太阳出来了 长城长 北京欢迎你 明天会更好 大家早上好package com.sxt; import java.util.Scanner; public class Test59 { public static void main(String[] args) { /* * 从键盘输入5句话,将这五句话逆序输出(不需要排序) 运行效果 大家早上好 明天会更好 北京欢迎你 长城长 太阳出来了 逆序输出 太阳出来了 长城长 * 北京欢迎你 明天会更好 大家早上好 */ Scanner scanner = new Scanner(System.in); String[] arr = new String[5]; for (int i = 0; i < arr.length; i++) { System.out.print("请输入第" + (i + 1) + "句话:"); arr[i] = scanner.next(); } for (int j = arr.length - 1; j >= 0; j--) { System.out.print(arr[j] + "\t"); } } }
-
有一整数数组,{1,3,-1,5,-2},并将数据复制到新数组中,
要求逆序输出新数组中的数,同时并将小于0的元素按0存储
package com.sxt; import java.util.Arrays; public class Test60 { public static void main(String[] args) { // 有一整数数组,{1,3,-1,5,-2},并将数据复制到新数组中, // 要求逆序输出新数组中的数,同时并将小于0的元素按0存储 int[] arr = new int[] { 1, 3, -1, 5, -2 }; System.out.println(Arrays.toString(arr)); int[] arr2 = new int[arr.length]; int temp; for (int i = arr.length - 1, j = 0; i >= 0; i--, j++) { temp = arr[i]; if (temp > 0) { arr2[j] = temp; } } System.out.println(Arrays.toString(arr2)); } }
-
从键盘录入10个数,统计出合法的数,(1,2,3)为合法数,其它数是非法数
package com.sxt; import java.util.Scanner; public class Test61 { public static void main(String[] args) { // 从键盘录入10个数,统计出合法的数,(1,2,3)为合法数,其它数是非法数 Scanner scanner = new Scanner(System.in); int[] arr = new int[10]; int temp; int count1 = 0; int count2 = 0; int count3 = 0; int count4 = 0; for (int i = 0; i < arr.length; i++) { System.out.print("请输入第" + (i+1) + "个数字:"); temp = scanner.nextInt(); arr[i] = temp; } for (int item : arr) { switch (item) { case 1: { count1++; break; } case 2: { count2++; break; } case 3: { count3++; break; } default: { count4++; break; } } } System.out.println("数字1的个数是:" + count1); System.out.println("数字2的个数是:" + count2); System.out.println("数字3的个数是:" + count3); System.out.println("非法数字个数是:" + count4); } }
-
写一个方法equals(int[] arr1,int[] arr2) 比较数组是否相等
public class Test01 {
//写一个方法equals(int[] arr1,int[] arr2) 比较数组是否相等
public static boolean equals(int[] arr1,int[] arr2){
boolean flag = false;
int len1 = arr1.length;
int len2 = arr2.length;
if(len1 != len2){
flag = false;
return flag;
}
int t1,t2;
int i = 0;
for(;i<len1;i++){
t1 = arr1[i];
t2 = arr2[i];
if(t1 != t2){
break;
}
}
if(i==len1){
flag = true;
}
return flag;
}
public static void main(String[] args) {
int [] arr1 = {1,2,3};
int [] arr2 = {1,2,3,4};
System.out.println(equals(arr1,arr2));
}
}
- 构建如下二维数组
1 0 0
0 1 0
0 0 1
package com.sxt;
public class Test72 {
public static void main(String[] args) {
/*
* 构建如下二维数组
* 1 0 0
* 0 1 0
* 0 0 1
*/
int[][] arr = new int[3][3];
for(int i=0;i<arr.length;i++) {
for(int j = 0;j<arr[i].length;j++) {
if(i == j) {
arr[i][j] =1;
}
System.out.print(arr[i][j] +"\t");
}
System.out.println();
}
}
}
- 根据所学的知识自己写一个数组复制的方法int[] copyArray(int[] arr, newLength);
public class Test02 {
//根据所学的知识自己写一个数组复制的方法int[] copyArray(int[] arr, newLength);
public static int[] copyArray(int[] arr, int newLength) {
int[] newArr = new int[newLength];
int min = arr.length > newLength ? newLength : arr.length;
for (int i = 0; i < min; i++) {
newArr[i] = arr[i];
}
return newArr;
}
public static void main(String[] args) {
int[] arr = {1, 3, 4};
int[] newArr = copyArray(arr, 3);
for (int i : newArr) {
System.out.print(i + "\t");
}
}
}
- 给定一个二维数组,请对二维数组进行排序
排序前
3 2 1
9 4 5
6 8 7
排序后
1 2 3
4 5 6
7 8 9
import java.util.Arrays;
public class Test03 {
/* 给定一个二维数组,请对二维数组进行排序
排序前
3 2 1
9 4 5
6 8 7
排序后
1 2 3
4 5 6
7 8 9
*/
public static void main(String[] args) {
int[][] arr = {
{3, 2, 1},
{9, 4, 5},
{6, 8, 7}
};
int len = 0;
for (int[] item : arr) {
len += item.length;
}
int[] newArr = new int[len];
int index = 0;
for (int[] ints : arr) {
System.arraycopy(ints, 0, newArr, index, ints.length);
index += ints.length;
}
int temp;
for (int i = 0; i < newArr.length - 1; i++) {
for (int j = 0; j < newArr.length - 1 - i; j++) {
if (newArr[j] > newArr[j + 1]) {
temp = newArr[j];
newArr[j] = newArr[j + 1];
newArr[j + 1] = temp;
}
}
}
int[] tmp;
int idx = 0;
for (int i = 0; i < arr.length; i++) {
tmp = arr[i];
System.arraycopy(newArr, idx, tmp, 0, tmp.length);
idx += tmp.length;
}
for (int[] ints : arr) {
for (int anInt : ints) {
System.out.print(anInt + "\t");
}
System.out.println();
}
}
}
- 已知一个数组存在重复元素,请去掉数组中的重复元素(腾讯面试题。难度系数很大)。
例如:[1,2,2,3,4,4,4,1,2,4] 删除重复后[1,2,3,4]
package com.sxt;
import java.util.Arrays;
public class Test76 {
public static void main(String[] args) {
// 已知一个数组存在重复元素,请去掉数组中的重复元素(腾讯面试题。难度系数很大)。
// 例如:[1,2,2,3,4,4,4,1,2,4] 删除重复后[1,2,3,4]
// int[] arr = {1,2,2,2,3,4};
int[] arr = {1,2,2,3,4,4,4,1,2,4};
int num = arr.length; // 记录数组有效元素个数
int target = 0; // 检测是否重复的目标元素
for(int i=0;i<num;i++){
target = arr[i];
for (int j=i+1;j<num;j++){
if(target == arr[j]){
// 删除元素(移动元素)
for(int k = j+1;k<num;k++) {
arr[k - 1] = arr[k];
}
arr[--num] = 0;
j--;
}
}
}
// test
for(int item:arr){
System.out.print(item + "\t");
}
}
}
-
写一个方法int strCom(str1,str2)用于比较两个字符串的大小,
str1 < str2 方法结果返回 负数(-1)
str1 == str2返回0,
str1 > str2返回 正数(1)package com.sxt; public class Test81 { public static int strCom(String str1, String str2) { int result = 0; int len1 = str1.length(); int len2 = str2.length(); int lim = Math.min(len1, len2); int k = 0; for (; k < lim; k++) { if (str1.charAt(k) == str2.charAt(k)) { if (len1 == len2) { result = 0; } else if (len1 > len2) { result = 1; } else { result = -1; } } else if (str1.charAt(k) > str2.charAt(k)) { result = 1; } else { result = -1; } } return result; } public static void main(String[] args) { /* * 写一个方法int strCom(str1,str2)用于比较两个字符串的大小, str1 < str2 方法结果返回 负数(-1) str1 * ==str2返回0, str1 > str2返回 正数(1) */ String str1 = "123"; String str2 = "123"; System.out.println(strCom(str1, str2)); } }