一、匿名对象
1. 定义格式
new 类名();
访问成员
new 类名().成员
2.注意事项
匿名对象建议只在使用一次的时候去创建匿名对象
3.使用场景
可以用于作为方法的参数或者返回值使用。但是建议只在使用一次的时候选择匿名对象的方式
public class Demo02Anonymous {
public static void main(String[] args) {
// 普通使用方式
// Scanner sc = new Scanner(System.in);
// int num = sc.nextInt();
// 匿名对象的方式
// int num = new Scanner(System.in).nextInt();
// System.out.println("输入的是:" + num);
// 使用一般写法传入参数
// Scanner sc = new Scanner(System.in);
// methodParam(sc);
// 使用匿名对象来进行传参
// methodParam(new Scanner(System.in));
Scanner sc = methodReturn();
int num = sc.nextInt();
System.out.println("输入的是:" + num);
}
public static void methodParam(Scanner sc) {
int num = sc.nextInt();
System.out.println("输入的是:" + num);
}
public static Scanner methodReturn() {
// Scanner sc = new Scanner(System.in);
// return sc;
return new Scanner(System.in);
}
}
二、Random
1.Random基本使用格式
导包:import java.util.Random;
创建对象:Random r = new Random();
调用方法:nextInt();
2.生成一个指定范围的随机数
调用方法:nextInt(num);
范围:包含0,不包含num 0~num-1
3.生成一个1-n之间的随机数
nextInt(n)+1;
例如:
nextInt(10)+1; // 1-10
4.猜数字小游戏案例
public class Demo04RandomGame {
public static void main(String[] args) {
Random r = new Random();
int randomNum = r.nextInt(100) + 1; // [1,100]
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请输入你猜测的数字:");
int guessNum = sc.nextInt(); // 键盘输入猜测的数字
if (guessNum > randomNum) {
System.out.println("太大了,请重试。");
} else if (guessNum < randomNum) {
System.out.println("太小了,请重试。");
} else {
System.out.println("恭喜你,猜中啦!");
break; // 如果猜中,不再重试
}
}
System.out.println("游戏结束。");
}
}
5.通过随机数给数组赋值
public class Test01 {
public static void main(String[] args) {
//创建一个长度为10的数组
int[] arr = new int[10];
//创建随机数对象
Random r = new Random();
//遍历数组
for (int i = 0; i < arr.length; i++) {
//生成1-100之间的随机数
int num = r.nextInt(100)+1;
//将数字保存到数组中
arr[i] = num;
}
//打印数组元素
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
6.完成一个随机点名器
public class Test02 {
public static void main(String[] args) {
String[] arr = {"张三","李四","王五","赵六","周七","王老八"};
System.out.println("班级中一共有:" + arr.length + "个人");
Random r = new Random();
//生成随机数
int index = r.nextInt(arr.length);
System.out.println("本次抽中的人员为:" + arr[index]);
}
}
三、对象数组
1.对象数组的使用
public class Demo01Array {
public static void main(String[] args) {
// 首先创建一个长度为3的数组,里面用来存放Person类型的对象
Person[] array = new Person[3];
Person one = new Person("迪丽热巴", 18);
Person two = new Person("古力娜扎", 28);
Person three = new Person("玛尔扎哈", 38);
// 将one当中的地址值赋值到数组的0号元素位置
array[0] = one;
array[1] = two;
array[2] = three;
System.out.println(array[0]); // 地址值
System.out.println(array[1]); // 地址值
System.out.println(array[2]); // 地址值
System.out.println(array[1].getName()); // 古力娜扎
}
}
四、集合
1.基本的使用格式
public class Demo02ArrayList {
public static void main(String[] args) {
// 创建了一个ArrayList集合,集合的名称是list,里面装的全都是String字符串类型的数据
// 备注:从JDK 1.7+开始,右侧的尖括号内部可以不写内容,但是<>本身还是要写的。
ArrayList<String> list = new ArrayList<>();
System.out.println(list); // []
// 向集合当中添加一些数据,需要用到add方法。
list.add("赵丽颖");
System.out.println(list); // [赵丽颖]
list.add("迪丽热巴");
list.add("古力娜扎");
list.add("玛尔扎哈");
System.out.println(list); // [赵丽颖, 迪丽热巴, 古力娜扎, 玛尔扎哈]
// list.add(100); // 错误写法!因为创建的时候尖括号泛型已经说了是字符串,添加进去的元素就必须都是字符串才行
}
}
add(E e);向集合中添加数据
get(int index);获取集合中指定索引上的元素
remove(int index);删除指定索引上的元素
set(int index,E e);修改指定索引上的元素
size();获取集合的长度
示例代码:
public class Demo03ArrayListMethod {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
System.out.println(list); // []
// 向集合中添加元素:add
boolean success = list.add("柳岩");
System.out.println(list); // [柳岩]
System.out.println("添加的动作是否成功:" + success); // true
list.add("高圆圆");
list.add("赵又廷");
list.add("李小璐");
list.add("贾乃亮");
System.out.println(list); // [柳岩, 高圆圆, 赵又廷, 李小璐, 贾乃亮]
// 从集合中获取元素:get。索引值从0开始
String name = list.get(2);
System.out.println("第2号索引位置:" + name); // 赵又廷
// 从集合中删除元素:remove。索引值从0开始。
String whoRemoved = list.remove(3);
System.out.println("被删除的人是:" + whoRemoved); // 李小璐
System.out.println(list); // [柳岩, 高圆圆, 赵又廷, 贾乃亮]
// 获取集合的长度尺寸,也就是其中元素的个数
int size = list.size();
System.out.println("集合的长度是:" + size);
//将贾乃亮 修改成 小猪佩奇 set()
list.set(3,"小猪佩奇");
System.out.println(list);
}
}c class Demo04ArrayListEach {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("迪丽热巴");
list.add("古力娜扎");
list.add("玛尔扎哈");
// 遍历集合
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
4.集合中保存基本数据类型
基本数据类型四类八种所对应的包装类
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
示例代码:
public class Demo05ArrayListBasic {
public static void main(String[] args) {
ArrayList<String> listA = new ArrayList<>();
// 错误写法!泛型只能是引用类型,不能是基本类型
// ArrayList<int> listB = new ArrayList<>();
ArrayList<Integer> listC = new ArrayList<>();
listC.add(100);
listC.add(200);
System.out.println(listC); // [100, 200]
int num = listC.get(1);
System.out.println("第1号元素是:" + num);
}
}
5.集合练习-删除集合中的指定内容
public class Test03 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("abc");
list.add("def");
list.add("def");
list.add("ghi");
list.add("def");
list.add("okl");
//2.删除集合中所有的def字符串
for (int i = 0; i < list.size(); i++) {
String s = list.get(i);
//如果当前s这个字符串要是def的话。就删除
if(s.equals("def")) {
list.remove(i--);
}
}
//输出
System.out.println(list);
}
}
6.集合练习-通过随机数为集合赋值
public class Demo01ArrayListRandom {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
Random r = new Random();
while(list.size() != 6) {
int num = r.nextInt(33) + 1;
list.add(num);
}
/*for (int i = 0; i < 6; i++) {
int num = r.nextInt(33) + 1;
list.add(num);
}*/
// 遍历集合
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
7.集合练习-向集合中存储自定义对象
public class Demo02ArrayListStudent {
public static void main(String[] args) {
ArrayList<Student> list = new ArrayList<>();
Student one = new Student("洪七公", 20);
Student two = new Student("欧阳锋", 21);
Student three = new Student("黄药师", 22);
Student four = new Student("段智兴", 23);
list.add(one);
list.add(two);
list.add(three);
list.add(four);
// 遍历集合
for (int i = 0; i < list.size(); i++) {
Student stu = list.get(i);
System.out.println("姓名:" + stu.getName() + ",年龄" + stu.getAge());
}
}
}
8.集合练习-使用指定格式打印集合数据
public class Demo03ArrayListPrint {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("张三丰");
list.add("宋远桥");
list.add("张无忌");
list.add("张翠山");
System.out.println(list); // [张三丰, 宋远桥, 张无忌, 张翠山]
//System.out.println(Integer.toHexString(list.hashCode()));
printArrayList(list);
}
/*
定义方法的三要素
返回值类型:只是进行打印而已,没有运算,没有结果;所以用void
方法名称:printArrayList
参数列表:ArrayList
*/
public static void printArrayList(ArrayList<String> list) {
//System.out.println(Integer.toHexString(list.hashCode()));
// {10@20@30}
System.out.print("{");
for (int i = 0; i < list.size(); i++) {
String name = list.get(i);
if (i == list.size() - 1) {
System.out.println(name + "}");
} else {
System.out.print(name + "@");
}
}
}
}
9.集合练习-筛选集合中指定的数据
public class Demo04ArrayListReturn {
public static void main(String[] args) {
ArrayList<Integer> bigList = new ArrayList<>();
Random r = new Random();
for (int i = 0; i < 20; i++) {
int num = r.nextInt(100) + 1; // 1~100
bigList.add(num);
}
ArrayList<Integer> smallList = getSmallList(bigList);
System.out.println("偶数总共有多少个:" + smallList.size());
for (int i = 0; i < smallList.size(); i++) {
System.out.println(smallList.get(i));
}
}
// 这个方法,接收大集合参数,返回小集合结果
public static ArrayList<Integer> getSmallList(ArrayList<Integer> bigList) {
// 创建一个小集合,用来装偶数结果
ArrayList<Integer> smallList = new ArrayList<>();
//遍历传入进来的集合
for (int i = 0; i < bigList.size(); i++) {
//获取每一个数字
int num = bigList.get(i);
//如果当前数字是偶数
if (num % 2 == 0) {
//添加到smallList集合当中
smallList.add(num);
}
}
return smallList;
}
}
五、扩展的知识
1.生成指定的随机数
public static void demo01() {
Random r = new Random();
//生成20-30之间的数字
for (int i = 0; i < 200; i++) {
int num = r.nextInt(11) + 20; // 0 ~ 10
System.out.println(num);
}
//生成1-100之间的偶数 1*2=2 3*2=6 4*2=8
for (int i = 0; i < 200; i++) {
int num = (r.nextInt(50) + 1) * 2; // 1-50之间的随机数
System.out.println(num);
}
}
2.字符串和数字之间的转换
public static void demo02() {
//如何将数字变成一个字符串
int num1 = 10;
String s1 = num1 + "";
System.out.println(s1 + 5);
//如何将一个字符串类型的数字。转换成int类型的数字
String s2 = "456";
int num2 = Integer.parseInt(s2);
System.out.println(num2 + 5);
}
3.集合的补充方法
boolean addAll(Collection<? extends E> c) :
将集合中的内容全部添加到另一个集合中
void clear() : 清空集合中所有的元素
boolean contains(Object o) : 判断集合中是否包含传入的元素
int indexOf(Object o) :
获取传入的元素在集合中第一次出现的索引位置,如果不存在则返回-1
int lastIndexOf(Object o) : 获取传入的元素在集合中最后一次出现的索引位置
boolean isEmpty() : 判断集合是否为空
示例代码:
public static void main(String[] args) {
//集合补充常用的方法
//addAll()
ArrayList<String> list = new ArrayList<>();
list.add("abc");
list.add("bcd");
list.add("ghi");
list.add("abc");
ArrayList<String> newList = new ArrayList<>();
newList.addAll(list);
System.out.println(newList); // abc, bcd, ghi
//clear()
//newList.clear();
//System.out.println(newList);
//contains
boolean flag = newList.contains("abcc");
System.out.println(flag);
//indexOf
int index = newList.indexOf("abc");
System.out.println(index);
//lastIndexof()
index = newList.lastIndexOf("abc");
System.out.println(index);
// isEmpty()
//newList.clear();
//System.out.println(newList.isEmpty());
}
4.加标记思想、return语句的巧用
public class Demo03 {
public static void main(String[] args) {
String[] arr = {"a","b","c","d","e"};
//return语句的巧用
//print(arr);
//加标记思想
/* boolean flag = false;
for (int i = 0; i < arr.length; i++) {
if(arr[i].equals("c")) {
flag = true;
}
}
if(flag == true) {
System.out.println("数组中包含c这个字符串");
}else {
System.out.println("数组中不包含c这个字符串");
}*/
}
public static void print(String[] arr) {
for (int i = 0; i < arr.length; i++) {
if(arr[i].equals("c")) {
System.out.println("数组中包含c这个字符串");
return;
}
}
System.out.println("数组中不包含c这个字符串");
}
}
“` 5.debug的模式使用对象内存图产品![ 这里写图片描述] (https://img-blog.csdn.net/20180720182549931?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ptajE4NzU2MjM1NTE4/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70 )