JDK core API-Manipulate arrays in java

Source: Internet
Author: User

If you have done a lot of java programs, you may be familiar with java Collection classes, such as Vector and ArrayList. You can create a set and add elements to it:

  1. List lst =NewArrayList ();
  2. Lst. add (New Integer(37 ));


In this special example, an Integer value 37 is used to construct an Integer encapsulation class object, which is then added to the List.

This simple example shows the basics of a set-they are used to manipulate a column of objects, each of which is a class or interface type. Therefore, an ArrayList can contain objects of the Object, String, Float, and Runnable types. The Collection class cannot be used for a list of original data types, such as an integer array.

If you use arrays of the original type in your program, how do you manipulate them? This tip shows you some of the technologies you can use.

The first technology is sorting. The java. util. Arrays class contains a set of class methods for sorting and searching Arrays. For example:

  1. ImportJava. util.Arrays;
  2. Public ClassArrayDemo1 {
  3. Public Static VoidMain (StringArgs []) {
  4. IntVec [] = {37, 47, 23,-5, 19, 56 };
  5. Arrays. sort (vec );
  6. For(IntI = 0; I <vec.Length; I ++ ){
  7. System. Out. println (vec [I]);
  8. }
  9. }
  10. }


This demo initializes an integer array and then calls Arrays. sort to sort the array in ascending order.

Similarly, you can perform a binary search on the sorted array:

  1. ImportJava. util.Arrays;
  2. Public ClassArrayDemo2 {
  3. Public Static VoidMain (StringArgs []) {
  4. IntVec [] = {-5, 19, 23, 37, 47, 56 };
  5. IntSlot = Arrays. binarySearch (vec, 35 );
  6. Slot =-(slot + 1 );
  7. System. Out. println ("insertion point =" + slot );
  8. }
  9. }


This program has a subtle concept. If the binary search fails, it will return:

-(Insertion point)-1

This demo calls the search method with parameter 35, and the parameter does not exist in the array. the return value of the method is-4. If this value is added and its negative number is obtained, 3 is obtained, this is the position where 35 should be inserted into the array. In other words, values-5, 19, and 23 occupy 0, 1, and 2 in the array. Therefore, the value 35 should be at the index 3 position, while the values 37, 47, and 56 are postponed. The search method does not actually insert data, but just specifies where to insert data.

In addition to sorting and searching, what can we do for the original array? Another useful technique is to convert an original array to an equivalent array of object types. Each corresponding element uses their encapsulate class. For example, in an encapsulated array, 37 becomes Integer (37 ).

  1. ImportJava. util.Arrays;
  2. ImportJava. lang. reflect.Array;
  3. Public ClassArrayDemo3 {
  4. // If input is a single-dimension primitive array,
  5. // Return a new array consisting of wrapped elements,
  6. // Else just return input argument
  7. Public Static ObjectToArray (ObjectVec ){
  8. // If null, return
  9. If(Vec =Null){
  10. ReturnVec;
  11. }
  12. // If not an array or elements not primitive, return
  13. ClassCls = vec. getClass ();
  14. If(! Cls. isArray ()){
  15. ReturnVec;
  16. }
  17. If(! Cls. getComponentType (). isPrimitive ()){
  18. ReturnVec;
  19. }
  20. & Nbs

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.