一、API概述和使用步骤
API:
application programming interface,应用程序编程接口,程序员词典。
二、Scanner
Scanner类的功能,可以实现键盘输入数据到程序中。
获取键盘输入的一个int数字:int num = sc.nextInt();
获取键盘输入的一个字符串:String str = sc.next();
习题
键盘输入两个数字求和
答:
import java.util.Scanner;
public class ScannerSum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数字:");
int num = sc.nextInt();
System.out.println("请输入第二个数字:");
int num1 = sc.nextInt();
int sum = num + num1;
System.out.println("结果是"+sum);
}
}
键盘输入三个数字,取出最大值
import java.util.Scanner;
public class ScannerMax {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数字");
int a = sc.nextInt();
System.out.println("请输入第二个数字");
int b = sc.nextInt();
System.out.println("请输入第三个数字");
int c = sc.nextInt();
int temp = a > b ? a : b;
int max = temp > c ? temp : c;
System.out.println("最大值是" + max);
}
}
创建匿名对象
创建对象的标准格式:
类名称 对象名 = new 类名称();
匿名对象就是只有右边的对象,没有左边的名字和赋值运算符。
new 类名称();
注意事项:
匿名对象一次只能使用一次,下次使用不得不创建一个新的对象。
使用建议:
如果确定有一个对象只需要使用唯一的一次,就可以匿名对象。
public class Anonymous {
public static void main(String[] args) {
Name one = new Name();
one.name = "高圆";
one.showname();
new Name().name = "赵廷";
new Name().showname();
}
}
public class Name {
String name;
public void showname(){
System.out.println("我叫"+name);
}
}
匿名对象作为方法的参数和返回值
import java.util.Scanner;
public class Demo02Anonymous {
public static void main(String[] args) {
//普通使用方式
//Scanner sc = new Scanner(System.in);
//int num = sc.nextInt();
//匿名使用方式
//int num1 = new Scanner(System.in).nextInt();
//System.out.println("输入的是"+num1);
//使用一般方法写入参数
//Scanner sc = new Scanner(System.in);
//methodParam(sc);
//使用匿名方法进行传参;
methodParam(new Scanner(System.in));
}
public static void methodParam(Scanner sc) {
int num2 = sc.nextInt();//sc.nextInt()+alt+回车;
System.out.println(num2);
}
public static Scanner methodReturn() {
//Scanner sc = new Scanner(System.in);
//return sc;
return new Scanner(System.in);
}
}
三、Random的概述及其基本使用
Random类用于生成随机数字
1.导包 import java .util.Random
2.创建 Random r = new Random(); //小括号当中留空即可
3.使用:获取一个随机数字(范围是int所有范围)
int num = r.nextInt();
获取一个随机数字(参数代表了范围,左闭右开区间)
Int num = r.nextInt(3)
实际代表含义是[0,3),也就是0-2。
import java.util.Random;
public class Demo01Random {
public static void main(String[] args) {
Random r =new Random();
int num =r.nextInt();
System.out.println(num);
}
}
练习
生成1-n之间的随机数字
import java.util.Random;
public class PracticeRandom {
public static void main(String[] args) {
Random r = new Random();
int n =5;
for (int i = 0; i < 5000; i++) {
int i1 = r.nextInt(n) + 1;
System.out.println(i1);
}
}
}
练习
猜数字小游戏
import java.util.Random;
import java.util.Scanner;
public class Guess {
public static void main(String[] args) {
Random r = new Random();
int a = r.nextInt(100) + 1;
while (true) {
System.out.println("请输入数字:");
Scanner sc = new Scanner(System.in);
int b = sc.nextInt();
if (a == b) {
System.out.println("恭喜你,回答正确!");
break;
} else if (a > b) {
System.out.println("小了,小了");
} else if (a < b) {
System.out.println("大了,大了");
}
}
System.out.println("爽!");
}
}
题目:定义一个数组,用来存储三个Person对象
public class Demo1Array {
public static void main(String[] args) {
Person [] array = new Person[3];
Person one = new Person("迪丽热巴",20);
Person two = new Person("古力娜扎",18);
Person three = new Person("玛尔扎哈",220);
array[0] = one;
array[1] = two;
array[2] = three;
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的长度可以发生改变
ArrayList。
对于ArrayList来说,有一个尖括号代表泛型;
泛型:也就是装在集合中的所有元素,全都是统一的什么类型。
注意:泛型只能是引用类型,不能是基本类型。
//创建一个ArrayList集合,集合的名称是list,里面装的全都是String字符串类型的数据
ArrayList<String>list = new ArrayList<>();
注意事项
对于ArrayList来说,直接打印的不是地址值,而是内容,如果内容是空的,得到的将是中括号:【】。
import java.util.ArrayList;
public class ArrayList1 {
public static void main(String[] args) {
//创建一个ArrayList集合,集合的名称是list,里面装的全都是String字符串类型的数据
ArrayList<String>list = new ArrayList<>();
list.add("爽");
list.add("迪丽热巴");
list.add("古力娜扎");
list.add("玛尔扎哈");
System.out.println(list);
}
}
ArrayList常用方法
Public boolean add (E,e);向集合中添加元素,参数类型和泛型一致;
Public E get (int index);向集合中获取元素,参数是索引编号,返回值就是对应位置的元素
Public E remove(int index);从集合当中删除元素,参数是索引编号,返回值就是被删除的元素
Public int size();获取集合尺寸长度,返回值是集合中包含的元素个数。
import java.util.ArrayList;
public class ArrayListMethod {
public static void main(String[] args) {
ArrayList <String> list = new ArrayList<>();
list.add("柳岩");
list.add("高圆圆");
list.add("赵又廷");
list.add("贾乃亮");
list.add("李小璐");
/*System.out.println(list);
String name = list.get(4);
System.out.println("第四号索引位"+name);
String whoremoved = list.remove(4);
System.out.println("被删除的人" + whoremoved);
System.out.println(list);
int size = list.size();
System.out.println(size);*/
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
Arraylistlist = new Arraylist<>();
如果希望向集合Arraylist当中存储基本类型数据,必须使用基本类型对应的包装类。
基本类型 包装类
byte Byte
short Short
int Integer【特殊】
long Long
float Float
double Double
char Character【特殊】
boolean Boolean
//题目:生成六个1-33之间随机整数,添加到集合,并且遍历集合
//1.生成随机数使用Random,1-33闭区间
//2.添加到集合,使用list.add命令
//3.遍历集合,list.fori;soutlist.get(i)
public class Text01Arraylist {
public static void main(String[] args) {
Random r = new Random();//随机数格式
ArrayList<Integer>list = new ArrayList<>();//数组格式
for (int i = 0; i < 6; i++) {
int a = r.nextInt(33) + 1;//生成随机数
list.add(a);//将随机数添加到数列中
}
for (int i1 = 0; i1 < list.size(); i1++) {
System.out.println(list.get(i1));//遍历数列
}
}
}
//自定义四个学生对象,添加到集合并且遍历
//1.创建类2.创建集合,对象3.添加4.遍历
public class Text02Student {
public static void main(String[] args) {
ArrayList<Student>list =new ArrayList<Student>();
Student s1 = new Student("赵丽颖",88);
Student s2 = new Student("周杰伦",18);
Student s3 = new Student("周润发",28);
Student s4 = new Student("赵匡胤",188);
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
for (int i = 0; i < list.size(); i++) {
Student stu = list.get(i);
System.out.println(stu.getName()+stu.getAge());
}
}
}
//自定义四个学生对象,添加到集合并且遍历
//1.创建类2.创建集合,对象3.添加4.遍历
public class Text02Student {
public static void main(String[] args) {
ArrayList<Student>list =new ArrayList<Student>();
Student s1 = new Student("赵丽颖",88);
Student s2 = new Student("周杰伦",18);
Student s3 = new Student("周润发",28);
Student s4 = new Student("赵匡胤",188);
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
for (int i = 0; i < list.size(); i++) {
Student stu = list.get(i);
System.out.println(stu.getName()+stu.getAge());
}
}
}
import java.util.ArrayList;
//定义以指定格式打印集合的方法(Arraylist类型作为参数)
// 使用{}括起来,使用@分割每一个元素,格式参照{元素@元素@元素}
public class Text03Arratlist {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("张三丰");
list.add("张二丰");
list.add("张一丰");
System.out.println(list);
printArraylist(list);
}
public static void printArraylist(ArrayList<String> list) {
System.out.print("{");
for (int i = 0; i < list.size(); i++) {
String name = list.get(i);
if (i == list.size() - 1) {
System.out.print(name + "}");
} else {
System.out.print(name + "@");
}
}
}
}
import java.util.ArrayList;
import java.util.Random;
//题目:用一个大集合存入20个随机数字,然后筛选其中的偶数元素,放在小集合中
//要求使用自定义方法进行筛选
//分析:1.创建集合存储Random
//2.循环20次,把随机数字放在大集合
//3.定义一个集合进行筛选,筛选符合的元素
//三要素返回值类型:Arraylist()小集合
//返回值名称getSmalllist
//参数Arraylist()大集合
public class Text04Arraylist {
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();
biglist.add(num);
ArrayList<Integer> smalllist = getSmalllist(biglist);
for (int i2 = 0; i2 < smalllist.size(); i2++) {
System.out.println(smalllist.get(i2));
}
}
}
public static ArrayList<Integer> getSmalllist(ArrayList<Integer> biglist) {
ArrayList<Integer> smalllist = new ArrayList<>();
for (int i1 = 0; i1 < biglist.size(); i1++) {
int num = biglist.get(i1);
if (num % 2 == 0) {
smalllist.add(num);
}
}
return smalllist;
}
}