常见对象·Arrays 类和 包装类

数组高级冒泡排序原理图解

* A:画图演示
  * 需求:
    数组元素:{24, 69, 80, 57, 13}
    请对数组元素进行排序

  * 冒泡排序:
    相邻元素两两比较,较大的往后放,第一次完毕,最大值出现在了最大索引处

数组高级冒泡排序代码实现

* A:案例演示
  * 数组高级冒泡排序代码

  1. package com.heima.array;
  2.  
  3. public class Demo1_Array {
  4.  
  5. public static void main(String[] args) {
  6. int[] arr = { 24, 69, 80, 57, 13 };
  7. bubbleSort(arr);
  8. for (int i = 0; i < arr.length; i++) {
  9. System.out.print(arr[i] + " ");
  10. }
  11.  
  12. }
  13.  
  14. /*
  15. * 冒泡排序:
  16. * 1、返回值类型,void
  17. * 2、参数列表,int[] arr
  18. */
  19. public static void bubbleSort(int[] arr) {
  20. for (int i = 0; i < arr.length - 1; i++) { // 外循环:只需要比较arr.lenth-1次就行了
  21. for (int j = 0; j < arr.length - i - 1; j++) { // 减一防止索引越界,-i并且提高效率
  22. if (arr[j] > arr[j + 1]) {
  23. int tmp = arr[j];
  24. arr[j] = arr[j + 1];
  25. arr[j + 1] = tmp;
  26. }
  27. }
  28. }
  29.  
  30. }
  31.  
  32. }

BubbleSort

数组高级选择排序原理图解

* A:画图演示
  * 需求:
    * 数组元素:{24, 68, 80, 57, 13}
    * 请对数组元素进行排序

    *选择排序
      * 从0索引开始,依次和后面元素比较,小的往前放,第一次完毕时,最小值出现在了最小索引处

数组高级选择排序代码实现

* A:案例演示
  * 数组高级选择排序代码

  1. package com.heima.array;
  2.  
  3. public class Demo2_Array {
  4.  
  5. public static void main(String[] args) {
  6. int[] arr = { 13, 69, 80, 57, 24 };
  7. selectSort(arr);
  8. print(arr);
  9. }
  10.  
  11. /*
  12. * 如果某个方法只针对本类使用,不想让其他类试图用就可以定义成私有的
  13. */
  14. public static void print(int[] arr) {
  15. for (int i = 0; i < arr.length; i++) {
  16. System.out.println(arr[i]);
  17. }
  18. }
  19.  
  20. /*
  21. * 选择排序: 1、返回值void 2、参数列表int[] arr
  22. */
  23. public static void selectSort(int[] arr) {
  24. for (int i = 0; i < arr.length - 1; i++) { // 只需要比较arr.length-1次
  25. for (int j = i + 1; j < arr.length; j++) {
  26. if (arr[i] > arr[j]) {
  27. swap(arr, i, j);
  28. }
  29. }
  30. }
  31. }
  32.  
  33. /*
  34. * 换位操作: 1、返回值类型,void 2、参数列表int[] arr, int i, int j
  35. */
  36. public static void swap(int[] arr, int i, int j) {
  37. int tmp = arr[i];
  38. arr[i] = arr[j];
  39. arr[j] = tmp;
  40. }
  41. }

Arrays

数组高级二分查找原理图解

* A:画图演示
  * 二分查找,查找元素所对应的索引
  * 前提,数组元素有序

数组高级二分查找代码实现及注意事项

* A:案例演示
  * 数组高级二分查找代码

* B:注意事项
  * 如果数组无序,就不能使用二分查找
    * 原因:因为如果你排序了,但是你排序的时候就已经改变了数组的原始索引

  1. package com.heima.array;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Demo3_Array {
  6. public static void main(String[] args) {
  7. int[] arr = { 11, 22, 33, 44, 55, 66, 77 };
  8. Scanner sc = new Scanner(System.in);
  9. int i = sc.nextInt();
  10. int p = binarySearch(arr, i);
  11. System.out.println(p);
  12. }
  13.  
  14. /*
  15. * 二分查找: 1、返回值类型,int
  16. * 2、参数列表 int[] arr, int value
  17. */
  18. private static int binarySearch(int[] arr, int value) {
  19. int min = 0;
  20. int max = arr.length - 1;
  21. int mid = (min + max) / 2;
  22.  
  23. while (arr[mid] != value) { // 当中间值不等于要找的值,就开始循环查找
  24. if (arr[mid] > value) { // 当中间值大于要找的值,改变最大值
  25. max = mid - 1;
  26. } else if (arr[mid] < value) { // 当中间值小于要找的值,就改变最小值
  27. min = mid + 1;
  28. }
  29. mid = (min + max) / 2; // 无论最大还是最小,中间索引都会随之改变
  30. if (min > max) { // 如果最小索引大于最大索引,则表面要找的值不存在
  31. return -1;
  32. }
  33. }
  34. return -(min + 1);
  35. }
  36. }

Arrays


Arrays类的概述和方法使用

* A:Arrays类概述
  * 针对数组进行操作的工具类
  * 提供了排序、查找等功能

* B:成员方法
  * public static String toString(int[ ] arr)
  * public static void sort(int[] arr)
  * public static int binarySearch(int[ ] a, int key)

  1. public static String toString(int[] a) {
  2. if (a == null)
  3. return "null";
  4. int iMax = a.length - 1;
  5. if (iMax == -1)
  6. return "[]";
  7.  
  8. StringBuilder b = new StringBuilder();
  9. b.append('[');
  10. for (int i = 0; ; i++) {
  11. b.append(a[i]);
  12. if (i == iMax)
  13. return b.append(']').toString();
  14. b.append(", ");
  15. }
  16. }

toString源码

  1. private static int binarySearch0(int[] a, int fromIndex, int toIndex,
  2. int key) {
  3. int low = fromIndex;
  4. int high = toIndex - 1;
  5.  
  6. while (low <= high) {
  7. int mid = (low + high) >>> 1;
  8. int midVal = a[mid];
  9.  
  10. if (midVal < key)
  11. low = mid + 1;
  12. else if (midVal > key)
  13. high = mid - 1;
  14. else
  15. return mid; // key found
  16. }
  17. return -(low + 1); // key not found.
  18. }

binarySearch源码

  1. package com.heima.array;
  2.  
  3. import java.util.Arrays;
  4.  
  5. public class Demo4_Array {
  6. public static void main(String[] args) {
  7. int[] arr = { 33, 22, 11, 44, 66, 55 };
  8. System.out.println(Arrays.toString(arr)); // 将数组变成字符串
  9.  
  10. Arrays.sort(arr);
  11. System.out.println(Arrays.toString(arr)); // 对数组进行排序
  12.  
  13. int[] arr2 = { 11, 22, 33, 44, 55, 66, 77 }; // 二分查找的对象必须是有序的数组,如果数组中包含多个重复的值,则无法保证找到的是哪一个
  14. System.out.println(Arrays.binarySearch(arr2, 12)); // 如果要找的值包含在数组中,则返回其索引;否则返回(-(插入点)-1)
  15. }
  16. }

Arrays


基本数据类型包装类的概述

* A:为什么会有基本类型的包装类
  * 好处在于可以在对象中定义更多的功能方法来操作该数据

* B:常用操作
  * 用于基本数据类型与字符串之间的转换

* C:基本类型和包装类的对应关系

  byte            Byte
  short           Short
  int               Integer
  long            Long
  float            Float
  double        Double
  char      Character
  boolean      Boolean

  1. package com.heima.wrapclass;
  2.  
  3. public class Demo1_Integer {
  4.  
  5. public static void main(String[] args) {
  6. System.out.println(Integer.toBinaryString(60)); // 二进制输出
  7. System.out.println(Integer.toOctalString(60)); // 八进制输出
  8. System.out.println(Integer.toHexString(60)); // 十六进制输出
  9.  
  10. }
  11. }

Integer

Integer类的概述和构造方法

* A:Integer类的概述
  * 通过JDK提供的API,查看Integer类 的说明
  * Integer类 在对象中包装了一个基本类型 int 的值
  * 该类提供了多个方法,能在 int 类型和 String 类型之间相互转换
  * 还提供了处理 int 类型时非常有用的其他一些常量和方法

* B:构造方法
  * public Integer(int value)
  * public Integer(String str)

* C:案例演示
  * 使用构造方法创建对象

  1. package com.heima.wrapclass;
  2.  
  3. public class Demo2_Integer {
  4.  
  5. public static void main(String[] args) {
  6. System.out.println(Integer.MAX_VALUE); // Integer能表示的最大数
  7. System.out.println(Integer.MIN_VALUE); // Integer能表示的最小数
  8.  
  9. // Integer构造方法
  10. Integer i1 = new Integer(100);
  11. System.out.println(i1);
  12.  
  13. // Integer i2 = new Integer("abc"); // 数字格式异常: java.lang.NumberFormatException
  14. Integer i2 = new Integer("100");
  15. System.out.println(i2);
  16.  
  17. }
  18. }

Integer

String 和 int 类型的相互转换

* A:int -> String
  * a:和 "" 进行拼接
  * b:public static String valueOf(int i)
  * c:int -> Integer -> String (调用 Integer类 的 toString方法)
  * d:public static String toString(int i) (Integer类的静态方法)

* B:String -> int
  * a:String -> Integer -> int
    * public staticint parseInt(String s)

  1. package com.heima.wrapclass;
  2.  
  3. public class Demo3_Integer {
  4. public static void main(String[] args) {
  5. // demo1(); // int -> String
  6. // demo2(); // String -> int
  7. String s1 = "true";
  8. boolean b = Boolean.parseBoolean(s1);
  9. System.out.println(b);
  10.  
  11. //String s2 = "abc";
  12. //char c = Character.parse // char的包装类Character中没有parseXxx的方法
  13. // 字符串到字符的转换通过toCharArray() 就可以把字符串转换为字符数组
  14.  
  15. }
  16.  
  17. public static void demo2() {
  18. String s = "200";
  19. Integer i3 = new Integer(s);
  20. int i4 = i3.intValue();// string转换成integer 再转换成int
  21. System.out.println(i4);
  22. int i5 = Integer.parseInt(s);// 直接将string转换成int,推荐使用
  23. System.out.println(i5);
  24. /*
  25. * 基本数据类型的包装类又八种
  26. * 其中七种都有parseXxx的方法,可以将七种类型转换成基本数据类型,char类型没有
  27. * 字符串到字符的转换通过toCharArray方法就行
  28. */
  29. }
  30.  
  31. public static void demo1() {
  32.  
  33. int i = 100;
  34. String s1 = i + "";
  35. System.out.println(s1); // 将数字和""相加,推荐使用
  36.  
  37. String s2 = String.valueOf(i);
  38. System.out.println(s2); // 调用valueof方法
  39.  
  40. Integer i2 = new Integer(i);
  41. String s3 = i2.toString();
  42. System.out.println(s3); // 将int转换为integer再调用toString方法
  43.  
  44. String s4 = Integer.toString(i);
  45. System.out.println(s4); // 直接调用integer类
  46. }
  47. }

Integer

JDk5新特性自动装箱和自动拆箱

* A:JDK5的新特性
  * 自动装箱:把基本类型转换为包装类类型
  * 自动拆箱:把包装类类型转换为基本类型

* B:案例演示
  * JDK5的新特性自动装箱和拆箱

  * Integer i = 100;
  * i += 100;

* C:注意事项
  * 在使用时,Integer x = null; 代码就会出现NullPointerException
  * 建议先判断为否为null,然后再使用

  1. package com.heima.wrapclass;
  2.  
  3. public class Demo4_JDK5 {
  4. public static void main(String[] args) {
  5. int x = 100;
  6. Integer i1 = new Integer(x);// 将基本数据类型包装成对象,手动装箱
  7. int y = i1.intValue(); // 将对象转换成基本数据类型,手动拆箱
  8.  
  9. Integer i2 = 100;// 自动装箱,把基本数据类型 转换成 引用数据类型
  10. int z = i2 + 200;// 自动拆箱,把引用数据类型 转换为 基本数据类型
  11. System.out.println(z);
  12.  
  13. // Integer i3 = null;
  14. // int a = i3 + 100; // 底层调用intValue,但是i3是null,会出现空指针异常:java.lang.NullPointerException
  15. // System.out.println(a);
  16.  
  17. }
  18. }

JDk5

Integer 的面试题

* A:Integer 的面试题
  * 看程序写结果

  1. package com.heima.wrapclass;
  2.  
  3. public class Demo5_Integer {
  4. /**
  5. * @param args
  6. */
  7. public static void main(String[] args) {
  8. Integer i1 = new Integer(97);
  9. Integer i2 = new Integer(97);
  10. System.out.println(i1 == i2); // 判断地址值,false
  11. System.out.println(i1.equals(i2)); // Integer类以重写equals方法,true
  12. System.out.println("---------------------");
  13.  
  14. Integer i3 = new Integer(197);
  15. Integer i4 = new Integer(197);
  16. System.out.println(i3 == i4); // false
  17. System.out.println(i3.equals(i4)); // true
  18. System.out.println("----------------------");
  19.  
  20. Integer i5 = 127;
  21. Integer i6 = 127;
  22. System.out.println(i5 == i6); // true,i5和i6指向了同一对象
  23. System.out.println(i5.equals(i6)); //true
  24. System.out.println("----------------------");
  25.  
  26. Integer i7 = 128;
  27. Integer i8 = 128;
  28. System.out.println(i7 == i8); // false
  29. System.out.println(i7.equals(i8)); // true
  30. /*
  31. * -128到127是byte的取值范围,
  32. * 如果在这个取值范围内,自动装箱就不会创建新对象,而是从常量池中获取
  33. * 如果超过了byte的取值范围,就会再新创建对象
  34. */
  35. }
  36. }

 

Java 常见对象 04的更多相关文章

  1. Java常见对象Object类中的个别方法

    Java常见对象Object类 public int hashCode() : 返回该对象的哈希码值. 注意:哈希值是根据哈希算法计算出来的一个值,这个值和地址值有关,但是不是实际地址值.你可以理解成 ...

  2. Java 常见对象 05

    常见对象·正则表达式 和 其他类 正则表达式的概述和简单使用 * A:正则表达式 * 是指一个用来描述或者匹配一系列符合某个语法规则的字符串的单个字符串.其实就是一种规则,有自己的特殊应用 * 作用: ...

  3. Java 常见对象 03

    常见对象·StringBuffer类 StringBuffer类概述 * A:StringBuffer类概述 * 通过 JDk 提供的API,查看StringBuffer类的说明 * 线程安全的可变字 ...

  4. Java 常见对象 02

    常见对象·String类 Scanner 的概述和方法介绍 * A:Scanner 的概述 * B:Scanner 的构造方法原理 * Scanner(InputStream source) * Sy ...

  5. Java 常见对象 01

    常见对象·Object类 Object类的概述 * A:Object 类概述 * 类层次结构的根类 * 所有类都直接或间接地继承自该类 * B:构造方法 * public Object() * 回想为 ...

  6. Java常见对象之String

    String类的概述 String 类代表字符串.Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现.字符串是常量,一旦被赋值,就不能被改变. String ...

  7. Java常见对象内存分析

    首先要明确Java内存的个位置上放的是啥 类.对象.实例三者的关系: 1.类:是对象的模板,可以实例化对象.(this不能出现在静态方法中) 2.对象:类的个体. 3.实例:实现的对象. 4.对应的引 ...

  8. day11<Java开发工具&常见对象>

    Java开发工具(常见开发工具介绍) Java开发工具(Eclipse中HelloWorld案例以及汉化) Java开发工具(Eclipse的视窗和视图概述) Java开发工具(Eclipse工作空间 ...

  9. Java学习笔记 04 类和对象

    一.类和对象的概念 类 >>具有相同属性和行为的一类实体 对象 >>实物存在的实体.通常会将对象划分为两个部分,即静态部分和动态部分.静态部分指的是不能动的部分,被称为属性,任 ...

随机推荐

  1. @Indexed 注解

    本文转载自:https://www.cnblogs.com/aflyun/p/11992101.html 最近在看 SpringBoot 核编程思想(核心篇),看到走向注解驱动编程这章,里面有讲解到: ...

  2. Springboot 过滤器和拦截器详解及使用场景

    一.过滤器和拦截器的区别 1.过滤器和拦截器触发时机不一样,过滤器是在请求进入容器后,但请求进入servlet之前进行预处理的.请求结束返回也是,是在servlet处理完后,返回给前端之前. 2.拦截 ...

  3. nginx实现文件上传和下载

    nginx实现文件上传和下载 发布时间:2020-06-05 16:45:27 来源:亿速云 阅读:156 作者:Leah 栏目:系统运维 这篇文章给大家分享的是nginx实现文件上传和下载的方法.小 ...

  4. next v5升级到next v7需要注意的地方

    title: next v5升级到next v7需要注意的地方 date: 2020-03-04 categories: web tags: [hexo,next] 大部分的设置都是一样的,但有一些细 ...

  5. codeforces 858A

    A. k-rounding time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  6. 杭电多校HDU 6579 Operation (线性基 区间最大)题解

    题意: 强制在线,求\(LR\)区间最大子集异或和 思路: 求线性基的时候,记录一个\(pos[i]\)表示某个\(d[i]\)是在某个位置更新进入的.如果插入时\(d[i]\)的\(pos[i]\) ...

  7. React Slingshot

    React Slingshot React 弹弓 https://github.com/coryhouse/react-slingshot https://decoupledkit-react.rea ...

  8. Python Coding Interview

    Python Coding Interview Python Advanced Use enumerate() to iterate over both indices and values Debu ...

  9. Node.js & process.env & OS Platform checker

    Node.js & process.env & OS Platform checker Window 10 Windows 7 ia32 CentOS $ node # process ...

  10. Raspberry Pi & Node.js & WebSockets & IM

    Raspberry Pi & Node.js & WebSockets & IM Raspberry Pi 4 nvm & node.js $ curl -o- htt ...