Scanner类:
一个可以解析基本类型和字符串的简单文本扫描器。
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
匿名对象 :没有变量名的对象。
匿名对象的作用:
-
匿名对象可以作为方法的参数
-
匿名对象可以作为方法的返回值
-
弊端:匿名对象只能使用一次
格式:
new 类名(参数列表);
Random类:
Random r = new Random();
int i = r.nextInt();
生成伪随机数
产生[n,m]之间的随机数—> r.nextInt(m-n+1)+n
ArrayList类:
java.util.ArrayList 是大小可变的数组的实现,存储在内的数据称为元素.
自动装箱:基本数据类型可以自动包装为包装类类型
int num = 10;
Integer i = num;
自动拆箱:包装类类型可以自动拆箱为基本数据类型
Integter i = 20;
int num2 = i;
将"123"
字符串转换为int
类型。
static Integer valueOf(String s)
返回保存指定的 String 的值的 Integer 对象。
String str = "123";
//将字符串转化为Integer对象
Integer i = Integer.valueOf(str);
//利用自动拆箱转为int类型
int num = i;
.nextLine(); //换行符结束
.next(); //换行符结束 空格 tab
public class Test{
public static void main(String[] args){
//创建文本扫描器对象
Scanner scanner = new Scanner(System.in);
//通过nextLine()录入
System.out.println("请输入内容(nextLine()):");
String s2 = scanner.nextLine(); //换行符结束
//通过next()录入
System.out.println("请输入内容(next()):");
String s1 = scanner.next(); //换行符结束 空格 tab
//输出结果
System.out.println("--------------------------------");
System.out.println("通过nextLine()录入的结果为:"+s2);
System.out.println("通过next()录入的结果为:"+s1);
}
}
运行截图: