目录
Random
/*
Random类用来生成随机数字,使用起来也是3个步骤
1.导包
import java.util.Random;
2.创建
Random r=new Random();
3.使用
获取一个随机的int数字(范围是int所有范围,有正负两种):int num=r.nextInt()
获取一个随机的int数字(参数代表了范围,左闭右开区间):int num=r.nextInt(3),实际上代表的含义是:【0,3)
*/
public class Demo01Random {
public static void main(String[] args) {
Random r=new Random(3);
int num=r.nextInt();
System.out.println("随机数是:"+num);
}
}
public class Demo2Random {
public static void main(String[] args) {
Random r=new Random();
for (int i = 0; i < 100; i++) {
int num=r.nextInt(2);
System.out.println(num);
}
}
}
/*题目:用代码模拟猜数字的小游戏
* 思路:
* 1.首先需要产生一个随机数字,并且需要产生一个随机数字,并且一旦产生不再变化
* 2.需要键盘输入,所以用到Scanner
* 3.获取键盘输入的数字,用Scanner当中的nextInt方法
* 4.已经得到了两个数字,判断一下:
* 如果太大了,提示太大,并且重试
* 如果太小了,提示太小,并且重试
* 如果猜中了,游戏结束
* 5.重试就是再来一次,循环次数不确定,用while(true)*/
public class Demo04RandomGame {
public static void main(String[] args) {
Random r=new Random();
int randomNum=r.nextInt(100)+1;
Scanner sc=new Scanner(System.in);
while (true){
System.out.println("请输入你猜测的数字:");
int guessNum=sc.nextInt();//键盘输入猜测的数字
if (randomNum>guessNum){
System.out.println("太大了,请重试");
}
else if (randomNum<guessNum){
System.out.println("太小了,请重试");
}
else {
System.out.println("恭喜你,猜中了!");
break;
}
}
}
}
Array
/*题目:
* 定义一个数组,用来存储3个Person对象*/
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("古力娜扎",24);
Person three=new Person("木拉提",26);
//将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());
}
public class Person {
private String name;
private int age;
public Person(){
}
public Person(String name,int age){
this.name=name;
this.age=age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
ArrayList
/*
* ArrayList当中常用的方法有:
* public boolean add(E e):向集合中添加元素,参数的类型和泛型一样
* public E get(int index):从集合中获取元素,参数是索引编号,返回值就是对应位置的元素
* public E remove(int index):从集合中删除元素,参数是索引编号,返回值就是被删掉的元素
* public int size():获取集合的尺寸长度,返回值是集合中包含的元素个数*/
public class Demo03ArrayListMethod {
public static void main(String[] args) {
ArrayList<String> list=new ArrayList<>();
//向集合中添加元素
boolean success=list.add("柳岩");
System.out.println(list);
System.out.println("添加的动作是否成功?"+success);
}
}
/*数组的长度不可以发生改变
* 但是ArrayList集合的长度是可以随意变化的
* 对于ArrayList来说,有一个尖括号代表泛型
* 泛型:也就是装在集合中的所有元素,全都是统一的什么类型
* 注意:泛型只能是引用类型,不能是基本类型
*
* 对于ArrayList集合来说,直接打印得到的不是地址值,而是内容
* 如果内容是空,得到是空的中括号*/
public class Demo02ArrayList {
public static void main(String[] args) {
//创建了一个ArrayList集合,集合的名称是list,里面装的全都是String字符串类型的数据
ArrayList<String> list=new ArrayList<>();
System.out.println(list);
//向集合中添加一些数据,需要用到add方法
list.add("dfgh");
System.out.println(list);
list.add("但是风骨");
list.add("大家很反感");
list.add("看见个等级");
System.out.println(list);
}
}
public 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));
}
}
}
/*
* 如果希望向集合ArrayList中存储基本类型数据,必须使用基本类型对应的“包装类”
* 基本类型 包装类(引用类型,包装类都位于Java.lang包下)
* 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<Integer> list=new ArrayList<>();
list.add(100);
list.add(200);
System.out.println(list);
int num=list.get(1);
System.out.println(num);
ArrayList<Character> list1=new ArrayList<>();
list1.add('d');
System.out.println(list1);
}
}
/*
* 题目:
* 生成6个1~33之间的随机整数,添加到集合,并遍历集合
*
* 思路:
* 1.需要提供6个数字,创建一个集合
* 2.产生随机数,需要用到random
* 3.用循环6次来产生6个随机数字:for循环
* 4.循环内调用r.nextInt(int n)
* 5.把数字添加到集合中
* 6.遍历集合:for、size、get
* */
public class Demo01ArrayListRandom {
public static void main(String[] args) {
ArrayList<Integer> list=new ArrayList<>();
Random r=new Random();
for (int i = 0; i < 6; i++) {
int num=r.nextInt(33)+1;
list.add(num);
System.out.println(list.get(i));
}
}
}
/*
* 题目:
* 自定义4个学生对象,添加到集合,并遍历
*
* 思路:
* 1.自定义Student学生类,四个部分
* 2.创建一个集合,用来存储学生对象
* 3.根据类创建4个学生对象
* 4.将四个学生对象添加到集合中
* */
public class Demo02ArrayListStudent {
public static void main(String[] args) {
ArrayList<Student> students=new ArrayList<>();
Student one=new Student("jdh",34);
Student two=new Student("dhuwh",23);
Student three=new Student("hgfhh",53);
Student four=new Student("dfuguw",11);
students.add(one);
students.add(two);
students.add(three);
students.add(four);
for (int i = 0; i < students.size(); i++) {
Student student=students.get(i);
System.out.println("学生姓名:"+student.getName()+','+"学生年纪:"+student.getAge());
}
}
}
/*
* 题目:
* 定义指定格式打印集合的方法(ArrayList类型作为参数),使用{}扩起每个集合,使用@分隔每个集合
* System.out.println(list); [10,20,30]
* printArrayList(list) {10@20@30}*/
public class Demo03ArrayListPrint {
public static void main(String[] args) {
ArrayList<String> list=new ArrayList<>();
list.add("赵武");
list.add("张飒");
list.add("王博");
list.add("钱枫苏");
list.add("扶苏");
list.add("周雾雾");
System.out.println(list);
/*
* 定义方法的三要素
* 返回值类型:只是进行打印,没有运算,没有结果
* 方法名称:printArrayList
* 参数列表:ArrayList*/
System.out.print('{');
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i));
if (i!=list.size()-1)
System.out.print('@');
}
System.out.println('}');
}
}
/*题目:
* 用一个大集合存入随机20个数字,然后筛选其中的偶数元素,放到小集合中
* 要求用自定义的方法来实现筛选
*
* 分析:
* 1.需要创建一个大集合,用来存储int数字
* 2.随机数字就用Random nextint
* 3.循环20次,把随机数字放入大集合中
* 4.定义一个方法,筛选符合要求的元素,得到小集合
* 5.判断是偶数
* 6.如果是偶数就放到小集合中,否则不放
* */
public class Demo4ArrayListReturn {
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(300)+1;
biglist.add(num);
}
ArrayList<Integer> smalllist=getSmallList(biglist);
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<Integer>();
for (int i = 0; i < biglist.size(); i++) {
int num=biglist.get(i);
if (num%2==0){
smalllist.add(num);
}
}
return smalllist;
}
}
String
/*
* 1.字符串的内容永不可变
* 2.正是因为字符串不可改变,所以字符串是可以共享使用的
* 3.字符串效果上相当于是char[]字符数组,但是底层原理是byte[]字节数组
*
* 创建字符串的常见3+1Z种方式
* 三种构造方法:
* public String():创建一个空白字符串,不含有任何内容
* public String(char[] array):根据字符数组的内容来创建对应的字符串
* public String(byte[] array):根据字符数组的内容来创建对应的字符串*/
public class Demo01String {
public static void main(String[] args) {
//使用空参构造
String str1=new String();
System.out.println("第一个字符串:"+str1);
//根据字符数组创建字符串
byte[] charArray={'A','B','C'};
String str2=new String(charArray);
System.out.println("第2个字符串:"+str2);
//根据字节数组创建字符串
byte[] byteArray={97,98,99};
String str3=new String(byteArray);
System.out.println("第3个字符串:"+str3);
}
}
/*
* public boolean equals(Object obj):参数可以是任何对象,只有参数是一个字符串并且内容相同的才会给true;否则返回false
* 备注:任何对象都能用Object进行接收
*
* 注意事项:
* 1.任何对象都能用Object进行接收
* 2.equals方法具有对称性,也就是a.equals(b)和b.equals(a)效果一样
* 3.如果比较双方一个变量一个常量,推荐把常量字符串写在前面
* */
public class Demo01StringEquals {
public static void main(String[] args) {
String str1="Hello";
String str2="Hello";
char[] charArray={'H','e','l','l','o'};
String str3=new String(charArray);
System.out.println(str1.equals(str2));
System.out.println(str2.equals(str3));
System.out.println(str3.equals("Hello"));
System.out.println("Hello".equals(str1));
String str4="Hello";
System.out.println(str1.equals(str4));
System.out.println("===========");
String str5="abc";
System.out.println("abc".equals(str5));
String strA="Java";
String strB="java";
System.out.println(strA.equals(strB));
System.out.println(strA.equalsIgnoreCase(strB));//忽略大小写
System.out.println("abc-123".equalsIgnoreCase("abc壹123"));
}
}
/*
* Stringd当中与转换相关的常用方法有:
*
* public char[] toCharArray():将当前字符串拆分成字符串作为返回值
* public byte[] getBytes():将获得当前字符串底层的字节数组
* public String replace(Sequence oldString,CharSequence newString):
* 将所有出现的老字符串替换称为新的字符串,返回替换之后的结果新字符串*/
public class Demo04StringConvert {
public static void main(String[] args) {
//转换成为字符数组
char[] chars="Hello".toCharArray();
System.out.println(chars[0]);
System.out.println(chars.length);
System.out.println("==============");
//转换成为字节数组
byte[] bytes="abc".getBytes();
for (int i = 0; i < bytes.length; i++) {
System.out.println(bytes[i]);
}
System.out.println("================");
String str1="How do you do?";
String str2=str1.replace("o","*");
System.out.println(str1);
System.out.println(str2);
String str3="白日依山尽,黄河入海流";
String str4=str3.replace("黄","彩");
System.out.println(str4);
}
}
/*
* 分割字符串的方法:
* public String[] split(String regex):
* 按照参数的规则,将字符串切分称为若干部分*/
public class Demo05StringSplit {
public static void main(String[] args) {
String str1="aaa,bbb,ccc";
String[] array1=str1.split(",");
for (int i = 0; i < str1.length(); i++) {
System.out.println(array1[i]);
}
// System.out.println("==============");
String str2="aaa bbb ccc";
String[] array2=str2.split(" ");
for (int i = 0; i < array2.length; i++) {
System.out.println(array2[i]);
}
// System.out.println("===============");
String str3="XXX.YYY.ZZZ";
String[] array3=str3.split(".");
for (int i = 0; i < array3.length; i++) {
System.out.println(array3[i]);
}
}
}
/*
* 题目:
* 定义一个方法,把数组{1,2,3}按照指定格式拼接成一个字符串。格式参照:[word1#word2#word3]
* 分析:
* 1.首先准备一个int[]数组,内容是1、2、3
* 2.定义一个方法,用来将数组变成字符串
* 三要素:
* 返回值类型:String
* 方法名称:fromArrayToString
* 参数列表:int[]
* 3.格式:[word1#word2#word3]
* 4.调用方法,得到返回值,并打印结果字符串*/
public class Demo06StringPractise {
public static void main(String[] args) {
int[] array={1,2,3};
System.out.println(fromArrayToString(array));
}
public static String fromArrayToString(int[] array){
String str="[";
for (int i = 0; i < array.length; i++) {
if (i==array.length-1){
str+="word"+array[i]+"]";
}
else str+="word"+array[i]+"#";
}
return str;
}
}
/*
* 题目:
* 键盘输入一个字符串,并且统计其中各种字符出现的次数
* 种类有:大写字母、小写字母、数字、其他
*
* 思路:
* 1.既然用到键盘输入,肯定有Scanner
* 2.键盘输入的是字符串
* 3.定义四个变量,分别代表四种字符各自的出现次数
* 4.需要对字符串逐字检查,String-->char[],方法就是toCharArray()
* 5.遍历char[]字符数组,对当前字符的种类进行判断,并且用四个变量进行++
* 6.遍历输出四个变量,分别代表四种字符出现次数*/
public class Demo07StringCount {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个字符串:");
String input=sc.next();//获取键盘输入的一个字符串
int countUpper=0;
int countLower=0;
int countNumber=0;
int countOther=0;
char[] charArray=input.toCharArray();
for (int i = 0; i < charArray.length; i++) {
char ch=charArray[i];
if ('A'<=ch&&ch<='Z')
countUpper++;
else if ('a'<=ch&&ch<='z')
countLower++;
else if ('0'<=ch&&ch<='9')
countNumber++;
else countOther++;
}
System.out.println("大写字母有:"+countUpper);
System.out.println("小写字母有:"+countLower);
System.out.println("数字有:"+countNumber);
System.out.println("其他字符有:"+countOther);
}
}
Static静态
/*如果一个成员变量使用了static关键字,那么这个变量不再属于变量自己,而是属于所在的类,多个对象共享同一个数据*/
public class Student {
private int id;
private String name;
private int age;
static String room;
private static int idCounter=0;
public Student(String name) {
idCounter++;
}
public Student(String name, int age) {
this.name = name;
this.age = age;
this.id=++idCounter;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class Demo01StaticField {
public static void main(String[] args) {
/* Student one=new Student("郭靖",19);
one.room="101教室";
System.out.println("姓名:"+one.getName()+" 年龄:"+one.getAge()+" 教室:"+one.room+" 学号:"+one.getId());
Student two=new Student("黄蓉",20);
System.out.println("姓名:"+two.getName()+" 年龄:"+two.getAge()+" 教室:"+two.room+" 学号:"+two.getId());*/
Student two=new Student("黄蓉",20);
two.room="101教室";
System.out.println("姓名:"+two.getName()+" 年龄:"+two.getAge()+" 教室:"+two.room+" 学号:"+two.getId());
Student one=new Student("郭靖",19);
System.out.println("姓名:"+one.getName()+" 年龄:"+one.getAge()+" 教室:"+one.room+" 学号:"+one.getId());
}
}
/*一旦使用static修饰成员方法,那么就成为了静态方法,静态方法不属于对象,二是属于类的
* 如果没有static关键字,那么必须首先创建对象,然后通过对象才能使用它
* 如果有了static关键字,那么不需要创建对象,直接就能通过类名称来使用它
*
* 无论是成员变量还是成员方法,如果有了static都推荐使用类名称进行调用
* 静态变量:类名称.静态变量()
* 静态方法:类名称.静态方法()
*
* 注意事项:
* 1.静态不能访问非静态,只能访问静态
* 原因:在内存中先有的静态内容,后有的非静态内容
* 2.静态方法当中不能用this
* 原因:this代表当前对象,通过谁调用的方法,谁就是当前对象*/
public class MyClass {
int num;
static int numStatic;
//成员方法
public void method() {
System.out.println("这是一个普通的成员方法。");
//成员方法可以访问成员变量
System.out.println(num);
//成员方法可以访问静态变量
System.out.println(numStatic);
}
//静态方法
public static void methodStatic(){
System.out.println("这是一个静态方法。");
//静态方法可以访问静态变量
System.out.println(numStatic);
//静态方法不能访问非静态变量
/*错误写法 System.out.println(num);*/
/*错误写法 System.out.println(this);*/
}
}
public class Demo2StaticMethod {
public static void main(String[] args) {
MyClass obj=new MyClass();//首先创建对象
//然后才能使用没有static关键字的内容
obj.method();
//对于静态方法来说,可以通过对象名进行调用,也可以直接通过类名称来调用
obj.methodStatic();//正确,不推荐,这种写法在编译之后也会被javac翻译成为“类名称.静态方法名”
MyClass.methodStatic();//正确,推荐
}
public static void myMethod(){
System.out.println("这是自己的方法!");
}
}
Array&Math
/*
* java.util.Array是一个与数组相关的工具类,里面提供了大量静态方法,用来实现数组常见的操作
*
* public static String toString(数组):将参数数组变成字符串(按照默认格式:[元素1,元素2,元素3...])
* public static void sort(数组):按照默认升序(从小到大)对数组的元素进行排序
*
* 备注:
* 1.如果是数值,sort默认按升序从小到大排序
* 2.如果是字符串,sort默认按照字母升序
* 3.如果是自定义的类型,那么这个自定义的类需要有Comparator或者Comparator接口的支持*/
public class Demo01Arrays {
public static void main(String[] args) {
int[] intArray={10,20,30};
//将int数组按照默认格式变成字符串
String intStr= Arrays.toString(intArray);
System.out.println(intStr);
int[] array1={2,1,3,6,4};
Arrays.sort(array1);
System.out.println(Arrays.toString(array1));
String[] array2={"aaa","ccc","bbb"};
Arrays.sort(array2);
System.out.println(Arrays.toString(array2));
}
}
/*
* 题目:
* 请使用Arrays相关的API,将一个随机字符串中的所有字符升序排列,并倒序打印*/
public class Demo02ArraysPractise {
public static void main(String[] args) {
String str="asdfghjkl982745";
char[] chars=str.toCharArray();
Arrays.sort(chars);
for (int i = 0; i < chars.length; i++) {
System.out.println(chars[i]);
}
}
}
/*
* java.util.Math类是数学相关的工具类,里面提供了大量的静态方法,完成与数学相关的操作
*
* public static double abs(double num):获取绝对值
* public static double ceil(double num):向上取整
* public static double floor(double num):向下取整
* public static long round(double num):四舍五入*/
public class Demo03Math {
public static void main(String[] args) {
//取绝对值
System.out.println(Math.abs(-3.14));
System.out.println(Math.abs(3.14));
//向上取整
System.out.println(Math.ceil(5.23));
System.out.println(Math.ceil(5.84));
//向下取整
System.out.println(Math.floor(6.85));
System.out.println(Math.floor(6.33));
//四舍五入
System.out.println(Math.round(5.4));
System.out.println(Math.round(6.8));
}
}
/*
* 题目:
* 计算在-10.8到5.9之间绝对值大于6或者小于2.1的整数有多少个*/
public class Demo04MathPractise {
public static void main(String[] args) {
double min=-10.8;
double max=5.9;
for (int i = (int)min; i <(int)max ; i++) {
int abs=Math.abs(i);
if (abs>6||abs<2.1){
System.out.println(i);
}
}
}
}