Java Methods-Arrays

Source: Internet
Author: User

"Java Array"

1. Sort the Java array using the sort () method, and how to use the BinarySearch () method to find the elements in the array

BinarySearch ()

return value:

Returns the index of the search key if it is contained in an array; (-(insertion point)-1)

The insertion point is defined as the point at which the key is inserted into the array: the first element index greater than this key, or a.length if all elements in the array are smaller than the specified key. Note that this guarantees that if and only if this key is found, the returned value will be >= 0.

Otherwise return (-(insertion point)-1) This sentence to note: If the value of the query is less than the minimum value in the array result (-(0)-1 result is-1), if the value of the query is greater than the maximum value inside the array. Then the result is (-(its index value)-1 result is-(1+ index value))

2. Use the sort () method to sort the Java array, and how to use the Insertelement () method to insert an element into an array

Insertelement () inserting elements into an array, customizing methods

system.arraycopy ()

System provides a static method Arraycopy (), which we can use to implement replication between arrays.

Its function prototype is: public static void Arraycopy (object src, int srcpos, object dest, int destpos, int length)

SRC: source array; Srcpos: The starting position where the source array is to be copied; Dest: the destination array; Destpos: The starting position of the destination array placement; Length: The lengths of the copies.

Note: Both SRC and dest must be of the same type or an array of types that can be converted.

The interesting thing is that this function can achieve itself to copy, for example: int[] Fun ={0,1,2,3,4,5,6}; System.arraycopy (fun,0,fun,3,3); The result is: {0,1,2,0,1,2,6}; The implementation process is like this, Sir, as a temporary array of length, copy the data from Srcpos to srcpos+length-1 in the fun array into a temporary array, and then execute the system.arraycopy (temporary array, 0,fun,3,3).

3. Get the length of an array using the property length of the array

4. Collections.reverse (ArrayList) reverses the array

5. Through the loop output array:

/* Author by w3cschool.cc file name: Main.java */public class Welcome {public   static void Main (string[] args) {      string[] Greeting = new String[3];      Greeting[0] = "This is the greeting";      GREETING[1] = "the readers from";      GREETING[2] = "Java Source.";      for (int i = 0; i < greeting.length; i++) {         System.out.println (greeting[i]);}}}   

6. Find the maximum and minimum values in an array by using the Collection.max () and Collection.min () methods of the Collection class

/* Author by w3cschool.cc file name: Main.java */import Java.util.arrays;import java.util.collections;public class Main {   public static void Main (string[] args) {      integer[] numbers = {8, 2, 7, 1, 4, 9, 5};      int min = (int) collections.min (arrays.aslist (numbers));      int max = (int) Collections.max (arrays.aslist (numbers));      SYSTEM.OUT.PRINTLN ("Minimum value:" + min);      System.out.println ("Max:" + max);}   }

Description

Aslist method: When using the Aslist () method, the array is linked to the list. When one of them is updated, the other automatically gets the update. Note: For object array types only, the base data type array does not have this attribute.

link:http://blog.csdn.net/cntanghai/article/details/7188296

7. Through the list class's Arrays.tostring () method and List class list. The AddAll (Array1.aslist (array2) method merges two arrays into an array:

/* Author by w3cschool.cc file name: Main.java */import java.util.arraylist;import java.util.arrays;import java.util.List; public class Main {public   static void main (string args[]) {      string a[] = {"A", "E", "I"};      String b[] = {"O", "U"};      List List = new ArrayList (Arrays.aslist (a));      List.addall (Arrays.aslist (b));      Object[] C = List.toarray ();      System.out.println (Arrays.tostring (c));}   }

Java Methods-Arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.