public static void main(String[] args) {
//1.定义数组
int[] a1; // 推荐这种写法,a1其实是引用,指向数组地址
int a2[]; // 不推荐
// int[10] a3; // 错误写法,定时数组时不要写大小,a3只代表是引用
//2.分配空间
a1 = new int[5];
//3.初始化
a1[0] = 0;
a1[1] = 1;
a1[2] = 2;
a1[3] = 3;
a1[4] = 4;
//3.1定义时初始化
int[] a4 = new int[]{0, 1, 2, 3, 4}; // 注意不要写成int[] a4 = new int[5]{0, 1, 2, 3, 4};
//3.2直接指定数组元素值
int[] a5 = {0, 1, 2, 3, 4}; // 不能分成两步写
//4.修改数组
//4.1 数组可以被重新赋值
int[] a6 = {0, 1, 2};
int[] a7 = {1, 0};
a7 = a6;
System.out.print("a7:");
for (int x : a7) {
System.out.print(x + " ");
}
//4.2
// a6 = {3, 2, 1}; // 错误写法
a6 = new int[10];
a6 = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
System.out.print("na6:");
for (int x : a6) {
System.out.print(x + " ");
}
}
输出
a7:0 1 2 a6:0 1 2 3 4 5 6 7 8 9 102.二维数组定义及初始化方法
public static void main(String[] args) {
//1.定义数组
int[][] a1;
//2.分配空间
a1 = new int[5][5];
//3.初始化
a1[0][0] = 1;
//3.1定义时初始化
int[][] a2 = new int[][]{{0, 1, 2}, {3, 4, 5}, {6, 7, 8}}; // 注意不要写成int[] a4 = new int[5]{0, 1, 2, 3, 4};
//3.2直接指定数组元素值
int[][] a3 = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}}; // 不能分成两步写
//4.修改数组
//4.1 数组可以被重新赋值
int[][] a4 = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}};
System.out.println("a4:");
for (int[] x : a4) {
for (int y : x) {
System.out.print(y + " ");
}
System.out.println();
}
//4.2
int[][] a5 = {{0, 1}};
a4 = a5;
System.out.println("a4:");
for (int[] x : a4) {
for (int y : x) {
System.out.print(y + " ");
}
System.out.println();
}
//5.不定长数组
int[][] a6 = new int[5][];
a6[0] = new int[1];
a6[1] = new int[2];
a6[2] = new int[3];
a6[3] = new int[4];
a6[4] = new int[5];
System.out.println("a6:");
for (int[] x : a6) {
for (int y : x) {
System.out.print(y + " ");
}
System.out.println();
}
}
输出
a4: 0 1 2 3 4 5 6 7 8 a4: 0 1 a6: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0二、字符串 1.char
public static void main(String[] args) {
char s1 = 'A';
char s2 = 'u0041'; // java的char采用的时Unicode编码,占两个字节长度
char s3 = 'u6B22'; // 欢
char s4 = 'uu8FCE'; // 迎
System.out.println("s1:" + s1);
System.out.println("s2:" + s2);
System.out.println("s3:" + s3 + " s4:" + s4);
if (Character.isDigit('9')) {
System.out.println("9" + " isDigit");
}
}
输出
s1:A s2:A s3:欢 s4:迎 9 isDigit2.String
public static void main(String[] args) {
//1.String不是基本类型,是引用类型,String是Java中预定义的类
String message = "Welcome to java";
System.out.println("message:" + message);
System.out.println("The length of " + message + " is: " + message.length());
System.out.println(message + "charAt(5) is: " + message.charAt(5));
//2.连接字符串
String s1 = "message";
String s2 = "HTML";
String s3 = s1.concat(s2);
System.out.println("s3:" + s3);
String s4 = s1 + s2;
System.out.println("s4:" + s4);
//3.转换为小写
System.out.println("toLowerCase:" + s4.toLowerCase());
//4.转换维大写
System.out.println("toUpperCase:" + s4.toUpperCase());
//5.比较字符串
if (s4.equals(s3)) { // 不能使用==判断,==判断的是不是指向同一个对象
System.out.println("s4等于s3");
} else {
System.out.println("s4不等于s3");
}
System.out.println("s4.compareTo(s3):" + s4.compareTo(s3));
//6.获得字串
System.out.println("s4.substring(3): " + s4.substring(3));
System.out.println("s4.substring(3, 5): " + s4.substring(3, 5));
//7.查找字符
System.out.println("index of s is: " + s4.indexOf('s'));
System.out.println("index of s from index 3 is: " + s4.indexOf('s', 3));
System.out.println("lastIndexOf s is:" + s4.lastIndexOf('s'));
System.out.println("lastIndexOf s from index 3 is: " + s4.lastIndexOf('s', 2));
//8.查找子串
System.out.println("index of HTM is: " + s4.indexOf("HTM"));
System.out.println("index of HTM is: " + s4.indexOf("HTM", 2));
System.out.println("lastIndeOf HTM is: " + s4.lastIndexOf("HTM"));
System.out.println("lastIndeOf HTM is: " + s4.lastIndexOf("HTM", 2));
//9.字符串和数字转换
System.out.println(Integer.parseInt("1234"));
System.out.println(Double.parseDouble("3.1415926"));
System.out.println("转数字: " + 123);
}
输出
message:Welcome to java The length of Welcome to java is: 15 Welcome to javacharAt(5) is: m s3:messageHTML s4:messageHTML toLowerCase:messagehtml toUpperCase:MESSAGEHTML s4等于s3 s4.compareTo(s3):0 sageHTML sa index of s is: 2 index of s from index 3 is: 3 lastIndexOf s is:3 lastIndexOf s from index 3 is: 2 index of HTM is: 7 index of HTM is: 7 lastIndeOf HTM is: 7 lastIndeOf HTM is: -1 1234 3.1415926 转数字: 123三、接口
public class Solution {
public static void main(String[] args) {
//接口
// 创建实现类的对象
MathClass calc = new MathClass(100, 300);
System.out.println("100和300相加的结果是: " + calc.sum());
System.out.println("100和300,哪个大: " + calc.maxNum(100, 300));
}
}
// 创建一个名称为 IMath 的接口
interface IMath {
public int sum(); // 完成两个数的相加
public int maxNum(int a, int b); // 获取较大的数
}
class MathClass implements IMath {
private final int num1;
private final int num2;
// 构造方法
public MathClass(int num1, int num2) {
this.num1 = num1;
this.num2 = num2;
}
// 实现接口中的求和方法
@Override
public int sum() {
return num1 + num2;
}
// 实现接口中的获取较大数的方法
@Override
public int maxNum(int a, int b) {
return Math.max(a, b);
}
}
输出
100和300相加的结果是: 400 100和300,哪个大: 300四、List 1.ArrayList
package com.wuhen;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Solution {
public static void main(String[] args) {
Product pd1 = new Product(4, "木糖醇", 10);
Product pd2 = new Product(5, "洗发水", 12);
Product pd3 = new Product(3, "热水壶", 49);
List list = new ArrayList<>();
list.add(pd1);
list.add(pd2);
list.add(pd3);
System.out.println("*********商品信息**********");
for (int i = 0; i < list.size(); i++) {
Product product = list.get(i);
System.out.println(product);
}
List list1 = new ArrayList<>();
list1.add("One");
list1.add("|");
list1.add("Two");
list1.add("|");
list1.add("Three");
list1.add("|");
list1.add("Four");
Iterator it = list1.iterator();
while (it.hasNext()) {
System.out.print(it.next() + "、");
}
}
}
class Product {
private final int id;
private final String name;
private final float price;
public Product(int id, String name, float price) {
this.name = name;
this.id = id;
this.price = price;
}
public String toString() {
return "商品编号: " + id + ", 名称: " + name + ", 价格: " + price;
}
}
输出
*********商品信息********** 商品编号: 4, 名称: 木糖醇, 价格: 10.0 商品编号: 5, 名称: 洗发水, 价格: 12.0 商品编号: 3, 名称: 热水壶, 价格: 49.0 One、|、Two、|、Three、|、Four、2.LinkedList
package com.wuhen;
import java.util.Iterator;
import java.util.LinkedList;
public class Solution {
public static void main(String[] args) {
LinkedList products = new LinkedList<>();
String p1 = new String("六角螺母");
String p2 = new String("10A 电缆线");
String p3 = new String("5M 卷尺");
String p4 = new String("4CM 原木方板");
products.add(p1);
products.add(p2);
products.add(p3);
products.add(p4);
String p5 = new String("标准文件夹小柜");
products.addLast(p5);
System.out.println("*************商品信息**************");
System.out.println("目前商品有: ");
for (int i = 0; i < products.size(); i++) {
System.out.println(products.get(i));
}
System.out.println("n第一个商品的名称为: " + products.getFirst());
System.out.println("最后一个商品的名称为: " + products.getLast());
products.removeLast();
System.out.println("删除最后的元素,目前商品有: ");
for (int i = 0; i < products.size(); i++) {
System.out.println(products.get(i));
}
System.out.println("n在第0个位置添加元素:"扳手"");
products.add(0, "扳手");
Iterator it = products.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
输出
*************商品信息************** 目前商品有: 六角螺母 10A 电缆线 5M 卷尺 4CM 原木方板 标准文件夹小柜 第一个商品的名称为: 六角螺母 最后一个商品的名称为: 标准文件夹小柜 删除最后的元素,目前商品有: 六角螺母 10A 电缆线 5M 卷尺 4CM 原木方板 在第0个位置添加元素:"扳手" 扳手 六角螺母 10A 电缆线 5M 卷尺 4CM 原木方板五、Set 1.HashSet
package com.wuhen;
import java.util.HashSet;
import java.util.Iterator;
public class Solution {
public static void main(String[] args) {
HashSet courseSet = new HashSet<>();
String course1 = new String("Java入门教程");
String course2 = new String("Python入门教程");
String course3 = new String("C语言学习教程");
String course4 = new String("Golang入门教程");
courseSet.add(course1); // 将 course1 存储到 Set 集合中
courseSet.add(course2); // 将 course2 存储到 Set 集合中
courseSet.add(course3); // 将 course3 存储到 Set 集合中
courseSet.add(course4); // 将 course4 存储到 Set 集合中
System.out.println("C语言中文网教程有: ");
Iterator it = courseSet.iterator();
while (it.hasNext()) {
System.out.println("《" + it.next() + "》");
}
System.out.println("有" + courseSet.size() + "套精彩教程!");
}
}
输出
C语言中文网教程有: 《Java入门教程》 《Python入门教程》 《C语言学习教程》 《Golang入门教程》 有4套精彩教程!
2.TreeSet
package com.wuhen;
import java.util.Iterator;
import java.util.Scanner;
import java.util.SortedSet;
import java.util.TreeSet;
public class Solution {
public static void main(String[] args) {
TreeSet scores = new TreeSet(); // 创建 TreeSet 集合
Scanner input = new Scanner(System.in);
System.out.println("------------学生成绩管理系统-------------");
for (int i = 0; i < 5; i++) {
System.out.println("第" + (i + 1) + "个学生成绩:");
double score = input.nextDouble();
// 将学生成绩转换为Double类型,添加到TreeSet集合中
scores.add(Double.valueOf(score));
}
Iterator it = scores.iterator(); // 创建 Iterator 对象
System.out.println("学生成绩从低到高的排序为:");
while (it.hasNext()) {
System.out.print(it.next() + "t");
}
System.out.println("n请输入要查询的成绩:");
double searchScore = input.nextDouble();
if (scores.contains(searchScore)) {
System.out.println("成绩为: " + searchScore + " 的学生存在!");
} else {
System.out.println("成绩为: " + searchScore + " 的学生不存在!");
}
// 查询不及格的学生成绩
SortedSet score1 = scores.headSet(60.0);
System.out.println("n不及格的成绩有:");
for (int i = 0; i < score1.toArray().length; i++) {
System.out.print(score1.toArray()[i] + "t");
}
// 查询90分以上的学生成绩
SortedSet score2 = scores.tailSet(90.0);
System.out.println("n90 分以上的成绩有:");
for (int i = 0; i < score2.toArray().length; i++) {
System.out.print(score2.toArray()[i] + "t");
}
}
}
输出
------------学生成绩管理系统------------- 第1个学生成绩: 21.3 第2个学生成绩: 23.4 第3个学生成绩: 98.8 第4个学生成绩: 60 第5个学生成绩: 87.1 学生成绩从低到高的排序为: 21.3 23.4 60.0 87.1 98.8 请输入要查询的成绩: 23.4 成绩为: 23.4 的学生存在! 不及格的成绩有: 21.3 23.4 90 分以上的成绩有: 98.8



