Scanner、Random、ArrayList

 1 package day05;
 2 
 3 import java.util.Scanner;
 4 
 5 public class MyDemoScanner {
 6 
 7     public static void main(String[] args) {
 8 
 9         Scanner sc = new Scanner(System.in);
10 
11         System.out.println("请输入数字: ");
12         int keyInput = sc.nextInt();
13         System.out.println("输入的数字为 " + keyInput);
14     }
15 }
 1 package day05;
 2 
 3 import java.util.Scanner;
 4 
 5 public class MyDemoNm {
 6 
 7     /**
 8      * 创建对象时,只有创建对象的语句,却没有把对象地址值赋值给某个变量。虽然是创建对象的简化写法,但是应用
 9      * 场景非常有限
10      *
11      * new 类名(参数列表);
12      * */
13 
14     public static void main(String[] args) {
15 
16         // new Scanner(System.in).nextInt();
17 
18         // 作为参数
19         input(new Scanner(System.in));
20 
21         // 作为返回值
22         Scanner sc = Sc();
23         sc.nextInt();
24     }
25 
26     /**
27      * 匿名对象可以作为方法的参数和返回值
28      * */
29     public static void input(Scanner sc) {
30         System.out.println(sc.nextInt());
31     }
32 
33     public static Scanner Sc(){
34         return new Scanner(System.in);
35     }
36 
37 }
 1 package day05;
 2 
 3 import java.util.Random;
 4 
 5 public class DemoR {
 6 
 7     public static void main(String[] args) {
 8         Random r = new Random();
 9 
10         for (int i = 0; i < 10 ; i++) {
11             int randomNum = r.nextInt(10) + 1;
12             System.out.println(randomNum);
13         }
14     }
15 }
 1 import java.util.ArrayList;
 2 
 3 public class DemoAL {
 4 
 5     /**
 6      * java.util.ArrayList 是大小可变的数组的实现,存储在内的数据称为元素。此类提供一些方法来操作内部存储
 7      * 的元素。  ArrayList 中可不断添加元素,其大小也自动增长。
 8      * */
 9 
10     public static void main(String[] args) {
11 
12         ArrayList<String> list = new ArrayList<>();
13 
14         String stu1 = "大爷1";
15         String stu2 = "大爷2";
16         String stu3 = "大爷3";
17 
18         list.add(stu1);
19         list.add(stu2);
20         list.add(stu3);
21         System.out.println(list);
22         System.out.println(list.get(0));
23         System.out.println(list.size());
24         list.remove(0);
25         System.out.println(list);
26 
27         for (int i = 0; i < list.size() ; i++) {
28             System.out.println(list.get(i));
29         }
30     }
31 
32 
33 }
原文地址:https://www.cnblogs.com/xiaoxiaolulu/p/11309944.html