标准javabean的生成
快捷键生成标准类(空参)/get&set类(有参数)
package com.chris.demo.test2;
public class Goods {
private String id;
private String name;
private double price;
private int count;
public Goods() {
}
public Goods(String id, String name, double price, int count) {
this.id = id;
this.name = name;
this.price = price;
this.count = count;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
数组学习
Java数组通俗解释(储物柜模型)
一、什么是数组?
想象一排带编号的储物柜:
- 每个柜子只能放同类型物品(比如全放书包或全放鞋子)
- 柜子数量固定(创建时确定数量,后期不能增减)
- 快速存取(知道编号就能直接找到对应柜子)
示例代码:
// 创建能放5本书的柜子
String[] bookCabinet = new String[5];
bookCabinet[0] = "Java入门"; // 第1个柜子
二、创建柜子的两种方式
- 现做柜子后放东西(动态初始化)
int[] scoreCabinet = new int[3]; // 3个放数字的柜子
scoreCabinet[0] = 90; // 第一个柜子放90分
- 定做带内容的柜子(静态初始化)
String[] fruitCabinet = {"苹果", "香蕉", "橘子"}; // 直接装满
三、使用柜子的要点
- 编号从0开始(没有负数和超限编号)
System.out.println(fruitCabinet[0]); // 输出"苹果"
// 错误示例:fruitCabinet[3] 会"找不到柜子"报错
- 查看柜子总数
System.out.println(fruitCabinet.length); // 输出3
- 遍历所有柜子
- 普通查看法(知道每个柜子编号)
for(int i=0; i<fruitCabinet.length; i++){
System.out.println("第" + (i+1) + "个水果:" + fruitCabinet[i]);
}
- 快速查看法(不需要知道编号)
for(String fruit : fruitCabinet){
System.out.println(fruit);
}
四、多层柜子(多维数组)
想象图书馆的储物区:
- 第一层:A区、B区、C区(行)
- 第二层:每个区有5个柜子(列)
示例代码:
// 创建一个3行2列的储物区
String[][] libraryCabinet = {
{"小说", "杂志"}, // A区
{"画册", "相簿"}, // B区
{"工具书", "词典"} // C区
};
System.out.println(libraryCabinet[1][0]); // 输出"画册"
五、特殊工具包(Arrays类)
Java提供了一套管理柜子的工具:
- 快速打印内容
System.out.println(Arrays.toString(fruitCabinet));
// 输出:[苹果, 香蕉, 橘子]
- 整理柜子(排序)
int[] nums = {3,1,4};
Arrays.sort(nums); // 变成[1,3,4]
- 复制柜子
String[] newCabinet = Arrays.copyOf(fruitCabinet, 2);
// 复制前两个水果
六、注意事项
- 柜子数量固定(不能中途增加)
- 只能放同类型物品(创建时确定类型)
- 小心空柜子(没放东西的柜子默认值:数字是0,对象是null)
- 传递柜子钥匙(数组作为参数传递时,修改会影响原数组)
生活场景类比
| 编程概念 | 现实类比 |
|---|---|
| 数组声明 | 在超市租用一组储物柜 |
| 数组长度 | 储物柜的总数量 |
| 数组索引 | 储物柜的编号(从0开始) |
| 多维数组 | 商场里的多层储物柜区域 |
| ArrayIndexOutOfBoundsException | 试图打开不存在的柜子编号 |
package com.chris.demo.test2;
public class GoodsTest {
public static void main(String[] args) {
//1.创建一个数组
Goods[] arr = new Goods[3];
//2.创建三个商品对面
Goods g1 = new Goods("001","华为P40",5999.0,100);
Goods g2 = new Goods("002","华为P60",5999.0,100);
Goods g3 = new Goods("003","华为P80",5999.0,100);
//3.把商品添加到数组中
arr[0] = g1;
arr[1] = g2;
arr[2] = g3;
//4.遍历
for(int i=0; i<arr.length;i++){
//i 索引 arr[i] 元素
Goods goods = arr[i];
System.out.println(goods.getId()+","+goods.getName()+","+goods.getPrice()+","+goods.getCount());
}
}
}
键盘接收知识点
Java键盘录入通俗解析(外卖点单模型)
一、基础操作:用Scanner点外卖
想象通过手机点外卖,Scanner就是你的点餐APP:
import java.util.Scanner; // 1️⃣ 安装点餐APP(导包)
public class Demo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); // 2️⃣ 打开APP(创建扫描器)
System.out.print("请输入菜品编号:");
int num = sc.nextInt(); // 3️⃣ 输入数字(比如输入2号套餐)
System.out.print("请输入备注:");
String note = sc.nextLine(); // 4️⃣ 输入文字(比如"不要香菜")
sc.close(); // 5️⃣ 关闭APP(释放资源)
}
}
类比理解:
nextInt()→ 选择数字类选项(套餐编号)nextLine()→ 填写文字备注close()→ 下单后关闭页面节省手机内存
二、输入方法对比(不同点餐方式)
| 方法 | 特点 | 类比场景 |
|---|---|---|
next() | 读到空格/回车停止,不读空格 | 点单时只说"鱼香肉丝" |
nextLine() | 读取整行包括空格 | 详细备注"鱼香肉丝,微辣" |
nextInt() | 只读整数,遇到字母会报错 | 输入"abc"当作份数会卡单 |
nextDouble() | 可读整数和小数(都转小数) | 输入"3"当作3.0元处理 |
三、避坑指南(常见错误)
-
混用导致"吞输入"
int num = sc.nextInt(); String s = sc.nextLine(); // 会读取到上一个输入的回车解决方法:统一用
nextLine()接收后转换:int num = Integer.parseInt(sc.nextLine()); -
输入类型不匹配
输入字母时用nextInt()会触发InputMismatchException异常,建议用try-catch处理:try { int age = sc.nextInt(); } catch (Exception e) { System.out.println("请输入数字!"); }
四、高效输入:BufferedReader(进阶版)
适合需要快速处理大量输入的场景(如竞赛编程):
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class FastInput {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine(); // 读取整行(比Scanner快)
int num = Integer.parseInt(s); // 手动转换类型
}
}
对比优势:
- 速度更快(缓冲区更大)
- 适合读取大段文本(如长篇文章)
五、实用技巧
-
输入前加提示语
System.out.print("请输入您的年龄:"); // 带箭头提示更友好 → -
循环持续输入
while(sc.hasNext()){ // 像外卖APP持续接单 String order = sc.nextLine(); if(order.equals("exit")) break; } -
输入验证
System.out.print("请输入密码(6位数字):"); String pwd = sc.nextLine(); if(pwd.length() != 6 || !pwd.matches("\\d+")) { System.out.println("密码格式错误!"); }
一句话总结
键盘录入就像点外卖:Scanner是操作简单的点餐APP,BufferedReader是专业后厨接单系统,用错方法就像把"微辣"输成数字会搞砸订单。记住口诀:"数字用nextInt,文字用nextLine,大量数据选缓冲"
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个整数");
int num1 = sc.nextInt();
System.out.println(num1);