Summary of the difference between java Array and Arrays

  • 2021-09-05 00:11:02
  • OfStack

When we operate on arrays, we often use Arrays methods, and at the same time, we will combine some functions to operate on arrays. At the same time, we also studied the array class Array. There is only a small difference in names between the two classes, but there is a big difference in usage. This article divides them into two parts, and explains their usage correspondingly, so that we can distinguish their use.

1. The array class Array belongs to java. lang

One of the most basic storage structures in Java.

Provides methods for dynamically creating and accessing Java arrays. The elements must be of the same type.

The efficiency is high, but the capacity is fixed and cannot be changed dynamically.

It can't tell how many elements actually exist in it. length just tells us the capacity of array.

2. Static class Arrays belongs to java. util

This static class is designed to manipulate array and provides static methods such as search, sort, copy, etc.

equals (): Compares two array for equality. array has the same number of elements, and all corresponding elements are equal in pairs.

sort (): Used to sort array.

binarySearch (): Look for elements in an ordered array.

java Arrays class instance extension:

1. Arrays class overview

A tool class that operates on arrays.

It provides functions such as sorting and searching.

2. Membership approach

public static String toString(int[] a)

public static void sort(int[] a)

public static int binarySearch(int[] a,int value)


package com;

import java.util.Arrays;

/**
 * Arrays Overview of class and common methods 
 *  A tool class that operates on arrays. 
 *  It provides functions such as sorting and searching. 
 *  Member method 
 * public static String toString(int[] a)  Will int Converts an array of type to a string 
 * public static void sort(int[] a)  Sort the array, and quickly sort it internally  
 * public static int binarySearch(int[] a,int key) 2 Separate search method 
 * @author  Xu Weiwei 
 *
 */
public class ArraysDemo {
 public static void main(String[] args) {
 int[] array = {3,44,2,546,74};
 //public static String toString(int[] a)  Will int Converts an array of type to a string 
 System.out.println(Arrays.toString(array));//[3, 44, 2, 546, 74]
 
 //public static void sort(int[] a)  Sort the array, and quickly sort it internally 
 Arrays.sort(array);
 System.out.println(Arrays.toString(array));//[2, 3, 44, 74, 546]
 
 //public static int binarySearch(int[] a,int key) 2 Separate search method 
 int index = Arrays.binarySearch(array, 5);
 System.out.println(index);//-3
 
 }

}


Related articles: