当前位置:网站首页>Use cases of the arrays class

Use cases of the arrays class

2022-04-23 14:19:00 White horse is not a horse·

Arrays class

Arrays Class is often used to manipulate array objects , There are two common functions , One is sorting , One is to transform into character string .

1. Convert to string output

One dimensional array output :Arrays.toString(data)

// When the array is one-dimensional :Arrays.toString(data)
int[] data=new int[]{
    2,3,4,5};
System.out.println("data="+Arrays.toString(data));
//data=[2, 3, 4, 5]

Multidimensional array output :Arrays.deepToString(data)

// When the array is one-dimensional :Arrays.toString(data)
int[]] data=new int[][]{
    {
    2,3,4,5},{
    2,3,4},{
    4,3,1}};
System.out.println("data="+Arrays.deepToString(data));
//data=[[2, 3, 4, 5], [2, 3, 4], [4, 3, 1]]

2. Sort

Take an integer array as an example , Sort

2.1 One dimensional array sort ( The default is ascending ):Arrays.sort(data)

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

2.2 One dimensional array sort ( Custom sort ):Arrays.sort(data)

  • If you need to customize the order of elements in the array , Need to be right sort Comparator Comparator Class compare Method , among Comparator Is an anonymous inner class .
// The comparator needs to be calibrated Comparator Class compare Method 
Integer[] data=new Integer[]{
    2,5,4,1};
Arrays.sort(data,new Comparator<Integer>(){
    
   @Override
   public int compare(Integer a,Integer b){
    
      return b-a;
            }
        });
   System.out.println("data="+Arrays.toString(data));

// have access to lambda Expression shortens anonymous inner classes 
Integer[] data=new Integer[]{
    2,5,4,1};
Arrays.sort(data,(a,b)-> b-a);
System.out.println("data="+Arrays.toString(data));

2.3 Multi dimensional array sorting ( Custom sort ):Arrays.sort(data)

// First, the array is arranged in ascending order according to the first element of the array ,
// If equal , Then arrange in reverse order according to the second element 
int[][] data=new int[][]{
    {
    3,1,2},{
    3,2,3},{
    1,2,3}};
Arrays.sort(data,(a,b)->{
    
	if(a[0]==b[0]) return a[1]-b[1];
	return b[0]-a[0];
	});
	System.out.println("data="+Arrays.deepToString(data));
	//data=[[3, 1, 2], [3, 2, 3], [1, 2, 3]]

3. Array to set Arrays.asList()( The easiest way )

String[] string={
    "2","3","a","b"};
List<String> list=new Arraylist<>(Arrays.asList(string);
list.add("3");
System.out.println("list="+lsit);

Be careful :

  • Arrays.asList(string) It's the method of the object , The passed in must be an object array , Not the basic type , Therefore, it is necessary to use packaging .
  • Arrays.asList(string), The object is Arrays An inner class , Not at all List aggregate , Therefore, there is no function to modify and add , We need to turn him into ArrayList aggregate .

4. ( Supplementary knowledge ) Anonymous inner class ( Inner class )

4.1 What is an inner class :

1) Classes defined in classes

4.2 The role of inner classes :

1) You can access the data within the scope of this class definition , Include private Decorated private data .
2) Can solve Java The defect of single inheritance .

4.3 What is an anonymous inner class :

1) There is no intrinsic name of a class , Defined in method , Block of code .
2) effect : Easy to create subclass objects , The ultimate goal is to simplify coding
3) The format is as follows :

Animal a=new Animal(){
    
public void run(){
    
};

5. ( Supplementary knowledge )Lambda expression

  • The simplified function is the anonymous inner class of the interface , The main purpose is to simplify the code .
  • In the face of Arrays.sort For custom sorting Lambda expression .

版权声明
本文为[White horse is not a horse·]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231407525534.html