知识点
- 方法的重载
- 数组
- 内存
方法的重载
overload(重载)
- 参数名相同
- 参数列表不同
数组
- 静态数组初始化
int[] arr = new int[]{1, 2, 3, 4, 5}; - 动态数组初始化
int[] arr = new int[5];
内存
- 常量池
存放常量 - 静态域
存放静态数据 - 栈
存放变量和引用 - 堆
存放new出来的数据
课后作业
- 用数组存放7个数据后进行排序
package com.day04;
public class Array {
public static void main(String[] args) {
int[] arr = new int[]{2, 4, 9, 5, 7};
int max = arr[0];
for(int i = 1; i < arr.length; i++) {
if(max < arr[i]) {
max = arr[i];
}
}
System.out.println(max);
}
}
- 给7个数据进行排序
package com.day04;
import java.util.Arrays;
public class Test01 {
public static void main(String[] args) {
int[] arr = new int[]{1, 3, 5, 2, 7, 3, 8};
//冒泡排序
for(int i = 0; i < arr.length - 1; i++) {
for(int j = 0; j < arr.length - i - 1; j++) {
int temp;
if(arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
System.out.println(Arrays.toString(arr));
//选择排序
for(int i = 0; i < arr.length - 1; i++) {
for(int j = i; j < arr.length - 1; j++) {
int temp;
if(arr[i] > arr[j + 1]) {
temp = arr[i];
arr[i] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
System.out.println(Arrays.toString(arr));
}
}
- 折半查找
package com.day04;
public class HalfQuery {
public static void main(String[] args) {
int[] arr = new int[]{2, 4, 9, 15, 27};
System.out.println(HalfQuery(arr, 27));
}
public static int HalfQuery(int[] arr, int val) {
int length = arr.length;
int front = 0;
int rear = length - 1;
int mid;
while(front <= rear) {
mid = (front + rear) / 2;
if(val == arr[mid]) {
return mid;
}
else if(arr[mid] < val) {
front = mid + 1;
}
else if(arr[mid] > val) {
rear = mid - 1;
}
}
return -1;
}
}
- 注册登录系统
package com.day04;
import java.util.Scanner;
public class Test03 {
private static int n = 5;
private static Scanner sca = new Scanner(System.in);
private static int length = 0;
private static String[][] data = new String[n][];
public static void main(String[] args) {
menu1();
}
//登陆菜单
static void menu1() {
System.out.println("欢迎使用夜雨柠檬的系统");
System.out.println("1. 登陆");
System.out.println("2. 注册");
System.out.println("0. 退出");
int p = sca.nextInt();
if(p == 1) {
login();
}
if(p == 2) {
register();
}
if(n == 0) {
return;
}
}
//操作菜单
static void menu2() {
System.out.println("欢迎进入夜雨柠檬内部系统!");
System.out.println("1. 添加会员");
System.out.println("2. 删除会员");
System.out.println("3. 查询会员");
System.out.println("4. 更改会员");
System.out.println("0. 退出");
int p = sca.nextInt();
if(p == 1) {
add();
}
if(p == 2) {
delete();
}
if(p == 3) {
query();
}
if(p == 4) {
change();
}
if(n == 0) {
return;
}
}
//登陆
static void login() {
System.out.print("请输入用户名:");
String username = sca.next();
System.out.print("请输入密码:");
String userpassword = sca.next();
for(int i = 0; i < length; i++) {
if(data[i][0].equals(username) && data[i][1].equals(userpassword)) {
System.out.println("登陆成功!");
menu2();
menu1();
return;
}
}
System.out.println("登陆失败!");
login();
}
//注册
static void register() {
if(length == n) {
System.out.println("会员已满");
menu1();
return;
}
data[length] = new String[2];
System.out.print("请输入会员名:");
data[length][0] = sca.next();
System.out.print("请输入密码:");
data[length][1] = sca.next();
length++;
System.out.println("注册成功!");
menu1();
return;
}
//添加
static void add() {
if(length == n) {
System.out.println("会员已满");
menu2();
return;
}
data[length] = new String[2];
System.out.print("请输入会员名:");
data[length][0] = sca.next();
System.out.print("请输入密码:");
data[length][1] = sca.next();
length++;
System.out.println("添加成功!");
menu2();
return;
}
//删除
static void delete() {
if(length == 0) {
System.out.println("数据为空!");
}
System.out.print("请输入用户名:");
String username = sca.next();
for(int i = 0; i < length; i++) {
if(data[i][0].equals(username)) {
for(int j = i; j < length - 1; j++) {
data[j] = data[j + 1];
}
length--;
System.out.println("删除成功!");
menu2();
return;
}
}
System.out.println("删除失败!");
delete();
}
//查询
static void query() {
if(length == 0) {
System.out.println("数据为空!");
}
System.out.print("请输入用户名:");
String username = sca.next();
for(int i = 0; i < length; i++) {
if(data[i][0].equals(username)) {
System.out.printf("%s\t", data[i][0]);
System.out.printf("%s\n", data[i][1]);
menu2();
return;
}
}
System.out.println("查询失败!");
query();
}
//更改会员
static void change() {
if(length == 0) {
System.out.println("数据为空!");
}
System.out.print("请输入要更改会员名:");
String username = sca.next();
for(int i = 0; i < length; i++) {
if(data[i][0].equals(username)) {
System.out.print("会员名将要改为:");
username = sca.next();
data[i][0] = username;
System.out.print("密码将要改为:");
String userpassword = sca.next();
data[i][1] = userpassword;
System.out.println("更改成功!");
menu2();
return;
}
}
System.out.println("更改失败!");
change();
}
}