java About Arrays, Array function

  Arrays and the Array, these two classes include many Java array for operating a static function, are defined as follows:

  public final class Array

  extends Object

  public class Arrays

  extends Object

  Difference between the two:

  Array  Arrays

  Array class provides static methods to dynamically create and access Java arrays.

  Array allows widening conversion (using conventional methods to create a larger array of arrays) during Gets or sets, but if the reduction conversion occurs, is thrown IllegalArgumentException.

  This class comprises various methods (such as sorting and searching) for operating the array. The class also contains a static factory, the array can be viewed as a list.

  If the specified array reference is null, the method in this class all throw a NullPointerException

  Brush up on the basics of the array:

  int [] ints = new int [5] // In this case virtual machines on some application memory unit size four bytes, a length of 5 contiguous memory, and initialize the elements to zero;

  (Re-initialize the array can be declared once initialized, the length of the array will not be changed) an array of commonly used default values ​​as follows:

  public static void main(String[] args) {

  int[] ints = new int[1];

  for (int i : ints) System.out.println(i);

  float[] floats = new float[1];

  for (float f : floats) System.out.println(f);

  char[] chars=new char[1];

  chars[0]=72;

  for(char c:chars) System.out.println(c);

  boolean[] booleans=new boolean[1];

  for(boolean b:booleans) System.out.println(b);

  String[] strings=new String[1];

  for(String s:strings) System.out.println(s);

  }

  result:

  

 

  Array:

  static Object newInstance (Class componentType, int length) to create a new array of the specified type and length of the component

  static Object get (Object array, int index) Returns the value of the specified array object index components.

  static int getInt (Object array, int index) Returns the value of the specified array object index components, such as int

  The value of the index component static void set (Object array, int index, Object value) of the specified array object to the specified new value.

  static void setBoolean (Object array, int index, boolean z) Sets the value of the index component of the specified array object to the specified boolean value.

  :( sample code which uses reflective knowledge, refer to my blog: https: //blog.csdn.net/qq_42013035/article/details/103362162)

  public static void main(String[] args) {

  int[] ints=new int[5];

  Arrays.fill (ints, 3); // initial fill 3

  System.out.println("length:"+ints.length);

  for(int i:ints) System.out.print(i+" ");

  int [] ints1 = (int []) Array.newInstance (ints.getClass () getComponentType (), 10.); // Create a new element of type int, an array of length 10

  System.arraycopy(ints,0,ints1,0,ints.length);

  System.out.println("\nlength:"+ints1.length);

  for(int i:ints1) System.out.print(i+" ");

  System.out.println("\n"+Array.get(ints,3));

  }

  result:

  

 

  Arrays:

  Arrays are operating an array of tools, it features more powerful compared Array (can also be said to be rich Array functions, both, useless), released in jdk1.2 version.

  static List asList (T ... a) fixed-size list returned by the specified array.

  static boolean deepEquals (Object [] a1, Object [] a2) if the two specified arrays depth equal to each other, return true

  static int deepHashCode (Object [] a) The "deep content" Returns a hash code of the specified array

  static void fill (int [] a, int val) int value assigned to each element of the specified array to the specified int.

  static void fill (int [] a, int fromIndex, int toIndex, int val) int the specified range of the specified value is assigned to each of the specified array element int

  static int hashCode (int [] a) Returns a hash code based on the contents of the specified array

  static int binarySearch search using a binary search algorithm specified value (int [] a, int key) (plus a range parameter) position, no negative return

  static int [] copyOfRange (int [] original, int from, int to) Copies the specified range of the specified array into a new array.

  static void sort (int [] a) arranged in numerical order specified array

  static void sort (Object [] a) for the specified object in ascending order of the array elements according to the natural ordering

  Specified range static void sort (int [] a, int fromIndex, int toIndex) in ascending order of the array

  static String toString (int [] a) returns the contents of the specified string representation of the array

  Back static IntStream stream (int [] array, int startInclusive, int endExclusive) IntStream sequence designated as the source and the array of the specified range

  Note: Zhengzhou flow of the hospital http://www.120zzzzyy.com/

  1.sort () is used by default MergeSort (merge sort), you can customize other algorithms, but it must be stable

  2. When the time with binarySearch, it must be ordered array, because binary search algorithm uses a dichotomy

  3.sort (Object [] a) are arranged in accordance with the natural order of characters

  Code Example:

  public static void main(String[] args) {

  int[] ints={2,4,3,1,9,6,5,4};

  System.out.println("inst length:"+ints.length);

  System.out.println(Arrays.toString(ints));

  int [] ints1 = Arrays.copyOf (ints, 10); // ha strange feeling worse than that in the Array newInstance

  System.out.println("ints1 length"+ints1.length);

  System.out.println(Arrays.toString(ints1));

  System.out.println("ints1's hashcode:"+Arrays.hashCode(ints1));

  Arrays.sort(ints);

  System.out.println("after sort by Arrays:");

  System.out.println(Arrays.toString(ints));

  System.out.println("binarySearch result is "+Arrays.binarySearch(ints,9));

  System.out.println(Arrays.toString(ints));

  /*

  Reference type data array

  */;

  String[] strings={"hello","world","who","are","you"};

  System.out.println(Arrays.toString(strings));

  System.out.println("deep hashcode:"+Arrays.deepHashCode(strings));

  Arrays.sort(strings);

  System.out.println(Arrays.toString(strings));

  List list = Arrays.asList (1,1,2,2,3); // Use quickly convert a smaller number of elements into a corresponding set of

  System.out.println(list.size());

  Iterator iterator=list.iterator();

  while (iterator.hasNext()) System.out.print(iterator.next().toString()+" ");

  }

  Thus the outcome.

Guess you like

Origin www.cnblogs.com/gnz49/p/12016235.html