Java wrapper classes and Arrays classes (detailed explanation)

Table of contents

Packaging

Function introduction

Unique functions of packaging classes

Arrays class

Arrays.fill() 

Arrays.toString() 

Arrays.sort()

Sort in ascending order

Sort descending

Arrays.equals()

Arrays.copyOf()

Arrays.binarySearch()

Packaging

Function introduction

The wrapper class is actually the reference type corresponding to the 8 basic data types.

Basic data types Reference data type
byte Byte
short Short
int Integer
long Long
char Character
float Float
double Double
boolean Boolean

Why do you need wrapper classes?

In order to realize that everything is an object, Java provides corresponding reference types for 8 basic types.

In fact, later collections and generics can only support packaging types and do not support basic data types.

Autoboxing :

Data and variables of basic types can be directly assigned to variables of wrapped types.

public static void main(String[] args) {
    int num1 = 10;

    // 自动装箱(将int基本数据类变量赋值给包装类型变量)
    Integer a = num1;
    System.out.println(a);
}

Automatic unboxing :

Variables of wrapper type can be directly assigned to variables of basic data type.

public static void main(String[] args) {
    Integer num2 = 100;

    // 自动拆箱(将包装类型变量赋值给基本类型变量)
    int b = num2;
    System.out.println(b);
}

Unique functions of packaging classes

The default value of the variables of the wrapper class can be null, which is more fault-tolerant, but the basic type cannot.

public static void main(String[] args) {
    Integer num3 = null;
}

You can convert basic type data into string type (not very useful)

  • Call the toString() method to get the string result.
  • Or call Integer.toString (basic type data).
public static void main(String[] args) {
    Integer number = 100;
    // 方式一
    String result1 = number.toString();
  	// 方式二
  	String result2 = Integer.toString(101);
    // 方式三: 最简单常用
    String result3 = number + "";
  
    System.out.println(result1 + 1); // 1001
    System.out.println(result2 + 1); // 1011
    System.out.println(result3 + 2); // 1002
}

Can convert string type values ​​into real data types (very useful)

  • teger.parseInt("integer of string type")
  • Double.parseDouble("Decimal of type string").
 String st1 = "123";
 String st2 = "12.123";
 int a = Integer.parseInt(st1);
 double b = Double.parseDouble(st2);
 System.out.println(a+1);//124
 System.out.println(b+1);//13.123

To convert string type numbers into real data, we can also call the valueOf method of the wrapper class

public static void main(String[] args) {
    String strNum1 = "123";
    String strNum2 = "12.123";

    // 字符串转整数
    int intNum = Integer.valueOf(strNum1);
    // 字符串转小数
    double doubleNum = Double.valueOf(strNum2);

    System.out.println(intNum + 1); // 124
    System.out.println(doubleNum + 1); // 13.123
}

Arrays class

Arrays.fill() 

Given a specific value val, make the elements in the entire array or within a certain subscript range be val.

 int [] a = new int[10];
 int [] b = new int[8];
 Arrays.fill(a,3);
 Arrays.fill(b,3,7,6);
 for(int x : a)
    System.out.print(x+",");
 System.out.println();
 for(int x : b)
    System.out.print(x+",");

  

Arrays.toString() 

Quickly output array contents, but the output is marked with [ ]

int[] a = {1,2,3,4,5};
System.out.println(Arrays.toString(a));
// 输出格式:[1,2,3,4,5]

Arrays.sort()

Sort in ascending order

Array sorting function, default ascending order

int[] a = new int[5]{5,4,3,2,1};
Arrays.sort(a); // 1 2 3 4 5
System.out.println(Arrays.toString(a));
// [1,2,3,4,5]

Two overloading methods:

1.Arrays.sort (array name)

2.Arrays.sort (array name, starting coordinates, sort length)

When sorting strings, just like C+, it is sorted in lexicographic order.

Sort descending

To implement descending sorting, you can use Collections.reverseOrder(), or you can wrap type arrays

No packaging

Arrays.sort(a, Collections.reverseOrder());

Package

 Integer[] a = {1,4,3,2,5,6,2,3,4};
 Arrays.sort(a,((o1, o2) -> o2 - o1));
 System.out.println(Arrays.toString(a));
 // [6, 5, 4, 4, 3, 3, 2, 2, 1]

Arrays.equals()

Compares whether the contents of two arrays are equal

int[] a = {1,2,3};
int[] b = {1,2,3};
boolean isSame = Arrays.equals(a,b);
//true

Note: Arrays.equals() compares array contents, while a.equals(b) method compares address values.

Special note: When comparing array a==b in Java, the addresses of the two are

When you use the reference data content (arrays, strings, various collections...) provided by Java in the future, you must use the equals() method to compare whether the content is equal, instead of stupidly using ==! Because the official has rewritten equals(). If you want to compare classes you wrote yourself, such as students sorted by grades, you have to override the equals() method yourself.

Arrays.copyOf()

Two ways:

1.B array = Arrays.copyOf(A array, assignment length)

 int [] a = new int[10];
 int [] b;
 Arrays.fill(a,10);
 b = Arrays.copyOf(a,11);//超过赋值长度赋值为0
 System.out.println(Arrays.toString(b));
 //[10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 0]

2.  B array = Arrays.copyOfRange(A array, starting coordinates, interception length)

int [] a = new int[10];
int [] b;
Arrays.fill(a,10);
b = Arrays.copyOfRange(a,2,5);
System.out.println(Arrays.toString(b));
//[10, 10, 10]

Arrays.binarySearch()

Search for the specified value in the array. If found, return the subscript of this value.

If not found, return -insertion point-1 (must be a negative value);

int[] a = {1,5,6,7};
Arrays.binarySearch(a,2)  //没找到,插入点为1,则返回 -2
Arrays.binarySearch(a,4)  //没找到,插入点为1,则返回 -2
Arrays,binarySearch(a,8)  //没找到,插入点为4,则返回 -5
Arrays.binarySearch(a,5)  //找到了!返回下标 1
只要返回值 ≥ 0 ,就代表找到了。

Recommended music: Once upon a time written by hand

Reference: Java basic Arrays class    Arrays common methods (super detailed explanation)

Guess you like

Origin blog.csdn.net/weixin_74088105/article/details/132624014