1.API

  1. API 概述:

    • API(Application Programming Interface),应用程序编程接口。Java API是一本程序员的 字典 ,是JDK中提供给我们使用的类的说明文档。这些类将底层的代码实现封装了起来,我们不需要关心这些类是如何实现的,只需要学习这些类如何使用即可。所以我们可以通过查询API的方式,来学习Java提供的类,并得知如何使用它们。
    • 所谓的API :就是里面有好多类,好多方法.为我们量身定制的字典.
  2. API 的使用步骤:
    1. 打开帮助文档。
    2. 点击显示,找到索引,看到输入框。
    3. 你要找谁?在输入框里输入,然后回车。
    4. 看包。java.lang下的类不需要导包,其他需要。
    5. 看类的解释和说明。
    6. 学习构造方法
    7. 使用成员方法

2.Scanner类

  1. Scanner类的概述:

    Scanner类的功能:可以实现键盘输入数据,到程序当中。

  2. 引用类型的使用步骤

    1. 导包
      import 包路径.类名称;
      如果需要使用的目标类,和当前类位于同一个包下,则可以省略导包语句不写
      只有java.lang包下的内容不需要导包,其他的包都需要import语句。

    2. 创建
      类名称 对象名 = new 类名称();

    3. 使用
      对象名.成员方法名()

  3. Scanner 类的使用步骤

    获取键盘输入的一个int数字:int num = sc.nextInt();
    获取键盘输入的一个字符串:String str = sc.next();

    import java.util.Scanner; // 1. 导包
    public class Demo01Scanner {public static void main(String[] args) {// 2. 创建// 备注:System.in代表从键盘进行输入Scanner sc = new Scanner(System.in);// 3. 获取键盘输入的int数字int num = sc.nextInt();System.out.println("输入的int数字是:" + num);// 4. 获取键盘输入的字符串String str = sc.next();System.out.println("输入的字符串是:" + str);}}
  4. 练习

    1. 键盘输入三个int数字,然后求出其中的最大值。

      分析:

      1. 既然是键盘输入,肯定需要用到Scanner

      2. Scanner三个步骤:导包、创建、使用nextInt()方法

      3. 既然是三个数字,那么调用三次nextInt()方法,得到三个int变量

      4. 无法同时判断三个数字谁最大,应该转换成为两个步骤:
        4.1 首先判断前两个当中谁最大,拿到前两个的最大值
        4.2 拿着前两个中的最大值,再和第三个数字比较,得到三个数字当中的最大值

      5. 打印最终结果

      public class Demo03ScannerMax {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);}}
    2. 键盘输入两个int数字,并且求出和值。

      分析 :

      1. 既然需要键盘输入,那么就用Scanner

      2. Scanner的三个步骤:导包、创建、使用

      3. 需要的是两个数字,所以要调用两次nextInt方法

      4. 得到了两个数字,就需要加在一起。

      5. 将结果打印输出。

      public class Demo02ScannerSum {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();int result = a + b;System.out.println("结果是:" + result);}}
  5. 补充 . 匿名对象

    1. 标准创建对象格式

      类名称 对象名 = new 类名称();
      
    2. 匿名对象

      匿名对象就是只有右边的对象,没有左边的名字和赋值运算符。
      格式: new 类名称();
      
    3. 注意事项:

      匿名对象只能使用唯一的一次,下次再用不得不再创建一个新对象。
      使用建议:如果确定有一个对象只需要使用唯一的一次,就可以用匿名对象。

    4. 代码 :

      public class Person {String name;public void showName() {System.out.println("我叫:" + name);}}
      public class Demo01Anonymous {public static void main(String[] args) {// 左边的one就是对象的名字Person one = new Person();one.name = "高圆圆";one.showName(); // 我叫高圆圆System.out.println("===============");// 匿名对象new Person().name = "赵又廷";new Person().showName(); // 我叫:null}}
    5. 匿名对象可以作为方法的参数和返回值

      1. 作为方法的参数 \ 作为返回值

        import java.util.Scanner;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);}}

3.Random类

  1. Random类的概述:

    Random类用来生成随机数字.

  2. Random 类的使用步骤

    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),也就是0~2
    public class Demo01Random {public static void main(String[] args) {Random r = new Random();//获取一个随机的int数字(范围是int所有范围,有正负两种):int num = r.nextInt()int num = r.nextInt();System.out.println("随机数是:" + num);}}
    
  3. 生成指定范围的随机数

    获取一个随机的int数字(参数代表了范围,左闭右开区间):int num = r.nextInt(3)   范围 就是 [0,2)
    调用 r.nextInt(给定参数);方法
    
  4. 练习

    1. 根据给定 int类型的值 n , 生成从[1 , x) 的随机数

      public class DemoRandom {public static void main(String[] args) {generationRanom(100);}private static void generationRanom(int x) {for (int i = 0; i < 100; i++) {int nextInt = new Random().nextInt(x) + 1;  // 生成 : [1 , x) 的随机数System.out.println(nextInt);}}
    2. 用代码模拟猜数字的小游戏。数字范围 [1 ,100)

      思路:

      1. 首先需要产生一个随机数字,并且一旦产生不再变化。用Random的nextInt方法

      2. 需要键盘输入,所以用到了Scanner

      3. 获取键盘输入的数字,用Scanner当中的nextInt方法

      4. 已经得到了两个数字,判断(if)一下:
        如果太大了,提示太大,并且重试;
        如果太小了,提示太小,并且重试;
        如果猜中了,游戏结束。

      5. 重试就是再来一次,循环次数不确定,用while(true)。

      public class GuessGame {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("游戏结束。");}}
      
    3. 给定验证码的长度 ,随机生成一个 x 长度的验证码 ;

      public class GenerateRandomCode {public static void main(String[] args) {System.out.println(GenerateRandomCode.GenerateVerificationCode(8));}//产生验证码的逻辑 生成数字 或者 生成 字母public static String GenerateVerificationCode(int n){//定义一个接收生成的字符串String val = "";Random random = new Random();for (int i = 0; i < n; i++) {//随机产生一个 0 | 1 的数字 ,如果是 0 则产生 数字 ,如果是 1 则产生 字符 .String s = random.nextInt(2) % 2 == 0 ? "number" : "char";// 产生数字和字母交替的目的,产生字母的情况if("char".equalsIgnoreCase(s)){//生成字母,int i1 = random.nextInt(2) % 2 == 0 ? 65 : 97;val += (char) (i1 + random.nextInt(26));}else if ("number".equalsIgnoreCase(s)){// 产生数字  数字转String  ---> String.valueOf(...)val += String.valueOf(random.nextInt(10));}}return val;}

4.ArrayList类

  1. 知识回顾 :

    • 定义一个数组,用来存储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("古力娜扎", 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()); // 古力娜扎}}
    
  2. ArrayList集合

    几点声明:

    ​ 数组的长度不可以发生改变。
    ​ 但是ArrayList集合的长度是可以随意变化的。

    ​ 对于ArrayList来说,有一个尖括号代表泛型。
    ​ 泛型:也就是装在集合当中的所有元素,全都是统一的什么类型。
    ​ 注意:泛型只能是引用类型,不能是基本类型。

    注意事项:
    对于ArrayList集合来说,直接打印得到的不是地址值,而是内容。
    如果内容是空,得到的是空的中括号:[]

    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); // 错误写法!因为创建的时候尖括号泛型已经说了是字符串,添加进去的元素就必须都是字符串才行}}
  3. ArrayList当中的常用方法有:

    • public boolean add(E e):向集合当中添加元素,参数的类型和泛型一致。返回值代表添加是否成功。

      备注:对于ArrayList集合来说,add添加动作一定是成功的,所以返回值可用可不用。
      但是对于其他集合(今后学习)来说,add添加动作不一定成功。

    • 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<>();System.out.println(list); // []// 向集合中添加元素:addboolean success = list.add("柳岩");System.out.println(list); // [柳岩]System.out.println("添加的动作是否成功:" + success); // truelist.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);}}
  4. ArrayList 集合的遍历

    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));}}}
  5. ArrayList 集合当中存放基本数据类型的解决办法

    1. 如果希望向集合ArrayList当中存储基本类型数据,必须使用基本类型对应的“包装类”。

    2. 自动装箱:基本类型 --> 包装类型

    3. 自动拆箱:包装类型 --> 基本类型 [从JDK 1.5+开始,支持自动装箱、自动拆箱。]

    4. 基本类型 包装类(引用类型,包装类都位于java.lang包下)
      byte Byte
      short Short
      int Integer 【特殊】
      long Long
      float Float
      double Double
      char Character 【特殊】
      boolean Boolean

    5. 代码演示:

      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);}}
  6. 练习

    1. 随机生成 6 个 1 - 33 的整数 ,添加到集合,并遍历集合.

      public class RandomlyGenerated {public static void main(String[] args) {//定义一个 存放 Integer的集合,用来存放产生的随机数ArrayList<Integer> list = new ArrayList<>();Random r = new Random();for (int i = 0; i < 6; i++) {//生成 6 个 随机数int nextInt = r.nextInt(33) + 1;//将产生的随机数放到集合内list.add(nextInt);}//查看一下集合内的内容System.out.println(list);//集合遍历for (int i = 0; i < list.size(); i++) {Integer integer = list.get(i);System.out.println(integer);}}
      }
      
    2. ArrayList 集合存放自定义对象,并遍历集合.

      1. 创建一个学生类 :

         public class Student {private String naem ;private int age ;private boolean isGirl;public Student() {}public Student(String naem, int age, boolean isGirl) {this.naem = naem;this.age = age;this.isGirl = isGirl;}public String getNaem() {return naem;}public void setNaem(String naem) {this.naem = naem;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public boolean isGirl() {return isGirl;}public void setGirl(boolean girl) {isGirl = girl;}
      2. 创建一个测试类

        public class StudentTest {public static void main(String[] args) {//创建对象Student s1 = new Student("周杰伦", 30, false);Student s2 = new Student("石原里美", 19, true);Student s3 = new Student("雪梨", 20, true);Student s4 = new Student("小仓柚子", 17, true);//创建容器ArrayList<Student> list = new ArrayList<>();//存对象list.add(s1);list.add(s2);slist.add(s3);list.add(s4);System.out.println(list.size());//遍历集合for (int i = 0; i < list.size(); i++) {Student stu = list.get(i);System.out.println("姓名 : " + stu.getNaem() + ",年龄  :"  + stu.getAge() + ", 是不适合暖床丫 :" + stu.isGirl());}}
        }
        
    3. 定义以指定格式打印集合的方法(ArrayList类型作为参数),使用{}扩起集合,使用@分隔每个元素。格式参照 {元素 @元素@元素}

      1. 分析:需要以集合作为传入的参数,得到集合,遍历元素,拼接成指定格式;

      2. ==== > 考察的是集合可以作为传入参数

        public class SpecifyFormatOutput {public static void main(String[] args) {ArrayList<String> list = new ArrayList<>();list.add("张三丰");list.add("张无忌");list.add("周芷若");list.add("谢逊");list.add("张三丰");letFormate(list);}public static void letFormate(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 + "}");}}}
        }
        
    4. 题目描述 : 用一个大集合存入20个随机数字,然后筛选其中的偶数元素,放到小集合当中。
      要求使用自定义的方法来实现筛选。要求使用自定义的方法来实现筛选。

      1. 分析:

        1. 需要创建一个大集合,用来存储int数字:
        2. 随机数字就用Random nextInt
        3. 循环20次,把随机数字放入大集合:for循环、add方法
        4. 定义一个方法,用来进行筛选。
          筛选:根据大集合,筛选符合要求的元素,得到小集合。

        三要素
        返回值类型:ArrayList小集合(里面元素个数不确定)
        方法名称:selectOld
        参数列表:ArrayList大集合(装着30个随机数字)

        1. 判断(if)是偶数:num % 2 == 0
        2. 如果是偶数,就放到小集合当中,否则不放。
        public class SelectNum {public static void main(String[] args) {//产生30个随机数,放到大集合里Random r = new Random();ArrayList<Integer> big = new ArrayList<>();for (int i = 0; i < 20; i++) {int nextInt = r.nextInt(100) + 1; // 1 - 100 之间的整数big.add(nextInt);}//查看大集合里存放的随机数System.out.println(big);//调用筛选方法,返回的小集合System.out.println(selectOld(big));}public  static ArrayList<Integer>  selectOld(ArrayList<Integer> big) {//创建一个小集合,用来存放筛选过后的数字ArrayList<Integer> small = new ArrayList<>();//遍历大集合,筛选条件, 添加元素for (int i = 0; i < big.size(); i++) {Integer sums = big.get(i);if(sums % 2  == 0){// 像小集合中存放small.add(sums);}}return small;}}
        

Java中常用 API(第一部分)相关推荐

  1. java中常用API、Scanner类、匿名对象、Random类、ArrayList类、对象数组

    java中常用API: API:Application Programming Interface,应用程序编程接口.Java API是JDK中提供给我们使用的类的说明文档.这些类将底层的代码实现封装 ...

  2. JAVA中常用api学习思维导图

  3. Java中常用的API

    文章目录 前言 一.java.lang String StringBuilder Integer parseXXX Math Object System Throwable Thread Runnab ...

  4. 【java中常用的API】

    java中有很多常用的API,它们提供了便捷的使用方法和工具类,让我们来看一看java中常用的API吧. 1.math类: 它包含基本的数字运算方法,如对数.指数.平方根和三角函数等,一般数据类型为d ...

  5. java 中常用框架、intell idea简单使用、爬虫系统

    学习:http://www.ityouknow.com/spring-boot.html http://blog.didispace.com/spring-boot-learning-1/ ***in ...

  6. java 中常用的类

    java 中常用的类 Math Math 类,包含用于执行基本数学运算的方法 常用API 取整 l  static double abs(double  a) 获取double 的绝对值 l  sta ...

  7. java中常用的几种排序算法--常见笔试面试

    转载:http://blog.csdn.net/ygc87/article/details/7208082 以下列出Java中常用的几种排序算法,只是简单实现了排序的功能,还有待改进,望指教(以下均假 ...

  8. Java中常用的类,包,接口

    Java中常用的类,包,接口 包名 说明 java.lang 该包提供了Java编程的基础类,例如 Object.Math.String.StringBuffer.System.Thread等,不使用 ...

  9. java 中常用英语_java中常用英语

    英语|JAVA笔试题常见英语_电子/电路_工程科技_专业资料.Java 笔试题常见英语 What will be the output when you compile and execute the ...

最新文章

  1. python基础代码事例-零基础学习Python开发练习100题实例(2)
  2. 这才是实现分布式锁的正确姿势!
  3. java int64如何定义_java – 具有两个int属性的自定义类的hashCode是什么?
  4. SAP OData错误消息:Invalid format (return structure): Property Name ‘Guid‘, Property Value ‘000000
  5. MySql简介及概念
  6. python123测验2答案八边形_Python试卷
  7. mysql抖动可能的原因_MySQL应对网络抖动问题
  8. canvas笔记-lineCap的使用
  9. STM32F103_DDWG窗口看门狗
  10. 突击计划——银行利息
  11. zookeeper 事务日志
  12. 数据库索引的概念和分类
  13. VS2015静态库的使用(下)
  14. 《内向性格的竞争力:发挥你的本来优势》读书笔记
  15. 聊一聊微服务之间的通讯方式
  16. CCI: Representing N cents
  17. LAN9252 out端口识别不到的原因排查
  18. 反斜杆e,Linux下五彩斑斓的命令行输出
  19. Html5 文件上传
  20. Selenium 2.0的由来及设计架构

热门文章

  1. 利用ESP8266+ESPNOW实现多点无线通信
  2. 对称加密、非对称加密、数字证书
  3. 我的飞信发展方案(二)
  4. 德赛西威上海车展重磅发布Smart Solution 2.0,有哪些革新点?
  5. git创建远程分支并同步到远程
  6. python编码进制转换_关于Python|进制转换问题
  7. python数据分析面试题
  8. waitKey()函数的一些用法
  9. 时代全芯卡位新型存储 聚焦PCM相变存储器
  10. kali-dmitry